github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/cache/lrucache.go (about) 1 package cache 2 3 import ( 4 lru "github.com/hashicorp/golang-lru" 5 "github.com/pkg/errors" 6 "github.com/unionj-cloud/go-doudou/toolkit/caller" 7 "time" 8 ) 9 10 type LruCache struct { 11 *base 12 } 13 14 func NewLruCache(size int, ttl time.Duration) *LruCache { 15 store, err := lru.New(size) 16 if err != nil { 17 panic(errors.Wrap(err, caller.NewCaller().String())) 18 } 19 return &LruCache{ 20 newBase(&LruCacheAdapter{store}, ttl), 21 } 22 } 23 24 type LruCacheAdapter struct { 25 store *lru.Cache 26 } 27 28 func (l *LruCacheAdapter) Get(key interface{}) (value interface{}, ok bool) { 29 return l.store.Get(key) 30 } 31 32 func (l *LruCacheAdapter) Add(key, value interface{}) { 33 l.store.Add(key, value) 34 } 35 36 func (l *LruCacheAdapter) Remove(key interface{}) { 37 l.store.Remove(key) 38 }