Class: Nutrella::Cache
- Inherits:
-
Object
- Object
- Nutrella::Cache
- Defined in:
- lib/nutrella/cache.rb
Overview
Provides a cache of the most recently used items.
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #fetch(key) ⇒ Object
-
#initialize(path, capacity) ⇒ Cache
constructor
A new instance of Cache.
- #search(search_reg_exp) ⇒ Object
Constructor Details
#initialize(path, capacity) ⇒ Cache
Returns a new instance of Cache.
12 13 14 15 |
# File 'lib/nutrella/cache.rb', line 12 def initialize(path, capacity) @path = path @capacity = capacity end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
10 11 12 |
# File 'lib/nutrella/cache.rb', line 10 def capacity @capacity end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/nutrella/cache.rb', line 10 def path @path end |
Instance Method Details
#fetch(key) ⇒ Object
17 18 19 20 21 |
# File 'lib/nutrella/cache.rb', line 17 def fetch(key) value = lookup(key) || yield write(key, value) value end |
#search(search_reg_exp) ⇒ Object
23 24 25 26 27 |
# File 'lib/nutrella/cache.rb', line 23 def search(search_reg_exp) cache_contents.find { |k, _v| search_reg_exp.match?(k) }.last rescue nil end |