From 322ac4eb2babc5aa73d66a8f5f80dcb0d72942a3 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 10 Feb 2015 17:55:47 +0500 Subject: [PATCH 01/26] Add optional extending life of cache --- cache.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index dcee7fd..2a4da9c 100644 --- a/cache.go +++ b/cache.go @@ -22,15 +22,17 @@ func (cache *Cache) Set(key string, data string) { } // Get is a thread-safe way to lookup items -// Every lookup, also touches the item, hence extending it's life -func (cache *Cache) Get(key string) (data string, found bool) { +// Every lookup, if toch set to true, touches the item, hence extending it's life +func (cache *Cache) Get(key string, touch bool) (data string, found bool) { cache.mutex.Lock() item, exists := cache.items[key] if !exists || item.expired() { data = "" found = false } else { - item.touch(cache.ttl) + if touch { + item.touch(cache.ttl) + } data = item.data found = true } From 93d0853c4eb07911cf6be7a4714809f648220630 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 10 Feb 2015 23:10:19 +0600 Subject: [PATCH 02/26] =?UTF-8?q?Add=20=D0=B5=D1=89=20test=20optional=20ex?= =?UTF-8?q?tending=20life=20of=20cache.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cache_test.go b/cache_test.go index a49a906..4aeef2b 100644 --- a/cache_test.go +++ b/cache_test.go @@ -11,13 +11,13 @@ func TestGet(t *testing.T) { items: map[string]*Item{}, } - data, exists := cache.Get("hello") + data, exists := cache.Get("hello", true) if exists || data != "" { t.Errorf("Expected empty cache to return no data") } cache.Set("hello", "world") - data, exists = cache.Get("hello") + data, exists = cache.Get("hello", true) if !exists { t.Errorf("Expected cache to return data for `hello`") } From 8468f763f13e3c88165b41f6d5780555f4c6eb1a Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 10 Feb 2015 23:11:39 +0600 Subject: [PATCH 03/26] Update build status --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 25b88ad..e515adf 100644 --- a/Readme.md +++ b/Readme.md @@ -6,7 +6,7 @@ TTLCache is a minimal wrapper over a string map in golang, entries of which are 2. Auto-Expiring after a certain time 3. Auto-Extending expiration on `Get`s -[![Build Status](https://travis-ci.org/wunderlist/ttlcache.svg)](https://travis-ci.org/wunderlist/ttlcache) +[![Build Status](https://travis-ci.org/ikoroteev/ttlcache.svg)](https://travis-ci.org/ikoroteev/ttlcache) #### Usage ```go @@ -21,4 +21,4 @@ func main () { value, exists := cache.Get("key") count := cache.Count() } -``` \ No newline at end of file +``` From 0003c0e656afed615336a761af57807f603e50c8 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 10 Feb 2015 23:11:59 +0600 Subject: [PATCH 04/26] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e515adf..3de0bad 100644 --- a/Readme.md +++ b/Readme.md @@ -4,7 +4,7 @@ TTLCache is a minimal wrapper over a string map in golang, entries of which are 1. Thread-safe 2. Auto-Expiring after a certain time -3. Auto-Extending expiration on `Get`s +3. Managed auto-Extending expiration on `Get`s [![Build Status](https://travis-ci.org/ikoroteev/ttlcache.svg)](https://travis-ci.org/ikoroteev/ttlcache) From ecfe582f2b74f4c2ea4592a1533773fd7b1f8ce3 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 10 Feb 2015 23:13:52 +0600 Subject: [PATCH 05/26] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 3de0bad..2c78131 100644 --- a/Readme.md +++ b/Readme.md @@ -18,7 +18,7 @@ import ( func main () { cache := ttlcache.NewCache(time.Second) cache.Set("key", "value") - value, exists := cache.Get("key") + value, exists := cache.Get("key", true) count := cache.Count() } ``` From fafc9f84d24e90e146eae2945a0cd0cff21af9ce Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Wed, 11 Feb 2015 00:12:38 +0600 Subject: [PATCH 06/26] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b35391a..858fd34 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ git: install: - go install -race std - - go get code.google.com/p/go.tools/cmd/cover + - go get godoc.org/golang.org/x/tools/cmd/cover - go get github.com/golang/lint/golint - go get github.com/tools/godep - export PATH=$HOME/gopath/bin:$PATH From 7b8fd866918533fd03f0cdd12815d84d41b624b2 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Wed, 11 Feb 2015 00:32:04 +0600 Subject: [PATCH 07/26] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 858fd34..1e6da67 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ git: install: - go install -race std - - go get godoc.org/golang.org/x/tools/cmd/cover + - go get golang.org/x/tools/cover - go get github.com/golang/lint/golint - go get github.com/tools/godep - export PATH=$HOME/gopath/bin:$PATH From 5942cd6dfc362519873984f78f84183f694dfaef Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Wed, 11 Feb 2015 00:39:56 +0600 Subject: [PATCH 08/26] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1e6da67..ce40467 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ git: install: - go install -race std - - go get golang.org/x/tools/cover + - go get golang.org/x/tools/cmd/cover - go get github.com/golang/lint/golint - go get github.com/tools/godep - export PATH=$HOME/gopath/bin:$PATH From 1a0a68f0ec487d3925b913a889d9b22118109130 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Thu, 12 Feb 2015 01:27:02 +0600 Subject: [PATCH 09/26] Add ttl parameter to cache item. Delete common ttl for cache. Add cache for interface{} type --- Readme.md | 14 ++++++++------ cache.go | 24 ++++++++++++------------ cache_test.go | 12 +++++------- item.go | 7 ++++--- item_test.go | 4 ++-- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/Readme.md b/Readme.md index 2c78131..c7d9042 100644 --- a/Readme.md +++ b/Readme.md @@ -1,10 +1,10 @@ -## TTLCache - an in-memory LRU cache with expiration +## TTLCache - an in-memory cache with expiration -TTLCache is a minimal wrapper over a string map in golang, entries of which are +TTLCache is a minimal wrapper over a map of custom in golang, entries of which are 1. Thread-safe 2. Auto-Expiring after a certain time -3. Managed auto-Extending expiration on `Get`s +3. Managed auto-extending expiration on `Get`s [![Build Status](https://travis-ci.org/ikoroteev/ttlcache.svg)](https://travis-ci.org/ikoroteev/ttlcache) @@ -12,12 +12,14 @@ TTLCache is a minimal wrapper over a string map in golang, entries of which are ```go import ( "time" - "github.com/wunderlist/ttlcache" + "github.com/ikoroteev/ttlcache" ) func main () { - cache := ttlcache.NewCache(time.Second) - cache.Set("key", "value") + cache := ttlcache.NewCache() + cache.Set("key", "value", time.Second) + cache.Set("key1", 24, time.Duration(500) * time.Millisecond) + cache.Set("key3", time.Second, time.Second) value, exists := cache.Get("key", true) count := cache.Count() } diff --git a/cache.go b/cache.go index 2a4da9c..f425c30 100644 --- a/cache.go +++ b/cache.go @@ -8,22 +8,21 @@ import ( // Cache is a synchronised map of items that auto-expire once stale type Cache struct { mutex sync.RWMutex - ttl time.Duration items map[string]*Item } // Set is a thread-safe way to add new items to the map -func (cache *Cache) Set(key string, data string) { +func (cache *Cache) Set(key string, data interface{}, ttl time.Duration) { cache.mutex.Lock() - item := &Item{data: data} - item.touch(cache.ttl) + item := &Item{data: data, ttl: ttl} + item.touch() cache.items[key] = item cache.mutex.Unlock() } // Get is a thread-safe way to lookup items -// Every lookup, if toch set to true, touches the item, hence extending it's life -func (cache *Cache) Get(key string, touch bool) (data string, found bool) { +// Every lookup, if touch set to true, touches the item, hence extending it's life +func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { cache.mutex.Lock() item, exists := cache.items[key] if !exists || item.expired() { @@ -31,7 +30,7 @@ func (cache *Cache) Get(key string, touch bool) (data string, found bool) { found = false } else { if touch { - item.touch(cache.ttl) + item.touch() } data = item.data found = true @@ -60,9 +59,11 @@ func (cache *Cache) cleanup() { } func (cache *Cache) startCleanupTimer() { - duration := cache.ttl - if duration < time.Second { - duration = time.Second + duration := time.Second + for i := range cache.items { + if cache.items[i].ttl < time.Second { + duration = cache.items[i].ttl + } } ticker := time.Tick(duration) go (func() { @@ -76,9 +77,8 @@ func (cache *Cache) startCleanupTimer() { } // NewCache is a helper to create instance of the Cache struct -func NewCache(duration time.Duration) *Cache { +func NewCache() *Cache { cache := &Cache{ - ttl: duration, items: map[string]*Item{}, } cache.startCleanupTimer() diff --git a/cache_test.go b/cache_test.go index 4aeef2b..6825680 100644 --- a/cache_test.go +++ b/cache_test.go @@ -7,7 +7,6 @@ import ( func TestGet(t *testing.T) { cache := &Cache{ - ttl: time.Second, items: map[string]*Item{}, } @@ -16,7 +15,7 @@ func TestGet(t *testing.T) { t.Errorf("Expected empty cache to return no data") } - cache.Set("hello", "world") + cache.Set("hello", "world", time.Second) data, exists = cache.Get("hello", true) if !exists { t.Errorf("Expected cache to return data for `hello`") @@ -28,13 +27,12 @@ func TestGet(t *testing.T) { func TestExpiration(t *testing.T) { cache := &Cache{ - ttl: time.Second, items: map[string]*Item{}, } - cache.Set("x", "1") - cache.Set("y", "z") - cache.Set("z", "3") + cache.Set("x", "1", time.Second) + cache.Set("y", 123, time.Second) + cache.Set("z", time.Second, time.Second) cache.startCleanupTimer() count := cache.Count() @@ -44,7 +42,7 @@ func TestExpiration(t *testing.T) { <-time.After(500 * time.Millisecond) cache.mutex.Lock() - cache.items["y"].touch(time.Second) + cache.items["y"].touch() item, exists := cache.items["x"] cache.mutex.Unlock() if !exists || item.data != "1" || item.expired() { diff --git a/item.go b/item.go index 875ecaa..d44ac2c 100644 --- a/item.go +++ b/item.go @@ -8,13 +8,14 @@ import ( // Item represents a record in the cache map type Item struct { sync.RWMutex - data string + data interface{} expires *time.Time + ttl time.Duration } -func (item *Item) touch(duration time.Duration) { +func (item *Item) touch() { item.Lock() - expiration := time.Now().Add(duration) + expiration := time.Now().Add(item.ttl) item.expires = &expiration item.Unlock() } diff --git a/item_test.go b/item_test.go index ac2323d..61c316d 100644 --- a/item_test.go +++ b/item_test.go @@ -25,8 +25,8 @@ func TestExpired(t *testing.T) { } func TestTouch(t *testing.T) { - item := &Item{data: "blahblah"} - item.touch(time.Second) + item := &Item{data: "blahblah", ttl: time.Second} + item.touch() if item.expired() { t.Errorf("Expected item to not be expired once touched") } From d656abfe1ae0cdf991f20b76fb5617847a458eae Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Thu, 12 Feb 2015 01:49:55 +0600 Subject: [PATCH 10/26] fix documentation --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index c7d9042..b510bf4 100644 --- a/Readme.md +++ b/Readme.md @@ -20,7 +20,7 @@ func main () { cache.Set("key", "value", time.Second) cache.Set("key1", 24, time.Duration(500) * time.Millisecond) cache.Set("key3", time.Second, time.Second) - value, exists := cache.Get("key", true) + value, exists := cache.Get("key", true) // true - extend cache ttl, otherwise false count := cache.Count() } -``` +``` \ No newline at end of file From 09561154b2aae3bdfdab9f67d883a89086549e6a Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Fri, 13 Feb 2015 15:40:28 +0500 Subject: [PATCH 11/26] Update Godeps.json --- Godeps/Godeps.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 08af3e4..db67240 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,5 +1,5 @@ { - "ImportPath": "github.com/wunderlist/ttlcache", + "ImportPath": "github.com/ikoroteev/ttlcache", "GoVersion": "go1.2.2", "Deps": [] } From 33b77e6e129f53afa404c7d15913dcdfc7868d9d Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 19 Jun 2015 11:49:56 +0600 Subject: [PATCH 12/26] add cleanAll function --- cache.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cache.go b/cache.go index f425c30..f44202d 100644 --- a/cache.go +++ b/cache.go @@ -76,6 +76,14 @@ func (cache *Cache) startCleanupTimer() { })() } +func (cache *Cache) cleanAll() { + cache.mutex.Lock() + for key, item := range cache.items { + delete(cache.items, key) + } + cache.mutex.Unlock() +} + // NewCache is a helper to create instance of the Cache struct func NewCache() *Cache { cache := &Cache{ From cbfc32957cda8c6e5a51f62b570d50b3aaeac027 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 19 Jun 2015 12:01:16 +0600 Subject: [PATCH 13/26] fix --- cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache.go b/cache.go index f44202d..d34c2bd 100644 --- a/cache.go +++ b/cache.go @@ -78,7 +78,7 @@ func (cache *Cache) startCleanupTimer() { func (cache *Cache) cleanAll() { cache.mutex.Lock() - for key, item := range cache.items { + for key, _ := range cache.items { delete(cache.items, key) } cache.mutex.Unlock() From e1d6510a49d11345a056d3a12ea132b1e5ab7c22 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Mon, 22 Jun 2015 12:38:15 +0600 Subject: [PATCH 14/26] publish func --- cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache.go b/cache.go index d34c2bd..61f1b64 100644 --- a/cache.go +++ b/cache.go @@ -76,7 +76,7 @@ func (cache *Cache) startCleanupTimer() { })() } -func (cache *Cache) cleanAll() { +func (cache *Cache) CleanAll() { cache.mutex.Lock() for key, _ := range cache.items { delete(cache.items, key) From 4072ffb5a3aa18653076d43a4b579d22bb4f8828 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 26 Jun 2015 15:01:22 +0600 Subject: [PATCH 15/26] counter get cache queries --- cache.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cache.go b/cache.go index 61f1b64..0cf6265 100644 --- a/cache.go +++ b/cache.go @@ -9,6 +9,7 @@ import ( type Cache struct { mutex sync.RWMutex items map[string]*Item + getCounter uint64 } // Set is a thread-safe way to add new items to the map @@ -32,6 +33,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { if touch { item.touch() } + cache.getCounter += 1 data = item.data found = true } @@ -39,6 +41,10 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { return } +func (cache *Cache) getCounter() int64 { + return cache.getCounter +} + // Count returns the number of items in the cache // (helpful for tracking memory leaks) func (cache *Cache) Count() int { From e9495f56d70a0e66dbbcbdfa2fb5584ffa8a9376 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 26 Jun 2015 15:09:45 +0600 Subject: [PATCH 16/26] fix --- cache.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cache.go b/cache.go index 0cf6265..560992e 100644 --- a/cache.go +++ b/cache.go @@ -33,7 +33,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { if touch { item.touch() } - cache.getCounter += 1 + cache.getCounter ++ data = item.data found = true } @@ -41,7 +41,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { return } -func (cache *Cache) getCounter() int64 { +func (cache *Cache) getCounter() uint64 { return cache.getCounter } @@ -94,6 +94,7 @@ func (cache *Cache) CleanAll() { func NewCache() *Cache { cache := &Cache{ items: map[string]*Item{}, + } cache.startCleanupTimer() return cache From f6d769d6978073744f371b6b080f185b7ec76122 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 26 Jun 2015 15:24:37 +0600 Subject: [PATCH 17/26] fix --- .travis.yml | 2 +- cache.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index ce40467..6e1e61c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.2 + - 1.4 - release - tip diff --git a/cache.go b/cache.go index 560992e..f2b26bb 100644 --- a/cache.go +++ b/cache.go @@ -9,7 +9,7 @@ import ( type Cache struct { mutex sync.RWMutex items map[string]*Item - getCounter uint64 + counter uint64 } // Set is a thread-safe way to add new items to the map @@ -33,7 +33,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { if touch { item.touch() } - cache.getCounter ++ + cache.counter ++ data = item.data found = true } @@ -42,7 +42,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { } func (cache *Cache) getCounter() uint64 { - return cache.getCounter + return cache.counter } // Count returns the number of items in the cache From 1cb3af2acb01312f0653c2fe2118a348ff9b59c2 Mon Sep 17 00:00:00 2001 From: Aldonin Maksim Date: Fri, 26 Jun 2015 15:49:22 +0600 Subject: [PATCH 18/26] fix + add test --- cache.go | 2 +- cache_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cache.go b/cache.go index f2b26bb..13d4d21 100644 --- a/cache.go +++ b/cache.go @@ -41,7 +41,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { return } -func (cache *Cache) getCounter() uint64 { +func (cache *Cache) GetCounter() uint64 { return cache.counter } diff --git a/cache_test.go b/cache_test.go index 6825680..2f36af2 100644 --- a/cache_test.go +++ b/cache_test.go @@ -23,6 +23,9 @@ func TestGet(t *testing.T) { if data != "world" { t.Errorf("Expected cache to return `world` for `hello`") } + if cache.GetCounter() != 1 { + t.Errorf("Expected cache get counter is equal to 1") + } } func TestExpiration(t *testing.T) { From 1aae39ef32a9c685247d2397fa08f8060079490c Mon Sep 17 00:00:00 2001 From: Koroteev Igor Date: Thu, 17 Sep 2015 18:06:12 +0600 Subject: [PATCH 19/26] Add delete method for cache --- cache.go | 17 +++++++++++------ cache_test.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cache.go b/cache.go index 13d4d21..f43250f 100644 --- a/cache.go +++ b/cache.go @@ -7,9 +7,9 @@ import ( // Cache is a synchronised map of items that auto-expire once stale type Cache struct { - mutex sync.RWMutex - items map[string]*Item - counter uint64 + mutex sync.RWMutex + items map[string]*Item + counter uint64 } // Set is a thread-safe way to add new items to the map @@ -33,7 +33,7 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { if touch { item.touch() } - cache.counter ++ + cache.counter++ data = item.data found = true } @@ -85,16 +85,21 @@ func (cache *Cache) startCleanupTimer() { func (cache *Cache) CleanAll() { cache.mutex.Lock() for key, _ := range cache.items { - delete(cache.items, key) + delete(cache.items, key) } cache.mutex.Unlock() } +func (cache *Cache) Delete(key string) { + cache.mutex.Lock() + delete(cache.items, key) + cache.mutex.Unlock() +} + // NewCache is a helper to create instance of the Cache struct func NewCache() *Cache { cache := &Cache{ items: map[string]*Item{}, - } cache.startCleanupTimer() return cache diff --git a/cache_test.go b/cache_test.go index 2f36af2..44d0bb7 100644 --- a/cache_test.go +++ b/cache_test.go @@ -28,6 +28,23 @@ func TestGet(t *testing.T) { } } +func testDelete(t *testing.T) { + cache := &Cache{ + items: map[string]*Item{}, + } + cache.Set("Test", "Delete", time.Second) + _, exists := cache.Get("Test", true) + if !exists { + t.Errorf("Expected cache to return data for `Test`") + } + cache.Delete("Test") + _, exists = cache.Get("Test", true) + if exists { + t.Errorf("Expected cache to delete data for `Test`") + } + +} + func TestExpiration(t *testing.T) { cache := &Cache{ items: map[string]*Item{}, From 8f0dc5311aed804f0b3e9200c9817af60d584b4a Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Thu, 26 Jan 2017 13:53:04 +0700 Subject: [PATCH 20/26] Fix test name for auto start --- cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache_test.go b/cache_test.go index 44d0bb7..e533873 100644 --- a/cache_test.go +++ b/cache_test.go @@ -28,7 +28,7 @@ func TestGet(t *testing.T) { } } -func testDelete(t *testing.T) { +func TestDelete(t *testing.T) { cache := &Cache{ items: map[string]*Item{}, } From 051cefcbfac04ec293988387bb41a4c2c8c05731 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Sat, 11 Feb 2017 00:10:32 +0700 Subject: [PATCH 21/26] update go version in travis to 1.7 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6e1e61c..90482e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.4 + - 1.7 - release - tip From e6b8810f74e66df5a331bec2a3ae79f1d119f1fb Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Sat, 11 Feb 2017 00:17:26 +0700 Subject: [PATCH 22/26] fix travis config --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 90482e2..6bde055 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,6 @@ language: go go: - 1.7 - - release - - tip git: depth: 1 From 2ac460b912c7de39451a2f34020846591571ab29 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Fri, 26 May 2017 20:40:00 +0700 Subject: [PATCH 23/26] delete unused Godeps --- Godeps/Godeps.json | 5 ----- Godeps/_workspace/.gitignore | 2 -- 2 files changed, 7 deletions(-) delete mode 100644 Godeps/Godeps.json delete mode 100644 Godeps/_workspace/.gitignore diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json deleted file mode 100644 index db67240..0000000 --- a/Godeps/Godeps.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ImportPath": "github.com/ikoroteev/ttlcache", - "GoVersion": "go1.2.2", - "Deps": [] -} diff --git a/Godeps/_workspace/.gitignore b/Godeps/_workspace/.gitignore deleted file mode 100644 index f037d68..0000000 --- a/Godeps/_workspace/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/pkg -/bin From 7a3a3c1c0a76960aa09e879a865096e9e46a447d Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Fri, 26 May 2017 20:44:31 +0700 Subject: [PATCH 24/26] remove godeps usage --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6bde055..569cdff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,5 +15,5 @@ install: script: - golint . - - godep go test -race ./... - - godep go test -cover ./... + - go test -race ./... + - go test -cover ./... From 9a1ecd17fb47ec8631a621eb8a0f2d0490df24b6 Mon Sep 17 00:00:00 2001 From: ikoroteev Date: Tue, 20 Feb 2018 14:18:55 +0700 Subject: [PATCH 25/26] Update .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 569cdff..616e9de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ install: - go install -race std - go get golang.org/x/tools/cmd/cover - go get github.com/golang/lint/golint - - go get github.com/tools/godep - export PATH=$HOME/gopath/bin:$PATH script: From e0bd42bc51a8e3cd62e2d2fbbc104c7f0fe8952e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=9A=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D1=82=D0=B5=D0=B5=D0=B2?= Date: Wed, 28 Feb 2018 00:36:10 +0700 Subject: [PATCH 26/26] Move unlock to defer --- cache.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index f43250f..784751c 100644 --- a/cache.go +++ b/cache.go @@ -15,16 +15,17 @@ type Cache struct { // Set is a thread-safe way to add new items to the map func (cache *Cache) Set(key string, data interface{}, ttl time.Duration) { cache.mutex.Lock() + defer cache.mutex.Unlock() item := &Item{data: data, ttl: ttl} item.touch() cache.items[key] = item - cache.mutex.Unlock() } // Get is a thread-safe way to lookup items // Every lookup, if touch set to true, touches the item, hence extending it's life func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { cache.mutex.Lock() + defer cache.mutex.Unlock() item, exists := cache.items[key] if !exists || item.expired() { data = "" @@ -37,10 +38,10 @@ func (cache *Cache) Get(key string, touch bool) (data interface{}, found bool) { data = item.data found = true } - cache.mutex.Unlock() return } +//GetCounter return cache hit count func (cache *Cache) GetCounter() uint64 { return cache.counter } @@ -56,12 +57,12 @@ func (cache *Cache) Count() int { func (cache *Cache) cleanup() { cache.mutex.Lock() + defer cache.mutex.Unlock() for key, item := range cache.items { if item.expired() { delete(cache.items, key) } } - cache.mutex.Unlock() } func (cache *Cache) startCleanupTimer() { @@ -82,14 +83,16 @@ func (cache *Cache) startCleanupTimer() { })() } +//CleanAll delete all data from cache func (cache *Cache) CleanAll() { cache.mutex.Lock() - for key, _ := range cache.items { + defer cache.mutex.Unlock() + for key := range cache.items { delete(cache.items, key) } - cache.mutex.Unlock() } +//Delete cache item by key func (cache *Cache) Delete(key string) { cache.mutex.Lock() delete(cache.items, key)