Skip to content

Commit

Permalink
add flush expires on close
Browse files Browse the repository at this point in the history
  • Loading branch information
c3mb0 committed Oct 6, 2021
1 parent a882c30 commit 70a628b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions niftycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Cache struct {
setCB callback
expireCB callback
extendTTL bool
flushExpires bool
items map[string]*item
ih *itemsHeap
m *sync.Mutex
Expand Down Expand Up @@ -64,6 +65,12 @@ func ExtendTTLOnHit() Option {
}
}

func FlushExpiresOnClose() Option {
return func(nc *Cache) {
nc.flushExpires = true
}
}

func MaxExpires(maxExpires int) Option {
return func(nc *Cache) {
nc.maxExpires = maxExpires
Expand Down Expand Up @@ -149,6 +156,17 @@ func (nc *Cache) handleExpirations() {
select {
case <-nc.done:
t.Stop()
if nc.flushExpires {
nc.m.Lock()
for item := nc.ih.peek(); item != nil; item = nc.ih.peek() {
delete(nc.items, item.key)
nc.ih.pop()
if nc.expireCB != nil {
nc.callbacks.Add(nc.expireCB(item.key, item.value))
}
}
nc.m.Unlock()
}
nc.wg1.Done()
return
case <-t.C:
Expand Down

0 comments on commit 70a628b

Please sign in to comment.