cache changes for evictBulk
This commit is contained in:
parent
738ddeb7e2
commit
71bfe9e709
29
cache.go
29
cache.go
|
@ -42,6 +42,7 @@ type cache struct {
|
||||||
items map[string]Item
|
items map[string]Item
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
onEvicted func(string, interface{})
|
onEvicted func(string, interface{})
|
||||||
|
onEvictedBulk func([]KeyAndValue)
|
||||||
janitor *janitor
|
janitor *janitor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -902,14 +903,14 @@ func (c *cache) delete(k string) (interface{}, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyAndValue struct {
|
type KeyAndValue struct {
|
||||||
key string
|
Key string
|
||||||
value interface{}
|
Value interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all expired items from the cache.
|
// Delete all expired items from the cache.
|
||||||
func (c *cache) DeleteExpired() {
|
func (c *cache) DeleteExpired() {
|
||||||
var evictedItems []keyAndValue
|
var evictedItems []KeyAndValue
|
||||||
now := time.Now().UnixNano()
|
now := time.Now().UnixNano()
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
for k, v := range c.items {
|
for k, v := range c.items {
|
||||||
|
@ -917,13 +918,20 @@ func (c *cache) DeleteExpired() {
|
||||||
if v.Expiration > 0 && now > v.Expiration {
|
if v.Expiration > 0 && now > v.Expiration {
|
||||||
ov, evicted := c.delete(k)
|
ov, evicted := c.delete(k)
|
||||||
if evicted {
|
if evicted {
|
||||||
evictedItems = append(evictedItems, keyAndValue{k, ov})
|
evictedItems = append(evictedItems, KeyAndValue{k, ov})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
// Call evict functions only when
|
||||||
|
// their respective functions exist.
|
||||||
|
if c.onEvictedBulk != nil{
|
||||||
|
c.onEvictedBulk(evictedItems)
|
||||||
|
}else if c.onEvicted != nil{
|
||||||
for _, v := range evictedItems {
|
for _, v := range evictedItems {
|
||||||
c.onEvicted(v.key, v.value)
|
c.onEvicted(v.Key, v.Value)
|
||||||
|
}
|
||||||
|
}else{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -936,6 +944,15 @@ func (c *cache) OnEvicted(f func(string, interface{})) {
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set an optional OnEvictedBulk which will be used to evict keys in bulk
|
||||||
|
// This is used when eviction happens in bulk when expiry is checked at
|
||||||
|
// regular intervals, we dont want to call onEvict multiple times
|
||||||
|
func (c *cache) OnEvictedBulk(f func([]KeyAndValue)) {
|
||||||
|
c.mu.Lock()
|
||||||
|
c.onEvictedBulk = f
|
||||||
|
c.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// Write the cache's items (using Gob) to an io.Writer.
|
// Write the cache's items (using Gob) to an io.Writer.
|
||||||
//
|
//
|
||||||
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
|
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
|
||||||
|
|
Loading…
Reference in New Issue