Class: ConvenientService::Support::Cache::Entities::Caches::Hash
- Defined in:
- lib/convenient_service/support/cache/entities/caches/hash.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
#default, #key, #parent, #store
Instance Method Summary collapse
- #backend ⇒ Symbol
- #clear ⇒ ConvenientService::Support::Cache::Entities::Caches::Hash
-
#default=(value) ⇒ Object
Can be any type.
-
#delete(key) ⇒ Object
Can be any type.
- #empty? ⇒ Boolean
- #exist?(key) ⇒ Boolean
-
#fetch(key, &block) ⇒ Object
Can be any type.
- #initialize(store: {}, **kwargs) ⇒ void constructor
-
#read(key) ⇒ Object
Can be any type.
-
#scope(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base
Creates a scoped cache.
-
#scope!(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base
Creates a scoped cache.
-
#write(key, value) ⇒ Object
Can be any type.
Methods inherited from Base
#==, #[], #[]=, #get, keygen, #keygen, #set
Methods included from AbstractMethod
Constructor Details
#initialize(store: {}, **kwargs) ⇒ void
18 19 20 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 18 def initialize(store: {}, **kwargs) super(store: ::Hash.new(kwargs[:default]).update(store), **kwargs) end |
Instance Method Details
#backend ⇒ Symbol
25 26 27 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 25 def backend Constants::Backends::HASH end |
#clear ⇒ ConvenientService::Support::Cache::Entities::Caches::Hash
109 110 111 112 113 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 109 def clear store.clear self end |
#default=(value) ⇒ Object
Returns Can be any type.
149 150 151 152 153 154 155 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 149 def default=(value) save_self_as_scope_in_parent! store.default = value @default = value end |
#delete(key) ⇒ Object
Returns Can be any type.
98 99 100 101 102 103 104 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 98 def delete(key) value = store.delete(key) delete_self_as_scope_in_parent! if store.empty? value end |
#empty? ⇒ Boolean
35 36 37 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 35 def empty? store.empty? end |
#exist?(key) ⇒ Boolean
46 47 48 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 46 def exist?(key) store.has_key?(key) end |
#fetch(key, &block) ⇒ Object
Returns Can be any type.
83 84 85 86 87 88 89 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 83 def fetch(key, &block) return store[key] unless block save_self_as_scope_in_parent! store.fetch(key) { store[key] = yield } end |
#read(key) ⇒ Object
Returns Can be any type.
57 58 59 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 57 def read(key) store[key] end |
#scope(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base
Creates a scoped cache. Parent cache is modified on the first write to the scoped cache.
126 127 128 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 126 def scope(key, backed_by: backend, default: self.default) store.fetch(key) { Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) } end |
#scope!(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base
Creates a scoped cache. Parent cache is modified immediately.
141 142 143 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 141 def scope!(key, backed_by: backend, default: self.default) store.fetch(key) { store[key] = Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) } end |
#write(key, value) ⇒ Object
Returns Can be any type.
69 70 71 72 73 |
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 69 def write(key, value) save_self_as_scope_in_parent! store[key] = value end |