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

Fix propagation of the expire time #7

Merged
merged 2 commits into from
Feb 27, 2023
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
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