Skip to content

Commit 4505cb1

Browse files
authored
Merge pull request #52 from Code-Hex/fix/51
fixed issue #51
2 parents cf45b46 + c0de4d3 commit 4505cb1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cache_internal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ func TestDeleteExpired(t *testing.T) {
123123
t.Errorf("want2 %d items but got2 %d", want2, got2)
124124
}
125125
})
126+
127+
t.Run("issue #51", func(t *testing.T) {
128+
defer restore()
129+
c := New[string, int]()
130+
131+
c.Set("1", 10, WithExpiration(10*time.Millisecond))
132+
c.Set("2", 20, WithExpiration(20*time.Millisecond))
133+
c.Set("1", 30, WithExpiration(100*time.Millisecond)) // expected do not expired key "1"
134+
135+
nowFunc = func() time.Time {
136+
return now.Add(30 * time.Millisecond).Add(time.Millisecond)
137+
}
138+
139+
c.DeleteExpired()
140+
141+
got := c.Len()
142+
if want := 1; want != got {
143+
t.Errorf("want %d items but got %d", want, got)
144+
}
145+
})
126146
}
127147

128148
func max(x, y int) int {

expiration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func newExpirationManager[K comparable]() *expirationManager[K] {
2121

2222
func (m *expirationManager[K]) update(key K, expiration time.Time) {
2323
if e, ok := m.mapping[key]; ok {
24+
e.expiration = expiration
2425
heap.Fix(&m.queue, e.index)
2526
} else {
2627
v := &expirationKey[K]{

0 commit comments

Comments
 (0)