Skip to content

feat: add reason to OnEvictCallback #37

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

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 8 additions & 7 deletions s3fifo/s3fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package s3fifo
import (
"container/list"
"context"
"github.com/scalalang2/golang-fifo/types"
"sync"
"time"

"github.com/scalalang2/golang-fifo/types"
)

const numberOfBuckets = 100
Expand Down Expand Up @@ -150,7 +151,7 @@ func (s *S3FIFO[K, V]) Remove(key K) (ok bool) {
s.mu.Lock()
defer s.mu.Unlock()
if e, ok := s.items[key]; ok {
s.removeEntry(e)
s.removeEntry(e, types.EvictReasonRemoved)
return true
}

Expand Down Expand Up @@ -209,9 +210,9 @@ func (s *S3FIFO[K, V]) Close() {
s.mu.Unlock()
}

func (s *S3FIFO[K, V]) removeEntry(e *entry[K, V]) {
func (s *S3FIFO[K, V]) removeEntry(e *entry[K, V], reason types.EvictReason) {
if s.callback != nil {
s.callback(e.key, e.value)
s.callback(e.key, e.value, reason)
}

if s.ghost.contains(e.key) {
Expand Down Expand Up @@ -256,7 +257,7 @@ func (s *S3FIFO[K, V]) deleteExpired() {
}

for _, e := range bucket.entries {
s.removeEntry(e)
s.removeEntry(e, types.EvictReasonExpired)
}

s.mu.Unlock()
Expand Down Expand Up @@ -292,7 +293,7 @@ func (s *S3FIFO[K, V]) evictFromSmall() {
s.evictFromMain()
}
} else {
s.removeEntry(el)
s.removeEntry(el, types.EvictReasonEvicted)
s.ghost.add(key)
evicted = true
delete(s.items, key)
Expand All @@ -314,7 +315,7 @@ func (s *S3FIFO[K, V]) evictFromMain() {
s.items[key].freq -= 1
s.items[key].element = s.main.PushFront(el.key)
} else {
s.removeEntry(el)
s.removeEntry(el, types.EvictReasonEvicted)
evicted = true
delete(s.items, key)
}
Expand Down
5 changes: 3 additions & 2 deletions s3fifo/s3fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"fortio.org/assert"
"github.com/scalalang2/golang-fifo/types"
)

const noEvictionTTL = 0
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestEvictionCallback(t *testing.T) {
cache := New[int, int](10, noEvictionTTL)
evicted := make(map[int]int)

cache.SetOnEvicted(func(key int, value int) {
cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
evicted[key] = value
})

Expand All @@ -216,7 +217,7 @@ func TestEvictionCallbackWithTTL(t *testing.T) {
var mu sync.Mutex
cache := New[int, int](10, time.Second)
evicted := make(map[int]int)
cache.SetOnEvicted(func(key int, value int) {
cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
mu.Lock()
evicted[key] = value
mu.Unlock()
Expand Down
38 changes: 20 additions & 18 deletions sieve/sieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,25 @@ func New[K comparable, V any](size int, ttl time.Duration) *Sieve[K, V] {
}

if ttl != 0 {
go func(ctx context.Context) {
ticker := time.NewTicker(ttl / numberOfBuckets)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
cache.deleteExpired()
}
}
}(cache.ctx)
go cache.cleanup(cache.ctx)
}

return cache
}

func (s *Sieve[K, V]) cleanup(ctx context.Context) {
ticker := time.NewTicker(s.ttl / numberOfBuckets)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.deleteExpired()
}
}
}

func (s *Sieve[K, V]) Set(key K, value V) {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down Expand Up @@ -147,7 +149,7 @@ func (s *Sieve[K, V]) Remove(key K) (ok bool) {
s.hand = s.hand.Prev()
}

s.removeEntry(e)
s.removeEntry(e, types.EvictReasonRemoved)
return true
}

Expand Down Expand Up @@ -191,7 +193,7 @@ func (s *Sieve[K, V]) Purge() {
defer s.mu.Unlock()

for _, e := range s.items {
s.removeEntry(e)
s.removeEntry(e, types.EvictReasonRemoved)
}

for i := range s.buckets {
Expand All @@ -213,9 +215,9 @@ func (s *Sieve[K, V]) Close() {
s.mu.Unlock()
}

func (s *Sieve[K, V]) removeEntry(e *entry[K, V]) {
func (s *Sieve[K, V]) removeEntry(e *entry[K, V], reason types.EvictReason) {
if s.callback != nil {
s.callback(e.key, e.value)
s.callback(e.key, e.value, reason)
}

s.ll.Remove(e.element)
Expand Down Expand Up @@ -249,7 +251,7 @@ func (s *Sieve[K, V]) evict() {
}

s.hand = o.Prev()
s.removeEntry(el)
s.removeEntry(el, types.EvictReasonEvicted)
}

func (s *Sieve[K, V]) addToBucket(e *entry[K, V]) {
Expand Down Expand Up @@ -285,7 +287,7 @@ func (s *Sieve[K, V]) deleteExpired() {
}

for _, e := range bucket.entries {
s.removeEntry(e)
s.removeEntry(e, types.EvictReasonExpired)
}

s.mu.Unlock()
Expand Down
5 changes: 3 additions & 2 deletions sieve/sieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"fortio.org/assert"
"github.com/scalalang2/golang-fifo/types"
)

const noEvictionTTL = 0
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestEvictionCallback(t *testing.T) {
cache := New[int, int](10, noEvictionTTL)
evicted := make(map[int]int)

cache.SetOnEvicted(func(key int, value int) {
cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
evicted[key] = value
})

Expand All @@ -170,7 +171,7 @@ func TestEvictionCallbackWithTTL(t *testing.T) {
var mu sync.Mutex
cache := New[int, int](10, time.Second)
evicted := make(map[int]int)
cache.SetOnEvicted(func(key int, value int) {
cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
mu.Lock()
evicted[key] = value
mu.Unlock()
Expand Down
15 changes: 14 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package types

type OnEvictCallback[K comparable, V any] func(key K, value V)
// EvictReason is the reason for an entry to be evicted from the cache.
// It is used in the [OnEvictCallback] function.
type EvictReason int

const (
// EvictReasonExpired is used when an item is removed because its TTL has expired.
EvictReasonExpired = iota
// EvictReasonEvicted is used when an item is removed because the cache size limit was exceeded.
EvictReasonEvicted
// EvictReasonRemoved is used when an item is explicitly deleted.
EvictReasonRemoved
)

type OnEvictCallback[K comparable, V any] func(key K, value V, reason EvictReason)

// Cache is the interface for a cache.
type Cache[K comparable, V any] interface {
Expand Down
Loading