Skip to content

Commit

Permalink
Merge pull request #7 from elastic/fix-expiry-bug
Browse files Browse the repository at this point in the history
Fix propagation of the expire time
  • Loading branch information
sysulq committed Feb 27, 2023
2 parents 173cfc3 + 9fa9c6e commit 5e180dc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
11 changes: 9 additions & 2 deletions 2q.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,16 @@ func (c *TwoQueueCache) Get(key interface{}) (interface{}, bool) {

// If the value is contained in recent, then we
// promote it to frequent
if val, ok := c.recent.Peek(key); ok {
if val, expire, ok := c.recent.PeekWithExpireTime(key); ok {
c.recent.Remove(key)
c.frequent.Add(key, val)
var expireDuration time.Duration
if expire != nil {
expireDuration = expire.Sub(time.Now())
if expireDuration < 0 {
return nil, false
}
}
c.frequent.AddEx(key, val, expireDuration)
return val, ok
}

Expand Down
25 changes: 25 additions & 0 deletions 2q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lru
import (
"math/rand"
"testing"
"time"
)

func Benchmark2Q_Rand(b *testing.B) {
Expand Down Expand Up @@ -304,3 +305,27 @@ func Test2Q_Peek(t *testing.T) {
t.Errorf("should not have updated recent-ness of 1")
}
}

// Test that values expire as expected
func Test2Q_Expire(t *testing.T) {
l, err := New2Q(100)
if err != nil {
t.Fatalf("failed to create LRU: %v", err)
}

l.AddEx("hey", "hello", 300*time.Millisecond)

value, ok := l.Get("hey")
if !ok {
t.Fatal("failed to read back value")
}
if value.(string) != "hello" {
t.Errorf("expected \"hello\", got %v", value)
}

time.Sleep(500 * time.Millisecond)
_, ok = l.Get("hey")
if ok {
t.Errorf("cached didn't properly expire")
}
}
14 changes: 11 additions & 3 deletions simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,21 @@ func (c *LRU) Contains(key interface{}) (ok bool) {
// Returns the key value (or undefined if not found) without updating
// the "recently used"-ness of the key.
func (c *LRU) Peek(key interface{}) (value interface{}, ok bool) {
v, _, ok := c.PeekWithExpireTime(key)
return v, ok
}

// Returns the key value (or undefined if not found) and its associated expire
// time without updating the "recently used"-ness of the key.
func (c *LRU) PeekWithExpireTime(key interface{}) (
value interface{}, expire *time.Time, ok bool) {
if ent, ok := c.items[key]; ok {
if ent.Value.(*entry).IsExpired() {
return nil, false
return nil, nil, false
}
return ent.Value.(*entry).value, true
return ent.Value.(*entry).value, ent.Value.(*entry).expire, true
}
return nil, ok
return nil, nil, ok
}

// Remove removes the provided key from the cache, returning if the
Expand Down

0 comments on commit 5e180dc

Please sign in to comment.