Module: Dry::Core::Cache
- Defined in:
- lib/dry/core/cache.rb
Overview
Allows you to cache call results that are solely determined by arguments.
Defined Under Namespace
Modules: Methods
Class Method Summary collapse
- .extended(klass) ⇒ Object private
Instance Method Summary collapse
- #cache ⇒ Object private
-
#fetch_or_store(*args) { ... } ⇒ Object
Caches a result of the block evaluation.
- #inherited(klass) ⇒ Object private
Class Method Details
.extended(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 26 27 |
# File 'lib/dry/core/cache.rb', line 23 def self.extended(klass) super klass.include(Methods) klass.instance_variable_set(:@__cache__, Concurrent::Map.new) end |
Instance Method Details
#cache ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 |
# File 'lib/dry/core/cache.rb', line 36 def cache @__cache__ end |
#fetch_or_store(*args) { ... } ⇒ Object
beware Proc instance hashes are not equal, i.e. -> { 1 }.hash != -> { 1 }.hash, this means you shouldn’t pass Procs in args unless you’re sure they are always the same instances, otherwise you introduce a memory leak
Caches a result of the block evaluation
51 52 53 |
# File 'lib/dry/core/cache.rb', line 51 def fetch_or_store(*args, &block) cache.fetch_or_store(args.hash, &block) end |
#inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
30 31 32 33 |
# File 'lib/dry/core/cache.rb', line 30 def inherited(klass) super klass.instance_variable_set(:@__cache__, cache) end |