Class: Tins::LRUCache
- Includes:
- Enumerable
- Defined in:
- lib/tins/lru_cache.rb
Overview
An LRU (Least Recently Used) cache implementation.
This cache maintains a fixed-size collection of key-value pairs, automatically removing the least recently accessed item when the capacity is exceeded.
Constant Summary collapse
- NOT_EXIST =
Our “nil” value if it wasn’ set by clients
Object.new.freeze
Instance Attribute Summary collapse
-
#capacity ⇒ Integer
readonly
Returns the maximum capacity of the cache.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Retrieves the value associated with the given key.
-
#[]=(key, value) ⇒ Object
Associates a value with a key in the cache.
-
#clear ⇒ Tins::LRUCache
Removes all items from the cache.
-
#delete(key) ⇒ Object?
Removes and returns the value associated with the given key.
-
#each {|key, value| ... } ⇒ Enumerator
Iterates over all key-value pairs in the cache.
-
#initialize(capacity) ⇒ LRUCache
constructor
Initializes a new LRU cache with the specified capacity.
-
#size ⇒ Integer
Returns the number of items currently in the cache.
Constructor Details
#initialize(capacity) ⇒ LRUCache
Initializes a new LRU cache with the specified capacity.
18 19 20 21 22 23 |
# File 'lib/tins/lru_cache.rb', line 18 def initialize(capacity) @capacity = Integer(capacity) @capacity >= 1 or raise ArgumentError, "capacity should be >= 1, was #@capacity" @data = {} end |
Instance Attribute Details
#capacity ⇒ Integer (readonly)
Returns the maximum capacity of the cache.
28 29 30 |
# File 'lib/tins/lru_cache.rb', line 28 def capacity @capacity end |
Instance Method Details
#[](key) ⇒ Object?
Retrieves the value associated with the given key.
If the key exists, it is moved to the most recently used position. Returns nil if the key does not exist.
37 38 39 40 41 42 43 44 |
# File 'lib/tins/lru_cache.rb', line 37 def [](key) case value = @data.delete(key) { NOT_EXIST } when NOT_EXIST nil else @data[key] = value end end |
#[]=(key, value) ⇒ Object
Associates a value with a key in the cache.
If the key already exists, its position is updated to most recently used. If adding this item exceeds the capacity, the least recently used item is removed.
55 56 57 58 59 60 61 62 |
# File 'lib/tins/lru_cache.rb', line 55 def []=(key, value) @data.delete(key) @data[key] = value if @data.size > @capacity @data.delete(@data.keys.first) end value end |
#clear ⇒ Tins::LRUCache
Removes all items from the cache.
87 88 89 90 |
# File 'lib/tins/lru_cache.rb', line 87 def clear @data.clear self end |
#delete(key) ⇒ Object?
Removes and returns the value associated with the given key.
80 81 82 |
# File 'lib/tins/lru_cache.rb', line 80 def delete(key) @data.delete(key) end |
#each {|key, value| ... } ⇒ Enumerator
Iterates over all key-value pairs in the cache.
Items are yielded in order from most recently used to least recently used.
72 73 74 |
# File 'lib/tins/lru_cache.rb', line 72 def each(&block) @data.reverse_each(&block) end |
#size ⇒ Integer
Returns the number of items currently in the cache.
95 96 97 |
# File 'lib/tins/lru_cache.rb', line 95 def size @data.size end |