From 02d6fa7f895e1e2f4793f6144fdb76a5c63d540c Mon Sep 17 00:00:00 2001 From: Bart Venter Date: Sun, 16 Jun 2024 13:35:19 +0000 Subject: [PATCH] fix(ramcache): Change `Item` to unexported `item` The `Item` struct has been changed to an unexported `item` struct. NOTE: Versions 1.4.2-1.6.0 of the `ramcache` package have been retracted. Please update to this version to avoid any issues. --- ramcache/go.mod | 4 ++++ ramcache/ramcache.go | 8 ++++---- ramcache/ramcache_test.go | 4 ++-- ramcache/store.go | 18 +++++++++--------- ramcache/store_test.go | 14 +++++++------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/ramcache/go.mod b/ramcache/go.mod index a7803b4..0a8d562 100644 --- a/ramcache/go.mod +++ b/ramcache/go.mod @@ -17,3 +17,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +retract ( + [v1.4.2, v1.6.0] // Accidentally published non-public API +) diff --git a/ramcache/ramcache.go b/ramcache/ramcache.go index c22b8a5..70d26c1 100644 --- a/ramcache/ramcache.go +++ b/ramcache/ramcache.go @@ -224,9 +224,9 @@ func (r *ramcache) Set(ctx context.Context, key string, value interface{}, modif key = keymod.ModifyKey(key, modifiers...) switch v := value.(type) { case string: - r.store.Set(key, Item{Value: []byte(v), Expiry: time.Now().Add(r.opts.DefaultTTL)}) + r.store.Set(key, item{Value: []byte(v), Expiry: time.Now().Add(r.opts.DefaultTTL)}) case []byte: - r.store.Set(key, Item{Value: v, Expiry: time.Now().Add(r.opts.DefaultTTL)}) + r.store.Set(key, item{Value: v, Expiry: time.Now().Add(r.opts.DefaultTTL)}) default: return gcerrors.NewWithScheme(Scheme, fmt.Errorf("unsupported value type: %T", v)) } @@ -238,9 +238,9 @@ func (r *ramcache) SetWithExpiry(ctx context.Context, key string, value interfac key = keymod.ModifyKey(key, modifiers...) switch v := value.(type) { case string: - r.store.Set(key, Item{Value: []byte(v), Expiry: time.Now().Add(expiry)}) + r.store.Set(key, item{Value: []byte(v), Expiry: time.Now().Add(expiry)}) case []byte: - r.store.Set(key, Item{Value: v, Expiry: time.Now().Add(expiry)}) + r.store.Set(key, item{Value: v, Expiry: time.Now().Add(expiry)}) default: return gcerrors.NewWithScheme(Scheme, fmt.Errorf("unsupported value type: %T", v)) } diff --git a/ramcache/ramcache_test.go b/ramcache/ramcache_test.go index 57bcd7c..f2aa0a5 100644 --- a/ramcache/ramcache_test.go +++ b/ramcache/ramcache_test.go @@ -40,14 +40,14 @@ func Test_ramcache_removeExpiredItems(t *testing.T) { // Add an expired item expiredKey := "expired" - r.store.Set(expiredKey, Item{ + r.store.Set(expiredKey, item{ Value: []byte("expired"), Expiry: time.Now().Add(-time.Hour), // 1 hour in the past }) // Add a non-expired item nonExpiredKey := "nonExpired" - r.store.Set(nonExpiredKey, Item{ + r.store.Set(nonExpiredKey, item{ Value: []byte("nonExpired"), Expiry: time.Now().Add(time.Hour), // 1 hour in the future }) diff --git a/ramcache/store.go b/ramcache/store.go index c71c2d4..fb04a82 100644 --- a/ramcache/store.go +++ b/ramcache/store.go @@ -6,38 +6,38 @@ import ( "time" ) -// Item is a cache Item. -type Item struct { +// item is a cache item. +type item struct { Value []byte // Value is the item value. Expiry time.Time // Expiry is the item expiry time. Default is 24 hours. } // IsExpired returns true if the item is expired. -func (i Item) IsExpired() bool { +func (i item) IsExpired() bool { return time.Now().After(i.Expiry) } // store is an in-memory store for cache items. type store struct { mu sync.RWMutex - items map[string]Item + items map[string]item } // newStore creates a new store. func newStore() *store { return &store{ - items: make(map[string]Item), + items: make(map[string]item), } } -func (s *store) Get(key string) (Item, bool) { +func (s *store) Get(key string) (item, bool) { s.mu.RLock() defer s.mu.RUnlock() item, exists := s.items[key] return item, exists } -func (s *store) Set(key string, item Item) { +func (s *store) Set(key string, item item) { s.mu.Lock() s.items[key] = item s.mu.Unlock() @@ -51,14 +51,14 @@ func (s *store) Delete(key string) { func (s *store) Clear() { s.mu.Lock() - s.items = make(map[string]Item) + s.items = make(map[string]item) s.mu.Unlock() } // keyItem is a struct that contains a key and an item. type keyItem struct { Key string // Key is the item key. - Item Item // Item is the item. + Item item // Item is the item. } // KeyItemsSortedByExpiry returns all key items sorted by expiry time (closest to expiry first). diff --git a/ramcache/store_test.go b/ramcache/store_test.go index 386d612..c250ba0 100644 --- a/ramcache/store_test.go +++ b/ramcache/store_test.go @@ -8,7 +8,7 @@ import ( func TestStore(t *testing.T) { t.Run("Set", func(t *testing.T) { s := newStore() - s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) + s.Set("key1", item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) item, exists := s.Get("key1") if !exists || string(item.Value) != "value1" { t.Errorf("Set failed. Expected value1, got %v", string(item.Value)) @@ -17,7 +17,7 @@ func TestStore(t *testing.T) { t.Run("Get", func(t *testing.T) { s := newStore() - s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) + s.Set("key1", item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) item, exists := s.Get("key1") if !exists || string(item.Value) != "value1" { t.Errorf("Get failed. Expected value1, got %v", string(item.Value)) @@ -26,7 +26,7 @@ func TestStore(t *testing.T) { t.Run("Delete", func(t *testing.T) { s := newStore() - s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) + s.Set("key1", item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) s.Delete("key1") _, exists := s.Get("key1") if exists { @@ -36,8 +36,8 @@ func TestStore(t *testing.T) { t.Run("Clear", func(t *testing.T) { s := newStore() - s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) - s.Set("key2", Item{Value: []byte("value2"), Expiry: time.Now().Add(20 * time.Minute)}) + s.Set("key1", item{Value: []byte("value1"), Expiry: time.Now().Add(10 * time.Minute)}) + s.Set("key2", item{Value: []byte("value2"), Expiry: time.Now().Add(20 * time.Minute)}) s.Clear() _, exists1 := s.Get("key1") _, exists2 := s.Get("key2") @@ -48,8 +48,8 @@ func TestStore(t *testing.T) { t.Run("KeyItemsSortedByExpiry", func(t *testing.T) { s := newStore() - s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(20 * time.Minute)}) - s.Set("key2", Item{Value: []byte("value2"), Expiry: time.Now().Add(10 * time.Minute)}) + s.Set("key1", item{Value: []byte("value1"), Expiry: time.Now().Add(20 * time.Minute)}) + s.Set("key2", item{Value: []byte("value2"), Expiry: time.Now().Add(10 * time.Minute)}) items := s.KeyItemsSortedByExpiry() if len(items) != 2 || items[0].Key != "key2" || items[1].Key != "key1" { t.Errorf("KeyItemsSortedByExpiry failed. Expected [key2, key1], got [%v, %v]", items[0].Key, items[1].Key)