@@ -232,7 +232,7 @@ const lrBatchSize = 64
232
232
const flattEntrySize = unsafe .Sizeof (flatentry {})
233
233
234
234
type lrBatch struct {
235
- m map [* flatentry ]struct {}
235
+ m map [* flatentry ]bool
236
236
}
237
237
238
238
func NewFlattenCache (limit int ) CacheStore {
@@ -247,7 +247,7 @@ func NewFlattenCache(limit int) CacheStore {
247
247
f .head .next = unsafe .Pointer (f .tail )
248
248
f .tail .prev = unsafe .Pointer (f .head )
249
249
f .lrup = sync.Pool {New : func () any {
250
- b := & lrBatch {m : make (map [* flatentry ]struct {} , lrBatchSize )}
250
+ b := & lrBatch {m : make (map [* flatentry ]bool , lrBatchSize )}
251
251
runtime .SetFinalizer (b , func (b * lrBatch ) {
252
252
if len (b .m ) >= 0 {
253
253
f .mu .Lock ()
@@ -287,15 +287,19 @@ func (f *flatten) llDel(e *flatentry) {
287
287
}
288
288
289
289
func (f * flatten ) llTail (e * flatentry ) {
290
- if e .mark == f .mark {
291
- f .llDel (e )
292
- f .llAdd (e )
293
- }
290
+ f .llDel (e )
291
+ f .llAdd (e )
294
292
}
295
293
296
294
func (f * flatten ) llTailBatch (b * lrBatch ) {
297
- for e := range b .m {
298
- f .llTail (e )
295
+ for e , expired := range b .m {
296
+ if e .mark == f .mark {
297
+ if expired {
298
+ f .remove (e )
299
+ } else {
300
+ f .llTail (e )
301
+ }
302
+ }
299
303
}
300
304
clear (b .m )
301
305
}
@@ -311,18 +315,20 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
311
315
e := f .cache [key ]
312
316
f .mu .RUnlock ()
313
317
ts := now .UnixMilli ()
314
- if v , _ := e .find (cmd , ts ); v != nil {
318
+ if v , expired := e .find (cmd , ts ); v != nil || expired {
315
319
batch := f .lrup .Get ().(* lrBatch )
316
- batch .m [e ] = struct {}{}
320
+ batch .m [e ] = expired
317
321
if len (batch .m ) >= lrBatchSize {
318
322
f .mu .Lock ()
319
323
f .llTailBatch (batch )
320
324
f .mu .Unlock ()
321
325
}
322
326
f .lrup .Put (batch )
323
- var ret RedisMessage
324
- _ = ret .CacheUnmarshalView (v )
325
- return ret , nil
327
+ if v != nil {
328
+ var ret RedisMessage
329
+ _ = ret .CacheUnmarshalView (v )
330
+ return ret , nil
331
+ }
326
332
}
327
333
fk := key + cmd
328
334
f .mu .RLock ()
0 commit comments