Class: BennyCache::Cache
- Inherits:
-
Object
- Object
- BennyCache::Cache
- Defined in:
- lib/benny_cache/cache.rb
Instance Method Summary collapse
- #clear(options = nil) ⇒ Object
- #delete(key, options = nil) ⇒ Object
- #fetch(key, options = nil, &block) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read(key, options = nil) ⇒ Object
- #write(key, val, options = nil) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
3 4 5 |
# File 'lib/benny_cache/cache.rb', line 3 def initialize @cache = {} end |
Instance Method Details
#clear(options = nil) ⇒ Object
39 40 41 |
# File 'lib/benny_cache/cache.rb', line 39 def clear( = nil) @cache = {} end |
#delete(key, options = nil) ⇒ Object
35 36 37 |
# File 'lib/benny_cache/cache.rb', line 35 def delete(key, = nil) @cache.delete(key) end |
#fetch(key, options = nil, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/benny_cache/cache.rb', line 7 def fetch(key, = nil, &block) val = @cache[key] if val.nil? && block_given? val = block.call() @cache[key] = val end begin val = val.dup unless val.nil? rescue TypeError #okay end val end |
#read(key, options = nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/benny_cache/cache.rb', line 24 def read(key, = nil) val = @cache[key] val = val.dup unless val.nil? end |
#write(key, val, options = nil) ⇒ Object
30 31 32 33 |
# File 'lib/benny_cache/cache.rb', line 30 def write(key, val, = nil) @cache[key] = val.dup return true end |