go-pkgz / expirable-cache Goto Github PK
View Code? Open in Web Editor NEWExpirable Go LRC\LRU cache without goroutines
Home Page: https://go-pkgz.umputun.dev/expirable-cache/
License: MIT License
Expirable Go LRC\LRU cache without goroutines
Home Page: https://go-pkgz.umputun.dev/expirable-cache/
License: MIT License
This cache is heavily inspired by hashicorp/golang-lru simplelru implementation.
How does it differ?
I'd like to store the cache on disk when I close my application, but I need a way to get the expiry time for each item so I can store it.
I'd also need to set it back later, so having the option to set the ttl with time.Time
would also be handy, but not as important since I can just subtract the current time.
The following code returns nil regardless of what key is passed in. The keys are obviously there and returned by the .Keys()
function
Why can I not access the actual cache values?
package cache
import (
"errors"
"fmt"
"log"
"time"
common "github.com/awilson9/redis_proxy/go/pkg/common"
config "github.com/awilson9/redis_proxy/go/pkg/config"
cache "github.com/go-pkgz/expirable-cache"
lru "github.com/go-pkgz/expirable-cache"
)
type LRUCache struct {
Cache lru.Cache
Expiry int
}
func (lruCache *LRUCache) InitCache() {
cfg, err := config.NewConfig(common.ConfigPath)
if err != nil {
log.Fatal(err)
}
var capacity = cfg.App.Cache.Capacity
var ttl = cfg.App.Cache.Expiry
log.Printf("Initializing LRU cache with capacity %v expiration %v", capacity, ttl)
cache, err := lru.NewCache(cache.MaxKeys(capacity), cache.TTL(time.Duration(ttl)*time.Millisecond))
if err != nil {
log.Fatal(err)
}
lruCache.Expiry = ttl
lruCache.Cache = cache
}
func (lruCache *LRUCache) GetFromCache(lruCacheKey string) (value string, ok bool) {
interfaceValue, ok := lruCache.Cache.Get(lruCacheKey)
var valueStr string = ""
if interfaceValue != nil {
valueStr = interfaceValue.(string)
}
return valueStr, ok
}
func (lruCache *LRUCache) AddToCache(lruCacheKey string, lruCacheValue string) {
lruCache.Cache.Set(lruCacheKey, lruCacheValue, 0)
}
func (lruCache *LRUCache) DumpCache() ([]common.CacheEntry, error) {
keys := lruCache.Cache.Keys()
cacheValues := make([]common.CacheEntry, len(keys))
log.Printf("%v", keys)
for _, key := range keys {
log.Printf("key: %s", key)
value, ok := lruCache.GetFromCache(key)
log.Printf("value: %v", value)
if ok {
cacheValues = append(cacheValues, common.CacheEntry{Key: key, Value: fmt.Sprintf("%v", value)})
} else {
return nil, errors.New("Unable to dump cache")
}
}
return cacheValues, nil
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.