Skip to content

Commit ef4bd54

Browse files
committed
Always promote on set
It's fine to conditionally promote on Get, to avoid blocking on a get (see: #52) but a set _must_ promote else we can end with an entry in our buckets that isn't in our list. issue: #64
1 parent 325d078 commit ef4bd54

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

cache.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ func (c *Cache) Get(key string) *Item {
108108
return nil
109109
}
110110
if !item.Expired() {
111-
c.promote(item)
111+
select {
112+
case c.promotables <- item:
113+
default:
114+
}
112115
}
113116
return item
114117
}
@@ -273,7 +276,7 @@ func (c *Cache) set(key string, value interface{}, duration time.Duration, track
273276
if existing != nil {
274277
c.deletables <- existing
275278
}
276-
c.promote(item)
279+
c.promotables <- item
277280
return item
278281
}
279282

@@ -283,14 +286,6 @@ func (c *Cache) bucket(key string) *bucket {
283286
return c.buckets[h.Sum32()&c.bucketMask]
284287
}
285288

286-
func (c *Cache) promote(item *Item) {
287-
select {
288-
case c.promotables <- item:
289-
default:
290-
}
291-
292-
}
293-
294289
func (c *Cache) worker() {
295290
defer close(c.control)
296291
dropped := 0

layeredcache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ func (c *LayeredCache) Get(primary, secondary string) *Item {
6868
return nil
6969
}
7070
if item.expires > time.Now().UnixNano() {
71-
c.promote(item)
71+
select {
72+
case c.promotables <- item:
73+
default:
74+
}
7275
}
7376
return item
7477
}

0 commit comments

Comments
 (0)