Skip to content

Commit

Permalink
Merge pull request #52 from Code-Hex/fix/51
Browse files Browse the repository at this point in the history
fixed issue #51
  • Loading branch information
Code-Hex committed Apr 14, 2024
2 parents cf45b46 + c0de4d3 commit 4505cb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cache_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ func TestDeleteExpired(t *testing.T) {
t.Errorf("want2 %d items but got2 %d", want2, got2)
}
})

t.Run("issue #51", func(t *testing.T) {
defer restore()
c := New[string, int]()

c.Set("1", 10, WithExpiration(10*time.Millisecond))
c.Set("2", 20, WithExpiration(20*time.Millisecond))
c.Set("1", 30, WithExpiration(100*time.Millisecond)) // expected do not expired key "1"

nowFunc = func() time.Time {
return now.Add(30 * time.Millisecond).Add(time.Millisecond)
}

c.DeleteExpired()

got := c.Len()
if want := 1; want != got {
t.Errorf("want %d items but got %d", want, got)
}
})
}

func max(x, y int) int {
Expand Down
1 change: 1 addition & 0 deletions expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func newExpirationManager[K comparable]() *expirationManager[K] {

func (m *expirationManager[K]) update(key K, expiration time.Time) {
if e, ok := m.mapping[key]; ok {
e.expiration = expiration
heap.Fix(&m.queue, e.index)
} else {
v := &expirationKey[K]{
Expand Down

0 comments on commit 4505cb1

Please sign in to comment.