Skip to content

Commit a6f9aa9

Browse files
committed
Clean up the finalization of records
1 parent 7ed4369 commit a6f9aa9

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

cache.go

+21-27
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ func (c *Cache) Set(key, value interface{}, ttl time.Duration) {
141141
for {
142142
old, loaded := c.records.LoadOrStore(key, r)
143143
if !loaded {
144-
r.init(value, ttl)
145144
if ttl > 0 {
146145
c.runLoop()
147146
}
147+
r.init(value, ttl)
148148
if front := c.list.PushBack(key, r.ring); front != nil {
149149
c.Evict(front)
150150
}
@@ -154,7 +154,7 @@ func (c *Cache) Set(key, value interface{}, ttl time.Duration) {
154154
c.mu.Lock()
155155
defer c.mu.Unlock()
156156
if c.deleteIfEqualsLocked(key, old.(*record)) {
157-
c.finalizeAsync(key, old.(*record))
157+
c.finalizeSync(key, old.(*record))
158158
}
159159
}()
160160
}
@@ -180,10 +180,10 @@ func (c *Cache) Fetch(key interface{}, ttl time.Duration, f FetchCallback) (valu
180180
c.deleteIfEqualsLocked(key, r)
181181
return nil, false
182182
}
183-
r.init(value, ttl)
184183
if ttl > 0 {
185184
c.runLoop()
186185
}
186+
r.init(value, ttl)
187187
r.wg.Add(1)
188188
if front := c.list.PushBack(key, r.ring); front != nil {
189189
c.Evict(front)
@@ -331,49 +331,43 @@ func (c *Cache) deleteIfEqualsLocked(key interface{}, r *record) bool {
331331
return true
332332
}
333333

334-
func (c *Cache) finalizeSync(key interface{}, r *record) (interface{}, bool) {
334+
func (c *Cache) remove(key interface{}, r *record) (interface{}, bool) {
335335
value, ok := r.LoadAndReset()
336336
if !ok {
337+
// An inactive record which had a concurrent Fetch and failed.
337338
return nil, false
338339
}
339340
if k := c.list.Remove(r.ring); k != nil && k != key {
340341
panic("evcache: invalid ring")
341342
}
342-
if readers := atomic.LoadUint32(&r.readers); readers == 0 {
343+
return value, true
344+
}
345+
346+
func (c *Cache) finalizeSync(key interface{}, r *record) (interface{}, bool) {
347+
value, ok := c.remove(key, r)
348+
if !ok {
349+
return nil, false
350+
}
351+
c.wg.Add(1)
352+
go func() {
353+
defer c.wg.Done()
354+
r.wg.Wait()
343355
c.pool.Put(r)
344356
if c.afterEvict != nil {
345-
c.wg.Add(1)
346-
go func() {
347-
defer c.wg.Done()
348-
c.afterEvict(key, value)
349-
}()
357+
c.afterEvict(key, value)
350358
}
351-
} else if readers > 0 {
352-
c.wg.Add(1)
353-
go func() {
354-
defer c.wg.Done()
355-
r.wg.Wait()
356-
c.pool.Put(r)
357-
if c.afterEvict != nil {
358-
c.afterEvict(key, value)
359-
}
360-
}()
361-
}
359+
}()
362360
return value, true
363361
}
364362

365363
func (c *Cache) finalizeAsync(key interface{}, r *record) {
366364
c.wg.Add(1)
367365
go func() {
368366
defer c.wg.Done()
369-
value, ok := r.LoadAndReset()
367+
value, ok := c.remove(key, r)
370368
if !ok {
371-
// An inactive record which had a concurrent Fetch and failed.
372369
return
373370
}
374-
if k := c.list.Remove(r.ring); k != nil && k != key {
375-
panic("evcache: invalid ring")
376-
}
377371
r.wg.Wait()
378372
c.pool.Put(r)
379373
if c.afterEvict != nil {
@@ -411,7 +405,7 @@ func (c *Cache) processRecords(now int64) {
411405
c.mu.Lock()
412406
defer c.mu.Unlock()
413407
if c.deleteIfEqualsLocked(key, r) {
414-
c.finalizeAsync(key, r)
408+
c.finalizeSync(key, r)
415409
}
416410
return true
417411
}

record.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ const (
1313
)
1414

1515
type record struct {
16-
mu sync.RWMutex
17-
wg sync.WaitGroup
18-
readers uint32
19-
ring *ring.Ring
16+
mu sync.RWMutex
17+
wg sync.WaitGroup
18+
ring *ring.Ring
2019

2120
value interface{}
2221
expires int64
@@ -29,7 +28,6 @@ func newRecord() *record {
2928
}
3029

3130
func (r *record) Close() error {
32-
atomic.AddUint32(&r.readers, ^uint32(0))
3331
r.wg.Done()
3432
return nil
3533
}
@@ -53,7 +51,6 @@ func (r *record) LoadAndHit() (interface{}, bool) {
5351
if !r.IsActive() {
5452
return nil, false
5553
}
56-
atomic.AddUint32(&r.readers, 1)
5754
atomic.AddUint32(&r.hits, 1)
5855
r.wg.Add(1)
5956
return r.value, true

0 commit comments

Comments
 (0)