mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 20:39:40 -08:00
feat(cache): add timestamp
This commit is contained in:
parent
7d2001c936
commit
7738cad7cb
|
@ -3,24 +3,24 @@ package main
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
type concurrentMap struct {
|
type concurrentMap struct {
|
||||||
values map[string]string
|
values map[string]interface{}
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConcurrentMap() *concurrentMap {
|
func newConcurrentMap() *concurrentMap {
|
||||||
return &concurrentMap{
|
return &concurrentMap{
|
||||||
values: make(map[string]string),
|
values: make(map[string]interface{}),
|
||||||
lock: sync.RWMutex{},
|
lock: sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concurrentMap) set(key, value string) {
|
func (c *concurrentMap) set(key string, value interface{}) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
c.values[key] = value
|
c.values[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concurrentMap) get(key string) (string, bool) {
|
func (c *concurrentMap) get(key string) (interface{}, bool) {
|
||||||
c.lock.RLock()
|
c.lock.RLock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
||||||
if val, ok := c.values[key]; ok {
|
if val, ok := c.values[key]; ok {
|
||||||
|
@ -28,3 +28,13 @@ func (c *concurrentMap) get(key string) (string, bool) {
|
||||||
}
|
}
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *concurrentMap) remove(key string) {
|
||||||
|
c.lock.RLock()
|
||||||
|
defer c.lock.RUnlock()
|
||||||
|
delete(c.values, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *concurrentMap) list() map[string]interface{} {
|
||||||
|
return c.values
|
||||||
|
}
|
||||||
|
|
|
@ -52,7 +52,8 @@ type cache interface {
|
||||||
init(home string)
|
init(home string)
|
||||||
close()
|
close()
|
||||||
get(key string) (string, bool)
|
get(key string) (string, bool)
|
||||||
set(key, value string)
|
// ttl in seconds
|
||||||
|
set(key, value string, ttl int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
type environmentInfo interface {
|
type environmentInfo interface {
|
||||||
|
@ -97,7 +98,14 @@ func (c *commandCache) set(command, path string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandCache) get(command string) (string, bool) {
|
func (c *commandCache) get(command string) (string, bool) {
|
||||||
return c.commands.get(command)
|
cmd, found := c.commands.get(command)
|
||||||
|
if !found {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
if command, ok := cmd.(string); ok {
|
||||||
|
return command, true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
type tracer struct {
|
type tracer struct {
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
cachePath = "/.omp.cache"
|
cachePath = "/.omp.cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type cacheObject struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
TTL int64 `json:"ttl"`
|
||||||
|
}
|
||||||
|
|
||||||
type fileCache struct {
|
type fileCache struct {
|
||||||
cache *concurrentMap
|
cache *concurrentMap
|
||||||
home string
|
home string
|
||||||
|
@ -18,32 +24,49 @@ type fileCache struct {
|
||||||
func (fc *fileCache) init(home string) {
|
func (fc *fileCache) init(home string) {
|
||||||
fc.cache = newConcurrentMap()
|
fc.cache = newConcurrentMap()
|
||||||
fc.home = home
|
fc.home = home
|
||||||
content, err := ioutil.ReadFile(home + cachePath)
|
content, err := ioutil.ReadFile(fc.home + cachePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, line := range strings.Split(string(content), "\n") {
|
|
||||||
if len(line) == 0 || !strings.Contains(line, "=") {
|
var list map[string]*cacheObject
|
||||||
continue
|
if err := json.Unmarshal(content, &list); err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
kv := strings.SplitN(line, "=", 2)
|
|
||||||
fc.set(kv[0], kv[1])
|
for key, co := range list {
|
||||||
|
fc.cache.set(key, co)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc *fileCache) close() {
|
func (fc *fileCache) close() {
|
||||||
var sb strings.Builder
|
fc.set("hello", "world", 200)
|
||||||
for key, value := range fc.cache.values {
|
if dump, err := json.Marshal(fc.cache.list()); err == nil {
|
||||||
cacheEntry := fmt.Sprintf("%s=%s\n", key, value)
|
_ = ioutil.WriteFile(fc.home+cachePath, dump, 0644)
|
||||||
sb.WriteString(cacheEntry)
|
|
||||||
}
|
}
|
||||||
_ = ioutil.WriteFile(fc.home+cachePath, []byte(sb.String()), 0644)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc *fileCache) get(key string) (string, bool) {
|
func (fc *fileCache) get(key string) (string, bool) {
|
||||||
return fc.cache.get(key)
|
val, found := fc.cache.get(key)
|
||||||
|
if !found {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
co, ok := val.(*cacheObject)
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
expired := time.Now().Unix() >= (co.Timestamp + co.TTL*60)
|
||||||
|
if expired {
|
||||||
|
fc.cache.remove(key)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return co.Value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc *fileCache) set(key, value string) {
|
func (fc *fileCache) set(key, value string, ttl int64) {
|
||||||
fc.cache.set(key, value)
|
fc.cache.set(key, &cacheObject{
|
||||||
|
Value: value,
|
||||||
|
Timestamp: time.Now().Unix(),
|
||||||
|
TTL: ttl,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue