Skip to content

Commit

Permalink
Merge pull request #2 from ezbuy/feature/cache-remove-return-bool
Browse files Browse the repository at this point in the history
lru: move returns boolean
  • Loading branch information
scbizu authored Aug 13, 2020
2 parents 8c9f03a + e77cba2 commit e4a939c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ func (c *Cache) Get(key Key) (value interface{}, ok bool) {
}

// Remove removes the provided key from the cache.
func (c *Cache) Remove(key Key) {
func (c *Cache) Remove(key Key) bool {
if c.cache == nil {
return
return false
}
if ele, hit := c.cache[key]; hit {
c.removeElement(ele)
return c.removeElement(ele)
}
return false
}

// RemoveOldest removes the oldest item from the cache.
Expand All @@ -103,13 +104,14 @@ func (c *Cache) RemoveOldest() {
}
}

func (c *Cache) removeElement(e *list.Element) {
c.ll.Remove(e)
func (c *Cache) removeElement(e *list.Element) bool {
n := c.ll.Remove(e)
kv := e.Value.(*entry)
delete(c.cache, kv.key)
if c.OnEvicted != nil {
c.OnEvicted(kv.key, kv.value)
}
return n != nil
}

// Len returns the number of items in the cache.
Expand Down

0 comments on commit e4a939c

Please sign in to comment.