Class: Retriever::Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/retriever/instruction.rb

Overview

targets in Retriever.catch!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, options, block) ⇒ Instruction

Instantiate Instruction

Parameters

  • :key - Key used to store and retrieve value from cache.

  • :options - Options defined in Retriever.catch!

  • :block - Block is the Proc 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, options, block)
  @key   = key
  @block = block
  @validity = options[:validity] || 365.days
end

Instance Attribute Details

#blockObject

Block executed to retrieve value to be cached.



11
12
13
# File 'lib/retriever/instruction.rb', line 11

def block
  @block
end

#keyObject

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