Skip to content

Commit

Permalink
do not support nil key/value (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
prpeh committed Jun 5, 2024
1 parent 064c9c2 commit a741d36
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ringcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (c *RingCache) Purge() {

// Add adds a value to the cache. Returns true if an eviction occurred.
func (c *RingCache) Add(key, value interface{}) (evicted bool) {
// Do nothing if key or value is nil
if key == nil || value == nil {
return false
}
// Check for existing item
evicted = false
k := c.keys[c.next]
Expand Down
7 changes: 1 addition & 6 deletions ringcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestCircularCache(t *testing.T) {
evicted := cache.Add(i, i)
require.False(t, evicted)
}
cache.Add(nil, nil)

// adding same key on the same position but no evict because the evict is nil
require.True(t, cache.Contains(1))
Expand Down Expand Up @@ -55,12 +56,6 @@ func TestCircularCache(t *testing.T) {

cache.Add(12, 12)
require.Equal(t, 1, cache.Len())

// Add/Get nil value
require.False(t, cache.Add(12, nil))
val, ok = cache.Get(12)
require.Nil(t, val)
require.False(t, ok)
}

func TestCircularCacheWithCallback(t *testing.T) {
Expand Down

0 comments on commit a741d36

Please sign in to comment.