Merge branch 'main' into issue_494_http_scanning_error
commit
b63f723296
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue