mirror of https://github.com/helm/helm
Signed-off-by: Yarden Shoham <git@yardenshoham.com>pull/12167/head
parent
4f2f5b5018
commit
892354cda0
@ -0,0 +1,57 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cache interface defines the methods for a cache
|
||||||
|
type Cache[V any] interface {
|
||||||
|
// Set adds an item to the cache
|
||||||
|
Set(key string, value V)
|
||||||
|
|
||||||
|
// Get retrieves an item from the cache
|
||||||
|
// The boolean return value indicates whether the key was found
|
||||||
|
Get(key string) (V, bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoOpCache implements Cache interface with no-op operations
|
||||||
|
type NoOpCache[V any] struct{}
|
||||||
|
|
||||||
|
func NewNoOpCache[V any]() *NoOpCache[V] {
|
||||||
|
return &NoOpCache[V]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *NoOpCache[V]) Set(key string, value V) {}
|
||||||
|
|
||||||
|
func (c *NoOpCache[V]) Get(key string) (V, bool) {
|
||||||
|
var zero V
|
||||||
|
return zero, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConcurrentMapCache implements Cache interface using a concurrent map
|
||||||
|
type ConcurrentMapCache[V any] struct {
|
||||||
|
items map[string]V
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConcurrentMapCache[V any]() *ConcurrentMapCache[V] {
|
||||||
|
return &ConcurrentMapCache[V]{
|
||||||
|
items: make(map[string]V),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConcurrentMapCache[V]) Set(key string, value V) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
c.items[key] = value
|
||||||
|
fmt.Println("set", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConcurrentMapCache[V]) Get(key string) (V, bool) {
|
||||||
|
c.mu.RLock()
|
||||||
|
defer c.mu.RUnlock()
|
||||||
|
value, exists := c.items[key]
|
||||||
|
fmt.Println("get", key, "exists", exists)
|
||||||
|
return value, exists
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNoOpCache(t *testing.T) {
|
||||||
|
cache := NewNoOpCache[string]()
|
||||||
|
|
||||||
|
t.Run("Set", func(t *testing.T) {
|
||||||
|
cache.Set("key", "value")
|
||||||
|
// No assertion needed as it's a no-op
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Get", func(t *testing.T) {
|
||||||
|
value, exists := cache.Get("key")
|
||||||
|
assert.False(t, exists)
|
||||||
|
assert.Empty(t, value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMapCache(t *testing.T) {
|
||||||
|
cache := NewConcurrentMapCache[int]()
|
||||||
|
|
||||||
|
t.Run("Set and Get", func(t *testing.T) {
|
||||||
|
cache.Set("key1", 42)
|
||||||
|
cache.Set("key2", 84)
|
||||||
|
|
||||||
|
value1, exists1 := cache.Get("key1")
|
||||||
|
assert.True(t, exists1)
|
||||||
|
assert.Equal(t, 42, value1)
|
||||||
|
|
||||||
|
value2, exists2 := cache.Get("key2")
|
||||||
|
assert.True(t, exists2)
|
||||||
|
assert.Equal(t, 84, value2)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Get non-existent key", func(t *testing.T) {
|
||||||
|
value, exists := cache.Get("non-existent")
|
||||||
|
assert.False(t, exists)
|
||||||
|
assert.Zero(t, value)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Overwrite existing key", func(t *testing.T) {
|
||||||
|
cache.Set("key1", 100)
|
||||||
|
value, exists := cache.Get("key1")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, 100, value)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in new issue