Skip to content

Commit 4505626

Browse files
committed
Fix LRUCache#store of existing key on max_size
1 parent 82cd2a9 commit 4505626

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/datadog/appsec/api_security/lru_cache.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def [](key)
3131
end
3232

3333
def store(key, value)
34+
return @store[key] = value if @store.delete(key)
35+
3436
# NOTE: evict the oldest entry if store reached the maximum allowed size
3537
@store.shift if @store.size >= @max_size
3638
@store[key] = value

spec/datadog/appsec/api_security/lru_cache_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@
5858
.from('old-value').to('new-value')
5959
end
6060

61+
context 'when cache is full and an existing key is updated' do
62+
it 'does not remove other entries' do
63+
lru_cache.store(:key2, 'value2')
64+
lru_cache.store(:key3, 'value3')
65+
lru_cache.store(:key1, 'value1')
66+
67+
lru_cache.store(:key1, 'new-value1')
68+
lru_cache.store(:key3, 'new-value3')
69+
lru_cache.store(:key2, 'new-value2')
70+
71+
aggregate_failures 'verifying LRU cache contents' do
72+
expect(lru_cache[:key1]).to eq('new-value1')
73+
expect(lru_cache[:key2]).to eq('new-value2')
74+
expect(lru_cache[:key3]).to eq('new-value3')
75+
end
76+
end
77+
end
78+
6179
context 'when maximum size is reached' do
6280
it 'evicts the least recently used item' do
6381
lru_cache.store(:key1, 'value1')

0 commit comments

Comments
 (0)