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. Both read and write are considered access.
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.
13 14 15 16 17 18 |
# File 'lib/tins/lru_cache.rb', line 13 def initialize(capacity) @capacity = Integer(capacity) @capacity >= 1 or raise ArgumentError, "capacity should be >= 1, was #@capacity" @data = {} # Least-recently used will always be the first element end |
Instance Attribute Details
#capacity ⇒ Integer (readonly)
Returns the maximum capacity of the cache.
23 24 25 |
# File 'lib/tins/lru_cache.rb', line 23 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.
32 33 34 35 36 |
# File 'lib/tins/lru_cache.rb', line 32 def [](key) if @data.has_key?(key) @data[key] = @data.delete(key) 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.
47 48 49 50 51 52 53 54 |
# File 'lib/tins/lru_cache.rb', line 47 def []=(key, value) @data.delete(key) @data[key] = value if @data.size > @capacity @data.shift end value end |
#clear ⇒ Tins::LRUCache
Removes all items from the cache.
79 80 81 82 |
# File 'lib/tins/lru_cache.rb', line 79 def clear @data.clear self end |
#delete(key) ⇒ Object?
Removes and returns the value associated with the given key.
72 73 74 |
# File 'lib/tins/lru_cache.rb', line 72 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.
64 65 66 |
# File 'lib/tins/lru_cache.rb', line 64 def each(&block) @data.reverse_each(&block) end |
#size ⇒ Integer
Returns the number of items currently in the cache.
87 88 89 |
# File 'lib/tins/lru_cache.rb', line 87 def size @data.size end |