Merge branch 'main' into issue_494_http_scanning_error

main
Toussaint Louis 2021-06-04 15:49:22 +02:00 committed by GitHub
commit b63f723296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -49,6 +49,10 @@ func (c *LRUCache) Put(key string, value interface{}) bool {
c.mu.Lock()
defer c.mu.Unlock()
if c.cap == 0 {
return false
}
// if the key already exists, move to front and update the value
if node, ok := c.m[key]; ok {
c.l.MoveToFront(node)

View File

@ -69,4 +69,31 @@ func TestCache(t *testing.T) {
}
assert.Equal(t, 5, cache.Len())
})
t.Run("should ignore keys when capacity is 0", func(t *testing.T) {
keys := []struct {
key string
value interface{}
}{
{
"test",
[]string{"slice"},
},
{
"test",
[]string{},
},
{
"test2",
[]resource.FakeResource{},
},
}
cache := New(0)
for _, k := range keys {
assert.Equal(t, false, cache.Put(k.key, k.value))
assert.Equal(t, nil, cache.Get(k.key))
}
assert.Equal(t, 0, cache.Len())
})
}