From 63ea70e00b36743531e57a8be535730d5513c9ca Mon Sep 17 00:00:00 2001 From: vitams Date: Wed, 10 Apr 2019 23:50:35 +0500 Subject: [PATCH 1/3] Add method for delete by regex rule --- cache.go | 11 +++++++++++ cache_test.go | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cache.go b/cache.go index db88d2f..f602d5e 100644 --- a/cache.go +++ b/cache.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "regexp" "runtime" "sync" "time" @@ -911,6 +912,16 @@ func (c *cache) Delete(k string) { } } +// Delete an item from the cache by regex rule +func (c *cache) DeleteRegex(rule string) { + re, _ := regexp.Compile(rule) + for k := range c.items { + if re.MatchString(k) { + c.Delete(k) + } + } +} + func (c *cache) delete(k string) (interface{}, bool) { if c.onEvicted != nil { if v, found := c.items[k]; found { diff --git a/cache_test.go b/cache_test.go index cb80b38..5bf1930 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1150,6 +1150,19 @@ func TestDelete(t *testing.T) { } } +func TestDeleteRegex(t *testing.T) { + tc := New(DefaultExpiration, 0) + tc.Set("foo1", "bar", DefaultExpiration) + tc.DeleteRegex(`^foo`) + x, found := tc.Get("foo") + if found { + t.Error("foo was found, but it should have been deleted") + } + if x != nil { + t.Error("x is not nil:", x) + } +} + func TestItemCount(t *testing.T) { tc := New(DefaultExpiration, 0) tc.Set("foo", "1", DefaultExpiration) From 82dc7e9bca3da834fd21695144e5c26927e73209 Mon Sep 17 00:00:00 2001 From: vitams Date: Thu, 11 Apr 2019 00:03:31 +0500 Subject: [PATCH 2/3] Mod --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..92168b4 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/vmpartner/go-cache + +go 1.12 From 5b9bc4754ace56830719cc0c0037a11ad96d7a79 Mon Sep 17 00:00:00 2001 From: vitams Date: Mon, 7 Jun 2021 20:00:53 +0500 Subject: [PATCH 3/3] Mod --- cache.go | 8 ++++---- go.mod | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index f602d5e..5420e00 100644 --- a/cache.go +++ b/cache.go @@ -16,7 +16,7 @@ type Item struct { Expiration int64 } -// Returns true if the item has expired. +// Expired Returns true if the item has expired. func (item Item) Expired() bool { if item.Expiration == 0 { return false @@ -25,9 +25,9 @@ func (item Item) Expired() bool { } const ( - // For use with functions that take an expiration time. + // NoExpiration For use with functions that take an expiration time. NoExpiration time.Duration = -1 - // For use with functions that take an expiration time. Equivalent to + // DefaultExpiration For use with functions that take an expiration time. Equivalent to // passing in the same expiration duration as was given to New() or // NewFrom() when the cache was created (e.g. 5 minutes.) DefaultExpiration time.Duration = 0 @@ -46,7 +46,7 @@ type cache struct { janitor *janitor } -// Add an item to the cache, replacing any existing item. If the duration is 0 +// Set Add an item to the cache, replacing any 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) Set(k string, x interface{}, d time.Duration) { diff --git a/go.mod b/go.mod index 92168b4..8ba8c9d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/vmpartner/go-cache -go 1.12 +go 1.16