Skip to content

[FSSDK-11158] update: add remove method in LRU Cache for CMAB service #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

FarhanAnjum-opti
Copy link
Contributor

Summary

Added remove method in LRU cache class

Test plan

Added remove method in LRU cache class

Issues

FSSDK-11158

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a remove method to the LRUCache class and includes corresponding tests to ensure it works under various conditions.

  • Implement remove in lib/optimizely/odp/lru_cache.rb
  • Add specs covering removal of existing keys, non-existent keys, zero-sized caches, re-adding keys, and thread safety in spec/odp/lru_cache_spec.rb

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/optimizely/odp/lru_cache.rb Added remove method to delete cache entries
spec/odp/lru_cache_spec.rb New tests for remove behavior under multiple scenarios

return if @capacity <= 0

@cache_mutex.synchronize do
@map.delete(key)
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remove method deletes the entry from the map but does not unlink the corresponding node from the internal doubly linked list, which can lead to stale nodes and incorrect eviction behavior. Consider retrieving the node before deletion and performing proper list unlinking (updating prev and next pointers) within the synchronized block.

Suggested change
@map.delete(key)
element = @map.delete(key)
return nil unless element
# Unlink the node from the doubly linked list
if element.prev
element.prev.next = element.next
end
if element.next
element.next.prev = element.prev
end

Copilot uses AI. Check for mistakes.

end
end

expect(cache.instance_variable_get('@map').size).to eq(max_size / 2)
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The test inspects the private @map instance variable directly. It’s more maintainable to expose a public method (e.g., size or count) on LRUCache for checking the number of entries, rather than relying on internal state.

Suggested change
expect(cache.instance_variable_get('@map').size).to eq(max_size / 2)
expect(cache.size).to eq(max_size / 2)

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant