-
Notifications
You must be signed in to change notification settings - Fork 140
/
localcache.go
45 lines (38 loc) · 891 Bytes
/
localcache.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package localcache
import (
"github.com/coocood/freecache"
"sync"
)
const (
minCacheSize = 512 * 1024 // 512kb
maxCacheSize = 20 * 1024 * 1024 // 20m
)
var (
once sync.Once
ins *LocalCache
)
type LocalCache struct {
c *freecache.Cache
}
func NewLocalCache(size int) *LocalCache {
c := freecache.NewCache(size)
return &LocalCache{c: c}
}
func LoadLocalCache(size int) *LocalCache {
once.Do(func() {
ins = NewLocalCache(size)
})
return ins
}
func (lc *LocalCache) Get(key string) (value []byte, err error) {
return lc.c.Get([]byte(key))
}
func (lc *LocalCache) Set(key string, val []byte) error {
return lc.SetWithExpire(key, val, 0)
}
func (lc *LocalCache) SetWithExpire(key string, val []byte, expireSeconds int) error {
return lc.c.Set([]byte(key), val, expireSeconds)
}
func (lc *LocalCache) Del(key string) (affected bool) {
return lc.c.Del([]byte(key))
}