Skip to content

Commit 451f5a6

Browse files
committed
Add GetWithoutPromote
This gets the value without promoting it (sort of like a Peak)
1 parent ef4bd54 commit 451f5a6

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

cache.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ func (c *Cache) Get(key string) *Item {
116116
return item
117117
}
118118

119+
// Same as Get but does not promote the value. This essentially circumvents the
120+
// "least recently used" aspect of this cache. To some degree, it's akin to a
121+
// "peak"
122+
func (c *Cache) GetWithoutPromote(key string) *Item {
123+
return c.bucket(key).get(key)
124+
}
125+
119126
// Used when the cache was created with the Track() configuration option.
120127
// Avoid otherwise
121128
func (c *Cache) TrackingGet(key string) TrackedItem {

cache_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ func (_ CacheTests) PromotedItemsDontGetPruned() {
144144
Expect(cache.Get("11").Value()).To.Equal(11)
145145
}
146146

147+
func (_ CacheTests) GetWithoutPromoteDoesNotPromote() {
148+
cache := New(Configure().ItemsToPrune(10).GetsPerPromote(1))
149+
for i := 0; i < 500; i++ {
150+
cache.Set(strconv.Itoa(i), i, time.Minute)
151+
}
152+
cache.SyncUpdates()
153+
cache.GetWithoutPromote("9")
154+
cache.SyncUpdates()
155+
cache.GC()
156+
Expect(cache.Get("9")).To.Equal(nil)
157+
Expect(cache.Get("10").Value()).To.Equal(10)
158+
Expect(cache.Get("11").Value()).To.Equal(11)
159+
}
160+
147161
func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
148162
cache := New(Configure().ItemsToPrune(11).Track())
149163
item0 := cache.TrackingSet("0", 0, time.Minute)

layeredcache.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ func (c *LayeredCache) Get(primary, secondary string) *Item {
7676
return item
7777
}
7878

79+
// Same as Get but does not promote the value. This essentially circumvents the
80+
// "least recently used" aspect of this cache. To some degree, it's akin to a
81+
// "peak"
82+
func (c *LayeredCache) GetWithoutPromote(primary, secondary string) *Item {
83+
return c.bucket(primary).get(primary, secondary)
84+
}
85+
7986
func (c *LayeredCache) ForEachFunc(primary string, matches func(key string, item *Item) bool) {
8087
c.bucket(primary).forEachFunc(primary, matches)
8188
}

layeredcache_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ func (_ LayeredCacheTests) PromotedItemsDontGetPruned() {
194194
Expect(cache.Get("11", "a").Value()).To.Equal(11)
195195
}
196196

197+
func (_ LayeredCacheTests) GetWithoutPromoteDoesNotPromote() {
198+
cache := Layered(Configure().ItemsToPrune(10).GetsPerPromote(1))
199+
for i := 0; i < 500; i++ {
200+
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
201+
}
202+
cache.SyncUpdates()
203+
cache.GetWithoutPromote("9", "a")
204+
cache.SyncUpdates()
205+
cache.GC()
206+
Expect(cache.Get("9", "a")).To.Equal(nil)
207+
Expect(cache.Get("10", "a").Value()).To.Equal(10)
208+
Expect(cache.Get("11", "a").Value()).To.Equal(11)
209+
}
210+
197211
func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
198212
cache := Layered(Configure().ItemsToPrune(10).Track())
199213
item0 := cache.TrackingSet("0", "a", 0, time.Minute)

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ The returned `*Item` exposes a number of methods:
6969

7070
By returning expired items, CCache lets you decide if you want to serve stale content or not. For example, you might decide to serve up slightly stale content (< 30 seconds old) while re-fetching newer data in the background. You might also decide to serve up infinitely stale content if you're unable to get new data from your source.
7171

72+
### GetWithoutPromote
73+
Same as `Get` but does not "promote" the value, which is to say it circumvents the "lru" aspect of this cache. Should only be used in limited cases, such as peaking at the value.
74+
7275
### Set
7376
`Set` expects the key, value and ttl:
7477

0 commit comments

Comments
 (0)