Skip to content

Commit

Permalink
fix(ramcache): Change Item to unexported item
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bartventer committed Jun 16, 2024
1 parent b6e3938 commit 02d6fa7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions ramcache/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions ramcache/ramcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions ramcache/ramcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
18 changes: 9 additions & 9 deletions ramcache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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).
Expand Down
14 changes: 7 additions & 7 deletions ramcache/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 02d6fa7

Please sign in to comment.