Skip to content

Commit b8175b2

Browse files
committed
pref: avoid CacheUnmarshalView in locks
1 parent 180872a commit b8175b2

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

cache.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,20 @@ func (f *flatentry) insert(e *flatentry) {
212212
f.mu.Unlock()
213213
}
214214

215-
func (f *flatentry) find(cmd string, ts int64) (ret RedisMessage, expired bool) {
215+
func (f *flatentry) find(cmd string, ts int64) ([]byte, bool) {
216216
for next := f; next != nil; {
217217
if ts >= next.ttl {
218-
expired = true
219-
return
218+
return nil, true
220219
}
221220
if cmd == next.cmd {
222-
_ = ret.CacheUnmarshalView(next.val)
223-
return
221+
return next.val, false
224222
}
225223
next.mu.RLock()
226224
ovfl := next.ovfl
227225
next.mu.RUnlock()
228226
next = ovfl
229227
}
230-
return
228+
return nil, false
231229
}
232230

233231
const lrBatchSize = 64
@@ -311,7 +309,7 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
311309
e := f.cache[key]
312310
f.mu.RUnlock()
313311
ts := now.UnixMilli()
314-
if v, _ := e.find(cmd, ts); v.typ != 0 {
312+
if v, _ := e.find(cmd, ts); v != nil {
315313
batch := f.lrup.Get().(*lrBatch)
316314
batch.m[e] = struct{}{}
317315
if len(batch.m) == lrBatchSize {
@@ -320,7 +318,9 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
320318
f.mu.Unlock()
321319
}
322320
f.lrup.Put(batch)
323-
return v, nil
321+
var ret RedisMessage
322+
_ = ret.CacheUnmarshalView(v)
323+
return ret, nil
324324
}
325325
fk := key + cmd
326326
f.mu.RLock()
@@ -333,9 +333,11 @@ func (f *flatten) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
333333
defer f.mu.Unlock()
334334
e = f.cache[key]
335335
v, expired := e.find(cmd, ts)
336-
if v.typ != 0 {
336+
if v != nil {
337337
f.llTail(e)
338-
return v, nil
338+
var ret RedisMessage
339+
_ = ret.CacheUnmarshalView(v)
340+
return ret, nil
339341
}
340342
if expired {
341343
f.remove(e)

0 commit comments

Comments
 (0)