Class: Stash::Cache
Overview
Key/value cache store
Instance Attribute Summary collapse
-
#store ⇒ Hash
readonly
Of the mem stash cache hash store.
Instance Method Summary collapse
-
#[](key = nil) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, returns KeyError.
-
#cache(key = nil, &code) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, run the code, cache the result, and return it.
-
#clear!(key = nil) ⇒ Object
Clear the whole stash store or the value of a key.
-
#include?(key = nil) ⇒ TrueClass, FalseClass
return a boolean indicating presence of the given key in the store.
-
#initialize(params = {}) ⇒ Cache
constructor
Initializes a new empty store.
-
#size ⇒ Fixnum
return the size of the store as an integer.
Constructor Details
#initialize(params = {}) ⇒ Cache
Initializes a new empty store
54 55 56 57 |
# File 'lib/hoodie/stash.rb', line 54 def initialize(params = {}) params = { store: params } unless params.is_a? Hash @store = params.fetch(:store) { Stash::DEFAULT_STORE.new } end |
Instance Attribute Details
#store ⇒ Hash (readonly)
Returns of the mem stash cache hash store.
50 51 52 |
# File 'lib/hoodie/stash.rb', line 50 def store @store end |
Instance Method Details
#[](key = nil) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, returns KeyError
80 81 82 83 84 |
# File 'lib/hoodie/stash.rb', line 80 def [](key = nil) key ||= Stash.caller_name fail KeyError, 'Key not cached' unless include? key.to_sym @store[key.to_sym] end |
#cache(key = nil, &code) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, run the code, cache the result, and return it
94 95 96 97 |
# File 'lib/hoodie/stash.rb', line 94 def cache(key = nil, &code) key ||= Stash.caller_name @store[key.to_sym] ||= code.call end |
#clear!(key = nil) ⇒ Object
Clear the whole stash store or the value of a key
clear.
66 67 68 69 |
# File 'lib/hoodie/stash.rb', line 66 def clear!(key = nil) key = key.to_sym unless key.nil? @store.clear! key end |
#include?(key = nil) ⇒ TrueClass, FalseClass
return a boolean indicating presence of the given key in the store
113 114 115 116 |
# File 'lib/hoodie/stash.rb', line 113 def include?(key = nil) key ||= Stash.caller_name @store.include? key.to_sym end |
#size ⇒ Fixnum
return the size of the store as an integer
103 104 105 |
# File 'lib/hoodie/stash.rb', line 103 def size @store.size end |