@@ -206,33 +206,42 @@ type flatentry struct {
206206
207207func (f * flatentry ) insert (e * flatentry ) {
208208 f .size += e .size
209+ f .ttl = e .ttl
209210 f .mu .Lock ()
210211 e .ovfl = f .ovfl
211212 f .ovfl = e
212213 f .mu .Unlock ()
213214}
214215
215216func (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 ; ; {
220228 if cmd == next .cmd {
221- return next .val , false
229+ return next .val
222230 }
223231 next .mu .RLock ()
224232 ovfl := next .ovfl
225233 next .mu .RUnlock ()
226- next = ovfl
234+ if next = ovfl ; next == nil {
235+ return nil
236+ }
227237 }
228- return nil , false
229238}
230239
231240const lrBatchSize = 64
232241const flattEntrySize = unsafe .Sizeof (flatentry {})
233242
234243type lrBatch struct {
235- m map [* flatentry ]bool
244+ m map [* flatentry ]struct {}
236245}
237246
238247func NewFlattenCache (limit int ) CacheStore {
@@ -247,7 +256,7 @@ func NewFlattenCache(limit int) CacheStore {
247256 f .head .next = unsafe .Pointer (f .tail )
248257 f .tail .prev = unsafe .Pointer (f .head )
249258 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 )}
251260 runtime .SetFinalizer (b , func (b * lrBatch ) {
252261 if len (b .m ) >= 0 {
253262 f .mu .Lock ()
@@ -292,13 +301,9 @@ func (f *flatten) llTail(e *flatentry) {
292301}
293302
294303func (f * flatten ) llTailBatch (b * lrBatch ) {
295- for e , expired := range b .m {
304+ for e := range b .m {
296305 if e .mark == f .mark {
297- if expired {
298- f .remove (e )
299- } else {
300- f .llTail (e )
301- }
306+ f .llTail (e )
302307 }
303308 }
304309 clear (b .m )
@@ -315,20 +320,18 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
315320 e := f .cache [key ]
316321 f .mu .RUnlock ()
317322 ts := now .UnixMilli ()
318- if v , expired := e .find (cmd , ts ); v != nil || expired {
323+ if v , _ := e .find (cmd , ts ); v != nil {
319324 batch := f .lrup .Get ().(* lrBatch )
320- batch .m [e ] = expired
325+ batch .m [e ] = struct {}{}
321326 if len (batch .m ) >= lrBatchSize {
322327 f .mu .Lock ()
323328 f .llTailBatch (batch )
324329 f .mu .Unlock ()
325330 }
326331 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
332335 }
333336 fk := key + cmd
334337 f .mu .RLock ()
@@ -382,7 +385,12 @@ func (f *flatten) Update(key, cmd string, val RedisMessage) (sxat int64) {
382385 f .remove (e )
383386 ep = e .next
384387 }
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 {
386394 fe .key = key
387395 f .cache [key ] = fe
388396 f .llAdd (fe )
0 commit comments