Skip to content

Commit 8a7356c

Browse files
author
lixizan
committed
fix
1 parent 44ef192 commit 8a7356c

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

index.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (c *MemoryCache) GetWithTTL(key string, exp time.Duration) (any, bool) {
145145

146146
v.ExpireAt = c.getExp(exp)
147147
b.heap.Down(v.index, b.heap.Len())
148-
return v, true
148+
return v.Value, true
149149
}
150150

151151
// Delete
@@ -161,6 +161,7 @@ func (c *MemoryCache) Delete(key string) (deleted bool) {
161161

162162
b.heap.Delete(v.index)
163163
delete(b.Map, key)
164+
v.cb(v, ReasonDeleted)
164165
return true
165166
}
166167

index_test.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,41 @@ func TestMemoryCache_GetAndRefresh(t *testing.T) {
135135
}
136136

137137
func TestMemoryCache_Delete(t *testing.T) {
138-
var count = 10000
139-
var mc = New(WithInterval(100*time.Millisecond, 100*time.Millisecond))
140-
for i := 0; i < count; i++ {
141-
key := string(utils.AlphabetNumeric.Generate(8))
142-
exp := rand.Intn(1000) + 200
143-
mc.Set(key, 1, time.Duration(exp)*time.Millisecond)
144-
}
138+
t.Run("1", func(t *testing.T) {
139+
var count = 10000
140+
var mc = New(WithInterval(100*time.Millisecond, 100*time.Millisecond))
141+
for i := 0; i < count; i++ {
142+
key := string(utils.AlphabetNumeric.Generate(8))
143+
exp := rand.Intn(1000) + 200
144+
mc.Set(key, 1, time.Duration(exp)*time.Millisecond)
145+
}
145146

146-
var keys = mc.Keys("")
147-
for i := 0; i < 100; i++ {
148-
deleted := mc.Delete(keys[i])
149-
assert.True(t, deleted)
147+
var keys = mc.Keys("")
148+
for i := 0; i < 100; i++ {
149+
deleted := mc.Delete(keys[i])
150+
assert.True(t, deleted)
150151

151-
key := string(utils.AlphabetNumeric.Generate(8))
152-
deleted = mc.Delete(key)
153-
assert.False(t, deleted)
154-
}
155-
assert.Equal(t, mc.Len(), count-100)
152+
key := string(utils.AlphabetNumeric.Generate(8))
153+
deleted = mc.Delete(key)
154+
assert.False(t, deleted)
155+
}
156+
assert.Equal(t, mc.Len(), count-100)
157+
})
158+
159+
t.Run("2", func(t *testing.T) {
160+
var mc = New()
161+
var wg = &sync.WaitGroup{}
162+
wg.Add(1)
163+
mc.SetWithCallback("ming", 1, -1, func(ele *Element, reason Reason) {
164+
assert.Equal(t, reason, ReasonDeleted)
165+
wg.Done()
166+
})
167+
mc.SetWithCallback("ting", 2, -1, func(ele *Element, reason Reason) {
168+
wg.Done()
169+
})
170+
go mc.Delete("ming")
171+
wg.Wait()
172+
})
156173
}
157174

158175
func TestMaxCap(t *testing.T) {

types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Reason uint8
88
const (
99
ReasonExpired = Reason(0)
1010
ReasonOverflow = Reason(1)
11+
ReasonDeleted = Reason(2)
1112
)
1213

1314
type CallbackFunc func(ele *Element, reason Reason)

0 commit comments

Comments
 (0)