Class: DS::Util::Cache
- Inherits:
-
Object
- Object
- DS::Util::Cache
- Defined in:
- lib/ds/util/cache.rb
Constant Summary collapse
- DEFAULT_MAX_SIZE =
10- UNLIMITED_SIZE =
Float::INFINITY
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#keys ⇒ Array<Object>
readonly
Returns an array of all the keys in the cache.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieves an item from the cache using the specified key.
-
#add(key, item) ⇒ Object
Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.
-
#cleanup ⇒ void
Cleanup the cache by removing items until the size is less than or equal to the maximum size.
-
#delete_item(key) ⇒ void
Deletes an item from the cache using the specified key.
-
#get_item(key) ⇒ Object
Retrieves an item from the cache using the specified key.
-
#get_or_add(key, item) ⇒ Object
Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.
-
#include?(key) ⇒ Boolean
Checks if the given key is present in the cache.
-
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ void
constructor
Initializes a new instance of the class with the specified maximum size.
-
#size ⇒ Integer
Returns the number of items in the cache.
-
#unlimited? ⇒ Boolean
Checks if the cache is unlimited.
Constructor Details
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ void
Initializes a new instance of the class with the specified maximum size.
17 18 19 20 |
# File 'lib/ds/util/cache.rb', line 17 def initialize max_size: DEFAULT_MAX_SIZE @max_size = max_size @items = {} end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
10 11 12 |
# File 'lib/ds/util/cache.rb', line 10 def items @items end |
#keys ⇒ Array<Object> (readonly)
Returns an array of all the keys in the cache.
78 79 80 |
# File 'lib/ds/util/cache.rb', line 78 def keys @keys end |
#max_size ⇒ Object
Returns the value of attribute max_size.
9 10 11 |
# File 'lib/ds/util/cache.rb', line 9 def max_size @max_size end |
Instance Method Details
#[](key) ⇒ Object
Retrieves an item from the cache using the specified key.
71 72 73 |
# File 'lib/ds/util/cache.rb', line 71 def [](key) get_item key end |
#add(key, item) ⇒ Object
Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.
37 38 39 40 41 42 |
# File 'lib/ds/util/cache.rb', line 37 def add key, item delete_item key items[key] = item cleanup item end |
#cleanup ⇒ void
This method returns an undefined value.
Cleanup the cache by removing items until the size is less than or equal to the maximum size.
This method does not take any parameters.
102 103 104 105 106 107 108 |
# File 'lib/ds/util/cache.rb', line 102 def cleanup return if size < max_size return if keys.blank? # don't allow an infinite loop while size > max_size delete_item keys.first end end |
#delete_item(key) ⇒ void
This method returns an undefined value.
Deletes an item from the cache using the specified key.
93 94 95 |
# File 'lib/ds/util/cache.rb', line 93 def delete_item key items.delete key end |
#get_item(key) ⇒ Object
Retrieves an item from the cache using the specified key.
63 64 65 |
# File 'lib/ds/util/cache.rb', line 63 def get_item key items[key] end |
#get_or_add(key, item) ⇒ Object
Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.
27 28 29 30 |
# File 'lib/ds/util/cache.rb', line 27 def get_or_add key, item add(key, item) unless include? key && unlimited? get_item key end |
#include?(key) ⇒ Boolean
Checks if the given key is present in the cache.
48 49 50 |
# File 'lib/ds/util/cache.rb', line 48 def include? key keys.include? key end |
#size ⇒ Integer
Returns the number of items in the cache.
85 86 87 |
# File 'lib/ds/util/cache.rb', line 85 def size keys.size end |
#unlimited? ⇒ Boolean
Checks if the cache is unlimited.
55 56 57 |
# File 'lib/ds/util/cache.rb', line 55 def unlimited? max_size == UNLIMITED_SIZE end |