You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
743 B
46 lines
743 B
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 |
|
}
|
|
|