Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Features: add MAdd and MGet #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down