Class: SmartCore::Engine::Cache
- Inherits:
-
Object
- Object
- SmartCore::Engine::Cache
- Defined in:
- lib/smart_core/engine/cache.rb
Overview
Instance Method Summary collapse
- #clear ⇒ NilClass
- #initialize ⇒ void constructor
-
#read(key, &fallback) ⇒ Any, NilClass
(also: #[])
rubocop:disable Style/NestedTernaryOperator.
- #write(key, value) ⇒ Any (also: #[]=)
Constructor Details
#initialize ⇒ void
10 11 12 13 |
# File 'lib/smart_core/engine/cache.rb', line 10 def initialize @store = {} # TODO: thread-safety (use SmartCore::Engine::Lock) end |
Instance Method Details
#clear ⇒ NilClass
45 46 47 48 |
# File 'lib/smart_core/engine/cache.rb', line 45 def clear @store.clear nil end |
#read(key, &fallback) ⇒ Any, NilClass Also known as: []
rubocop:disable Style/NestedTernaryOperator
33 34 35 36 37 |
# File 'lib/smart_core/engine/cache.rb', line 33 def read(key, &fallback) # @note # key?-flow is a compromise used to provide an ability to cache `nil` objects too. @store.key?(key) ? @store[key] : (block_given? ? write(key, yield) : nil) end |
#write(key, value) ⇒ Any Also known as: []=
21 22 23 |
# File 'lib/smart_core/engine/cache.rb', line 21 def write(key, value) @store[key] = value end |