Class: Retriever::Instruction
- Inherits:
-
Object
- Object
- Retriever::Instruction
- Defined in:
- lib/retriever/instruction.rb
Overview
targets in Retriever.catch!
Instance Attribute Summary collapse
-
#block ⇒ Object
Block executed to retrieve value to be cached.
-
#key ⇒ Object
Key used by instruction for storage manipulation.
Instance Method Summary collapse
-
#execute(storage, block_parameters) ⇒ Object
Checks if value in storage is expired before trying to retrieve a new value.
-
#execute!(storage, block_parameters) ⇒ Object
Cache the result of the block.
-
#initialize(key, options, block) ⇒ Instruction
constructor
Instantiate Instruction.
Constructor Details
#initialize(key, options, block) ⇒ Instruction
Instantiate Instruction
Parameters
-
:key
- Key used to store and retrieve value from cache. -
:options
- Options defined inRetriever.catch!
-
:block
- Block is theProc
object to be executed. Resulting value would be the cached.
22 23 24 25 26 |
# File 'lib/retriever/instruction.rb', line 22 def initialize(key, , block) @key = key @block = block @validity = [:validity] || 365.days end |
Instance Attribute Details
#block ⇒ Object
Block executed to retrieve value to be cached.
11 12 13 |
# File 'lib/retriever/instruction.rb', line 11 def block @block end |
#key ⇒ Object
Key used by instruction for storage manipulation.
8 9 10 |
# File 'lib/retriever/instruction.rb', line 8 def key @key end |
Instance Method Details
#execute(storage, block_parameters) ⇒ Object
Checks if value in storage is expired before trying to retrieve a new value. Value in storage is returned if value is still valid.
Parameters
-
:storage
- Instance of the storage handler. -
:block_parameters
- Arguments sent to the block if value in storage is expired.
37 38 39 |
# File 'lib/retriever/instruction.rb', line 37 def execute(storage, block_parameters) if expired? then execute!(storage, block_parameters) else storage.get(key) end end |
#execute!(storage, block_parameters) ⇒ Object
Cache the result of the block. Just like execute
but does not check cached value.
Parameters
-
:storage
- Instance of the storage handler. -
:block_parameters
- Arguments sent to the block if value in storage is expired.
50 51 52 53 |
# File 'lib/retriever/instruction.rb', line 50 def execute!(storage, block_parameters) @last_execution = Time.now storage.set(key, block.call(*block_parameters)) end |