From 4167b0885c442042da2210d71b9b06d32c285090 Mon Sep 17 00:00:00 2001 From: bjkxt <74945140+bjkxt@users.noreply.github.com> Date: Tue, 24 Nov 2020 15:30:29 +0800 Subject: [PATCH] Update lru.go add MAdd and MGet --- lru.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lru.go b/lru.go index aa52433..1220940 100644 --- a/lru.go +++ b/lru.go @@ -45,6 +45,15 @@ func (c *Cache) Add(key, value interface{}) (evicted bool) { return evicted } +// MAdd adds many values to the cache. Returns true if an eviction occurred. +func (c *Cache) MAdd(keys, values []interface{}, evicteds []bool) { + c.lock.Lock() + for i, key := range keys { + evicteds[i] = c.lru.Add(key, values[i]) + } + c.lock.Unlock() +} + // Get looks up a key's value from the cache. func (c *Cache) Get(key interface{}) (value interface{}, ok bool) { c.lock.Lock() @@ -53,6 +62,15 @@ func (c *Cache) Get(key interface{}) (value interface{}, ok bool) { return value, ok } +// MGet looks up many keys's value from the cache. +func (c *Cache) MGet(keys, values []interface{}, oks []bool) { + c.lock.Lock() + for i, key := range keys { + values[i], oks[i] = c.lru.Get(key) + } + c.lock.Unlock() +} + // Contains checks if a key is in the cache, without updating the // recent-ness or deleting it for being stale. func (c *Cache) Contains(key interface{}) bool {