/
cache.go
30 lines (24 loc) · 906 Bytes
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package character
import (
"time"
gocache "github.com/patrickmn/go-cache"
)
// Cache is an instance of a key-value store with contents specific to
// each instance and are not shared between instances. By default its
// entries do not expire without a TTL.
type Cache struct {
cacheInstance *gocache.Cache
}
func NewCache() *Cache {
return &Cache{cacheInstance: gocache.New(-1, 10*time.Second)}
}
// Put sets a key/value pair in the cache with an optional duration. Passing 0 for
// ttl will cause the default expiration to be used and -1 will not set a ttl.
func (c *Cache) Put(key string, value interface{}, ttl time.Duration) {
c.cacheInstance.Set(key, value, ttl)
}
// Get fetches a value from the cache, returning the value as well as whether
// or not the value was found (semantics similar to map).
func (c *Cache) Get(key string) (interface{}, bool) {
return c.cacheInstance.Get(key)
}