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

Adding OnEvict callback for when items are removed #28

Merged
merged 3 commits into from
May 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lazylru.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type LazyLRU[K comparable, V any] struct {
lock sync.RWMutex
isRunning bool
isClosing bool
numEvictCB int32 // faster to check than locking and checking the length of onEvict
numEvictCB atomic.Int32 // faster to check than locking and checking the length of onEvict
}

// New creates a LazyLRU[string, interface{} with the given capacity and default
Expand Down Expand Up @@ -96,15 +96,15 @@ func NewT[K comparable, V any](maxItems int, ttl time.Duration) *LazyLRU[K, V] {
func (lru *LazyLRU[K, V]) OnEvict(cb EvictCB[K, V]) {
lru.lock.Lock()
lru.onEvict = append(lru.onEvict, cb)
lru.numEvictCB++
lru.numEvictCB.Add(1)
lru.lock.Unlock()
}

func (lru *LazyLRU[K, V]) execOnEvict(deathList []*item[K, V]) {
if len(deathList) == 0 {
return
}
if atomic.LoadInt32(&(lru.numEvictCB)) == 0 {
if lru.numEvictCB.Load() == 0 {
return
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func (lru *LazyLRU[K, V]) reap(start int, deathList []*item[K, V]) {
lru.lock.Unlock()
}
atomic.AddUint32(&lru.stats.ReaperCycles, cycles)
if len(aggDeathList) > 0 && atomic.LoadInt32(&lru.numEvictCB) > 0 {
if len(aggDeathList) > 0 && lru.numEvictCB.Load() > 0 {
lru.execOnEvict(aggDeathList)
}
}
Expand Down Expand Up @@ -392,7 +392,7 @@ func (lru *LazyLRU[K, V]) SetTTL(key K, value V, ttl time.Duration) {
lru.lock.Lock()
deathList := lru.setInternal(key, value, time.Now().Add(ttl))
lru.lock.Unlock()
if len(deathList) > 0 && atomic.LoadInt32(&lru.numEvictCB) > 0 {
if len(deathList) > 0 && lru.numEvictCB.Load() > 0 {
lru.execOnEvict(deathList)
}
}
Expand Down Expand Up @@ -455,7 +455,7 @@ func (lru *LazyLRU[K, V]) MSetTTL(keys []K, values []V, ttl time.Duration) error
deathList = append(deathList, lru.setInternal(keys[i], values[i], expiration)...)
}
lru.lock.Unlock()
if len(deathList) > 0 && atomic.LoadInt32(&lru.numEvictCB) > 0 {
if len(deathList) > 0 && lru.numEvictCB.Load() > 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

lru.execOnEvict(deathList)
}
return nil
Expand All @@ -480,7 +480,7 @@ func (lru *LazyLRU[K, V]) Delete(key K) {
lru.items.update(pqi, 0) // move this item to the top of the heap
deadguy := heap.Pop[*item[K, V]](&lru.items) // pop item from the top of the heap
lru.lock.Unlock()
if atomic.LoadInt32(&lru.numEvictCB) > 0 {
if lru.numEvictCB.Load() > 0 {
lru.execOnEvict([]*item[K, V]{deadguy})
}
}
Expand Down