cacheinterface/lru.go

47 lines
743 B
Go

package cache
import (
"github.com/hashicorp/golang-lru"
"time"
)
type LruCache struct {
c *lru.Cache
}
func NewLruCache(size int) (CacheInterface, error) {
c, err := lru.New(size)
if err != nil {
return nil, err
}
return &LruCache{c: c}, nil
}
func (mc *LruCache) Has(key string) bool {
_, exists := mc.c.Get(key)
return exists
}
func (mc *LruCache) Get(key string, dst ...interface{}) ([]byte, error) {
item, exists := mc.c.Get(key)
if !exists {
return nil, ErrMemoryCacheNotExists
}
return memoryCacheGet(item, dst...)
}
func (mc *LruCache) Set(key string, val interface{}, ttl time.Duration) error {
mc.c.Add(key, val)
return nil
}
func (mc *LruCache) Del(key string) error {
mc.c.Remove(key)
return nil
}