This is a ttlcache in the Go.
go get github.com/0x5010/ttlcache
Get and set with default ttlcache:
// cache bytes
ttlcache.Set("key", []byte("value"), time.Duration(30*time.Second))
// get
b, ok := ttlcache.Get("key")
// cache other with gob
type Test struct {
V [][]string
}
var test Test
...
var serialize bytes.Buffer
encoder := gob.NewEncoder(&serialize)
err := encoder.Encode(testStruct)
if err != nil {
return nil, err
}
ttlcache.Set("key", serialize.Bytes(), time.Duration(30*time.Second))
// get
b, ok := ttlcache.Get("key")
t := &Test{}
decoder := gob.NewDecoder(bytes.NewReader(b))
err := decoder.Decode(t)
new cache:
myCache := ttlcache.New(time.Duration(20 * time.Minute))
myCache.Set("key", []byte("value"), time.Duration(30*time.Second))
b, ok := myCache.Get("key")