From 26fb4f10432047e92f13ebf1e9718a0ffb2119a8 Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Mon, 29 Aug 2016 22:42:41 +0530 Subject: [PATCH 1/6] check locally with exporting and using shraded cache --- sharded.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharded.go b/sharded.go index bcc0538..7df0943 100644 --- a/sharded.go +++ b/sharded.go @@ -178,7 +178,7 @@ func newShardedCache(n int, de time.Duration) *shardedCache { return sc } -func unexportedNewSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { +func NewSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { if defaultExpiration == 0 { defaultExpiration = -1 } From 86164a864c8210850a7ac7497e6f9c6cd79e349e Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Tue, 30 Aug 2016 01:05:50 +0530 Subject: [PATCH 2/6] cache test and Update added --- cache.go | 16 ++++++++++++++++ sharded_test.go | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index 3562543..f214480 100644 --- a/cache.go +++ b/cache.go @@ -109,6 +109,22 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error { return nil } +// Update only if it is already existing, keep same ttl +// Set new expiry time which is now - its expiration +func (c *cache) Update(k string, x interface{}) error { + c.mu.Lock() + _, found := c.get(k) + if !found { + c.mu.Unlock() + return fmt.Errorf("Item %s doesn't exist", k) + } + newTtl := time.Unix(0, c.items[k].Expiration) + c.set(k, x, newTtl.Sub(time.Now())) + c.mu.Unlock() + return nil +} + + // Get an item from the cache. Returns the item or nil, and a bool indicating // whether the key was found. func (c *cache) Get(k string) (interface{}, bool) { diff --git a/sharded_test.go b/sharded_test.go index aef8597..384ed59 100644 --- a/sharded_test.go +++ b/sharded_test.go @@ -27,7 +27,7 @@ var shardedKeys = []string{ } func TestShardedCache(t *testing.T) { - tc := unexportedNewSharded(DefaultExpiration, 0, 13) + tc := NewSharded(DefaultExpiration, 0, 13) for _, v := range shardedKeys { tc.Set(v, "value", DefaultExpiration) } @@ -43,7 +43,7 @@ func BenchmarkShardedCacheGetNotExpiring(b *testing.B) { func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) { b.StopTimer() - tc := unexportedNewSharded(exp, 0, 10) + tc := NewSharded(exp, 0, 10) tc.Set("foobarba", "zquux", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -62,7 +62,7 @@ func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) { func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) { b.StopTimer() n := 10000 - tsc := unexportedNewSharded(exp, 0, 20) + tsc := NewSharded(exp, 0, 20) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) From b205f45f519119bc08be63ef400e86732ad768b8 Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Tue, 13 Sep 2016 13:44:46 +0530 Subject: [PATCH 3/6] make sharded cache unexported back again --- sharded.go | 2 +- sharded_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sharded.go b/sharded.go index 7df0943..04f59a9 100644 --- a/sharded.go +++ b/sharded.go @@ -178,7 +178,7 @@ func newShardedCache(n int, de time.Duration) *shardedCache { return sc } -func NewSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { +func newSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { if defaultExpiration == 0 { defaultExpiration = -1 } diff --git a/sharded_test.go b/sharded_test.go index 384ed59..d1c34fc 100644 --- a/sharded_test.go +++ b/sharded_test.go @@ -27,7 +27,7 @@ var shardedKeys = []string{ } func TestShardedCache(t *testing.T) { - tc := NewSharded(DefaultExpiration, 0, 13) + tc := newSharded(DefaultExpiration, 0, 13) for _, v := range shardedKeys { tc.Set(v, "value", DefaultExpiration) } @@ -43,7 +43,7 @@ func BenchmarkShardedCacheGetNotExpiring(b *testing.B) { func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) { b.StopTimer() - tc := NewSharded(exp, 0, 10) + tc := newSharded(exp, 0, 10) tc.Set("foobarba", "zquux", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -62,7 +62,7 @@ func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) { func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) { b.StopTimer() n := 10000 - tsc := NewSharded(exp, 0, 20) + tsc := newSharded(exp, 0, 20) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) From 1b4adb128aecb252d1006c7a6392d8568ab35f68 Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Tue, 13 Sep 2016 14:36:28 +0530 Subject: [PATCH 4/6] make back to unexported:typo --- sharded.go | 2 +- sharded_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sharded.go b/sharded.go index 04f59a9..bcc0538 100644 --- a/sharded.go +++ b/sharded.go @@ -178,7 +178,7 @@ func newShardedCache(n int, de time.Duration) *shardedCache { return sc } -func newSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { +func unexportedNewSharded(defaultExpiration, cleanupInterval time.Duration, shards int) *unexportedShardedCache { if defaultExpiration == 0 { defaultExpiration = -1 } diff --git a/sharded_test.go b/sharded_test.go index d1c34fc..aef8597 100644 --- a/sharded_test.go +++ b/sharded_test.go @@ -27,7 +27,7 @@ var shardedKeys = []string{ } func TestShardedCache(t *testing.T) { - tc := newSharded(DefaultExpiration, 0, 13) + tc := unexportedNewSharded(DefaultExpiration, 0, 13) for _, v := range shardedKeys { tc.Set(v, "value", DefaultExpiration) } @@ -43,7 +43,7 @@ func BenchmarkShardedCacheGetNotExpiring(b *testing.B) { func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) { b.StopTimer() - tc := newSharded(exp, 0, 10) + tc := unexportedNewSharded(exp, 0, 10) tc.Set("foobarba", "zquux", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -62,7 +62,7 @@ func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) { func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) { b.StopTimer() n := 10000 - tsc := newSharded(exp, 0, 20) + tsc := unexportedNewSharded(exp, 0, 20) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) From 738ddeb7e2138a19f59a7a58b8a4877c1832a294 Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Tue, 13 Sep 2016 15:22:42 +0530 Subject: [PATCH 5/6] TestUpdate check Update expiration time --- cache_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cache_test.go b/cache_test.go index 6e81693..3246e08 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1137,6 +1137,25 @@ func TestReplace(t *testing.T) { } } +func TestUpdate(t *testing.T) { + tc := New(75*time.Millisecond, 1*time.Millisecond) + tc.Set("a", 1, DefaultExpiration) + + <-time.After(50 * time.Millisecond) + _, found := tc.Get("a") + if !found { + t.Error("error in update; didnt find value that was expected") + } + + tc.Update("a", 2) + <-time.After(26 * time.Millisecond) + + a, found := tc.Get("a") + if found || a != nil { + t.Error("Getting A found value that shouldn't exist:", a) + } +} + func TestDelete(t *testing.T) { tc := New(DefaultExpiration, 0) tc.Set("foo", "bar", DefaultExpiration) From 71bfe9e7093f718514cd9ed7fccbbd096bdbade8 Mon Sep 17 00:00:00 2001 From: Pranjal Pandit Date: Thu, 15 Sep 2016 18:47:54 +0530 Subject: [PATCH 6/6] cache changes for evictBulk --- cache.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/cache.go b/cache.go index f214480..512e61d 100644 --- a/cache.go +++ b/cache.go @@ -42,6 +42,7 @@ type cache struct { items map[string]Item mu sync.RWMutex onEvicted func(string, interface{}) + onEvictedBulk func([]KeyAndValue) janitor *janitor } @@ -902,14 +903,14 @@ func (c *cache) delete(k string) (interface{}, bool) { return nil, false } -type keyAndValue struct { - key string - value interface{} +type KeyAndValue struct { + Key string + Value interface{} } // Delete all expired items from the cache. func (c *cache) DeleteExpired() { - var evictedItems []keyAndValue + var evictedItems []KeyAndValue now := time.Now().UnixNano() c.mu.Lock() for k, v := range c.items { @@ -917,13 +918,20 @@ func (c *cache) DeleteExpired() { if v.Expiration > 0 && now > v.Expiration { ov, evicted := c.delete(k) if evicted { - evictedItems = append(evictedItems, keyAndValue{k, ov}) + evictedItems = append(evictedItems, KeyAndValue{k, ov}) } } } c.mu.Unlock() - for _, v := range evictedItems { - c.onEvicted(v.key, v.value) + // Call evict functions only when + // their respective functions exist. + if c.onEvictedBulk != nil{ + c.onEvictedBulk(evictedItems) + }else if c.onEvicted != nil{ + for _, v := range evictedItems { + c.onEvicted(v.Key, v.Value) + } + }else{ } } @@ -936,6 +944,15 @@ func (c *cache) OnEvicted(f func(string, interface{})) { c.mu.Unlock() } +// Set an optional OnEvictedBulk which will be used to evict keys in bulk +// This is used when eviction happens in bulk when expiry is checked at +// regular intervals, we dont want to call onEvict multiple times +func (c *cache) OnEvictedBulk(f func([]KeyAndValue)) { + c.mu.Lock() + c.onEvictedBulk = f + c.mu.Unlock() +} + // Write the cache's items (using Gob) to an io.Writer. // // NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the