@@ -206,33 +206,42 @@ type flatentry struct {
206
206
207
207
func (f * flatentry ) insert (e * flatentry ) {
208
208
f .size += e .size
209
+ f .ttl = e .ttl
209
210
f .mu .Lock ()
210
211
e .ovfl = f .ovfl
211
212
f .ovfl = e
212
213
f .mu .Unlock ()
213
214
}
214
215
215
216
func (f * flatentry ) find (cmd string , ts int64 ) ([]byte , bool ) {
216
- for next := f ; next != nil ; {
217
- if ts >= next .ttl {
218
- return nil , true
219
- }
217
+ if f == nil {
218
+ return nil , false
219
+ }
220
+ if ts >= f .ttl {
221
+ return nil , true
222
+ }
223
+ return f ._find (cmd ), false
224
+ }
225
+
226
+ func (f * flatentry ) _find (cmd string ) []byte {
227
+ for next := f ; ; {
220
228
if cmd == next .cmd {
221
- return next .val , false
229
+ return next .val
222
230
}
223
231
next .mu .RLock ()
224
232
ovfl := next .ovfl
225
233
next .mu .RUnlock ()
226
- next = ovfl
234
+ if next = ovfl ; next == nil {
235
+ return nil
236
+ }
227
237
}
228
- return nil , false
229
238
}
230
239
231
240
const lrBatchSize = 64
232
241
const flattEntrySize = unsafe .Sizeof (flatentry {})
233
242
234
243
type lrBatch struct {
235
- m map [* flatentry ]bool
244
+ m map [* flatentry ]struct {}
236
245
}
237
246
238
247
func NewFlattenCache (limit int ) CacheStore {
@@ -247,7 +256,7 @@ func NewFlattenCache(limit int) CacheStore {
247
256
f .head .next = unsafe .Pointer (f .tail )
248
257
f .tail .prev = unsafe .Pointer (f .head )
249
258
f .lrup = sync.Pool {New : func () any {
250
- b := & lrBatch {m : make (map [* flatentry ]bool , lrBatchSize )}
259
+ b := & lrBatch {m : make (map [* flatentry ]struct {} , lrBatchSize )}
251
260
runtime .SetFinalizer (b , func (b * lrBatch ) {
252
261
if len (b .m ) >= 0 {
253
262
f .mu .Lock ()
@@ -292,13 +301,9 @@ func (f *flatten) llTail(e *flatentry) {
292
301
}
293
302
294
303
func (f * flatten ) llTailBatch (b * lrBatch ) {
295
- for e , expired := range b .m {
304
+ for e := range b .m {
296
305
if e .mark == f .mark {
297
- if expired {
298
- f .remove (e )
299
- } else {
300
- f .llTail (e )
301
- }
306
+ f .llTail (e )
302
307
}
303
308
}
304
309
clear (b .m )
@@ -315,20 +320,18 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
315
320
e := f .cache [key ]
316
321
f .mu .RUnlock ()
317
322
ts := now .UnixMilli ()
318
- if v , expired := e .find (cmd , ts ); v != nil || expired {
323
+ if v , _ := e .find (cmd , ts ); v != nil {
319
324
batch := f .lrup .Get ().(* lrBatch )
320
- batch .m [e ] = expired
325
+ batch .m [e ] = struct {}{}
321
326
if len (batch .m ) >= lrBatchSize {
322
327
f .mu .Lock ()
323
328
f .llTailBatch (batch )
324
329
f .mu .Unlock ()
325
330
}
326
331
f .lrup .Put (batch )
327
- if v != nil {
328
- var ret RedisMessage
329
- _ = ret .CacheUnmarshalView (v )
330
- return ret , nil
331
- }
332
+ var ret RedisMessage
333
+ _ = ret .CacheUnmarshalView (v )
334
+ return ret , nil
332
335
}
333
336
fk := key + cmd
334
337
f .mu .RLock ()
@@ -382,7 +385,12 @@ func (f *flatten) Update(key, cmd string, val RedisMessage) (sxat int64) {
382
385
f .remove (e )
383
386
ep = e .next
384
387
}
385
- if e := f .cache [key ]; e == nil {
388
+ e := f .cache [key ]
389
+ if e != nil && e ._find (cmd ) != nil {
390
+ f .remove (e )
391
+ e = nil
392
+ }
393
+ if e == nil {
386
394
fe .key = key
387
395
f .cache [key ] = fe
388
396
f .llAdd (fe )
0 commit comments