Class: RustyLRU::Cache
- Inherits:
-
Object
- Object
- RustyLRU::Cache
- Extended by:
- Forwardable
- Includes:
- Enumerable, EnumHelpers
- Defined in:
- lib/rusty_lru/cache.rb
Overview
This class implements an LRU Cache.
Cache objects behave a lot like Ruby’s Hash class, but with the important difference that internally a list is maintained of all keys in order from most recently used to least recently used (LRU).
Additionally, if the cache is capped (see #initialize), then when the cache is full to capacity an operation that adds a new key will cause the key-value pair at the bottom of the LRU list (i.e. the least-recently-used pair) to be dropped from the cache. This way the cache never grows beyond a pre-determined size.
To precent items from being dropped, some operations (notably #[] and #[]= move the key to the front of the list.
Like Hash, this class includes the Enumerable module, so it responds to a number of methods not listed here, such as #map, #reduce, #to_a, #to_h, etc.
Like a Hash, values can be any object whatsoever, and keys can be any object that responds to #hash and #eql? correctly.
Defined Under Namespace
Modules: EnumHelpers
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Retrieves a value from the cache by key, updating the LRU list.
-
#[]=(key, value) ⇒ Object?
(also: #store)
Stores a key-value pair in the cache, updating the LRU list.
-
#clear ⇒ nil
Removes all key-value pairs from the cache.
-
#delete(key) ⇒ Object?
Deletes the key-value pair corresponding to the given key from the cache.
- #each_key ⇒ Object
- #each_pair ⇒ Object (also: #each)
- #each_value ⇒ Object
-
#empty? ⇒ Boolean
Returns true iff the cache is empty.
-
#has_key?(key) ⇒ Boolean
(also: #key?, #member?)
Returns true iff the given key is present.
-
#initialize(cap = nil) ⇒ Object
constructor
Initializes a new cache object.
-
#keys ⇒ Array<Object>
Returns all keys in the cache as an array.
-
#length ⇒ Integer
(also: #size)
Returns the number of key-value pairs stored in the cache.
-
#lru_pair ⇒ <(Object, Object)>?
Returns the least-recently used pair without affecting the LRU list.
-
#peek(key) ⇒ Object?
Retrieves a value from the cache by key without updating the LRU list.
-
#pop ⇒ <(Object, Object)>?
Deletes and returns the least-recently used key-value pair.
-
#resize(cap) ⇒ nil
Alters the store’s cap.
-
#values ⇒ Array<Object>
Returns all values in the cache as an array.
Constructor Details
#initialize(cap = nil) ⇒ Object
Initializes a new cache object.
If no cap is specified, the cache will be uncapped. A cap can be added later using #resize.
|
# File 'lib/rusty_lru/cache.rb', line 29
|
Instance Method Details
#[](key) ⇒ Object?
Retrieves a value from the cache by key, updating the LRU list.
|
# File 'lib/rusty_lru/cache.rb', line 41
|
#[]=(key, value) ⇒ Object? Also known as: store
Stores a key-value pair in the cache, updating the LRU list.
The stored pair becomes the most recently used upon insertion/update.
|
# File 'lib/rusty_lru/cache.rb', line 50
|
#clear ⇒ nil
Removes all key-value pairs from the cache.
|
# File 'lib/rusty_lru/cache.rb', line 130
|
#delete(key) ⇒ Object?
Deletes the key-value pair corresponding to the given key from the cache.
|
# File 'lib/rusty_lru/cache.rb', line 63
|
#each_key {|key| ... } ⇒ self #each_key ⇒ Enumerator
|
# File 'lib/rusty_lru/cache.rb', line 157
|
#each_pair {|key, value| ... } ⇒ self #each_pair ⇒ Enumerator Also known as: each
|
# File 'lib/rusty_lru/cache.rb', line 135
|
#each_value {|value| ... } ⇒ self #each_value ⇒ Enumerator
|
# File 'lib/rusty_lru/cache.rb', line 170
|
#empty? ⇒ Boolean
Returns true iff the cache is empty.
|
# File 'lib/rusty_lru/cache.rb', line 108
|
#has_key?(key) ⇒ Boolean Also known as: key?, member?
Returns true iff the given key is present. Does not affect the LRU list.
|
# File 'lib/rusty_lru/cache.rb', line 113
|
#keys ⇒ Array<Object>
Returns all keys in the cache as an array
206 |
# File 'lib/rusty_lru/cache.rb', line 206 def_delegator :each_key, :to_a, :keys |
#length ⇒ Integer Also known as: size
Returns the number of key-value pairs stored in the cache.
|
# File 'lib/rusty_lru/cache.rb', line 119
|
#lru_pair ⇒ <(Object, Object)>?
Returns the least-recently used pair without affecting the LRU list.
This method is similar to #pop, but does not mutate the cache.
|
# File 'lib/rusty_lru/cache.rb', line 98
|
#peek(key) ⇒ Object?
Retrieves a value from the cache by key without updating the LRU list.
This method is equivalent to #[], except that the LRU list is not affected.
|
# File 'lib/rusty_lru/cache.rb', line 85
|
#pop ⇒ <(Object, Object)>?
Deletes and returns the least-recently used key-value pair.
|
# File 'lib/rusty_lru/cache.rb', line 74
|
#resize(cap) ⇒ nil
Alters the store’s cap.
|
# File 'lib/rusty_lru/cache.rb', line 124
|
#values ⇒ Array<Object>
Returns all values in the cache as an array
212 |
# File 'lib/rusty_lru/cache.rb', line 212 def_delegator :each_value, :to_a, :values |