From 9174f96924600365adb64132f7bb9aaf13cc992e Mon Sep 17 00:00:00 2001 From: Alejandro Carstens Cattori Date: Sun, 19 Nov 2023 21:09:32 -0800 Subject: [PATCH 1/3] adding expire method similar to the EXPIRE method in Redis or Touch in Memcache --- .idea/.gitignore | 8 ++++++++ .idea/go-cache.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ cache.go | 15 +++++++++++++++ cache_test.go | 35 ++++++++++++++++++++++++++++++++++- 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/go-cache.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/go-cache.iml b/.idea/go-cache.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-cache.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e0724da --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cache.go b/cache.go index db88d2f..e1ccbc1 100644 --- a/cache.go +++ b/cache.go @@ -922,6 +922,21 @@ func (c *cache) delete(k string) (interface{}, bool) { return nil, false } +// Expire overrides the expiry tim of an existing item. If the duration is 0 +// (DefaultExpiration), the cache's default expiration time is used. If it is -1 +// (NoExpiration), the item never expires. +func (c *cache) Expire(k string, d time.Duration) error { + c.mu.Lock() + item, found := c.items[k] + if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) { + c.mu.Unlock() + return fmt.Errorf("Item %s doesn't exist", k) + } + c.set(k, item.Object, d) + c.mu.Unlock() + return nil +} + type keyAndValue struct { key string value interface{} diff --git a/cache_test.go b/cache_test.go index de3e9d6..539edc2 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1378,7 +1378,7 @@ func TestFileSerialization(t *testing.T) { tc := New(DefaultExpiration, 0) tc.Add("a", "a", DefaultExpiration) tc.Add("b", "b", DefaultExpiration) - f, err := ioutil.TempFile("", "go-cache-cache.dat") + f, err := ioutil.TempFile("", "go-cache-1-cache.dat") if err != nil { t.Fatal("Couldn't create cache file:", err) } @@ -1769,3 +1769,36 @@ func TestGetWithExpiration(t *testing.T) { t.Error("expiration for e is in the past") } } + +func TestExpire(t *testing.T) { + var ( + ct = time.Now() + tc = New(DefaultExpiration, 0) + ) + tc.Set("a", 1, time.Second) + if err := tc.Expire("a", time.Second); err != nil { + t.Error("expected err to be nil; value", err) + } + + _, expiration, found := tc.GetWithExpiration("a") + if !found { + t.Error("e was not found while getting e2") + } + if expiration.IsZero() { + t.Error("expected expiration not to be zero") + } + if d := expiration.Sub(ct); d <= time.Second { + t.Error("expected difference to be lesser than or equal to one second", d) + } + if err := tc.Expire("a", DefaultExpiration); err != nil { + t.Error("expected err to be nil; value", err) + } + + _, expiration, found = tc.GetWithExpiration("a") + if !found { + t.Error("e was not found while getting e2") + } + if !expiration.IsZero() { + t.Error("expected expiration to be zero") + } +} From 71c2c65211046c6c66833bf7e3d921e7e54a2643 Mon Sep 17 00:00:00 2001 From: Alejandro Carstens Cattori Date: Sun, 19 Nov 2023 21:11:32 -0800 Subject: [PATCH 2/3] Delete .idea/.gitignore --- .idea/.gitignore | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml From 76b65f46a9cb90ed5824acb790f4ae5b125311aa Mon Sep 17 00:00:00 2001 From: Alejandro Carstens Cattori Date: Sun, 19 Nov 2023 21:12:08 -0800 Subject: [PATCH 3/3] remove idea files. --- .idea/go-cache.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 3 files changed, 23 deletions(-) delete mode 100644 .idea/go-cache.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/go-cache.iml b/.idea/go-cache.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/go-cache.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index e0724da..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file