-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlrucache.go
More file actions
47 lines (38 loc) · 1.01 KB
/
lrucache.go
File metadata and controls
47 lines (38 loc) · 1.01 KB
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
46
47
package lrucache
import (
"time"
"github.com/easy-cache/cache"
lru "github.com/hashicorp/golang-lru"
)
type lruCacheDriver struct {
lrucache *lru.Cache
}
func (lcd lruCacheDriver) Get(key string) ([]byte, bool, error) {
item, hit := lcd.lrucache.Get(key)
if hit == false {
return nil, false, nil
}
bs, ok := item.(*cache.Item).GetValue()
if ok == false {
_ = lcd.Del(key)
}
return bs, ok, nil
}
func (lcd lruCacheDriver) Set(key string, val []byte, ttl time.Duration) error {
lcd.lrucache.Add(key, cache.NewItem(val, ttl))
return nil
}
func (lcd lruCacheDriver) Del(key string) error {
lcd.lrucache.Remove(key)
return nil
}
func NewLRUDriver(lrucache *lru.Cache) cache.DriverInterface {
return lruCacheDriver{lrucache: lrucache}
}
func NewLRUCache(lrucache *lru.Cache, args ...interface{}) cache.Interface {
return cache.New(append(args, NewLRUDriver(lrucache))...)
}
func NewLRUCache2(size int, args ...interface{}) cache.Interface {
lruCache, _ := lru.New(size)
return NewLRUCache(lruCache, args...)
}