Module: Retriever

Defined in:
lib/retriever.rb,
lib/retriever/config.rb,
lib/retriever/version.rb,
lib/retriever/instruction.rb

Overview

Main Retriever Entry Point

Defined Under Namespace

Modules: Config, Storage Classes: Instruction

Constant Summary collapse

VERSION =

Version of Retriever

"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.instructionsObject

Array of instructions.



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

def instructions
  @instructions
end

.storageObject

Instance of storage handler.



9
10
11
# File 'lib/retriever.rb', line 9

def storage
  @storage
end

Class Method Details

.catch!(&block) ⇒ Object

Setup targets for retriever to cache.

Example

Retriever.catch! do
  target :bone do
    'wishbone'
  end
end


24
25
26
27
28
# File 'lib/retriever.rb', line 24

def catch!(&block)
  @instance_timestamp = Time.now
  @instructions       = Hash.new
  self.instance_eval &block
end

.clean!Object

Delete all cache. Automatically run on instance process termination.



120
121
122
123
124
125
# File 'lib/retriever.rb', line 120

def clean!
  return unless instructions
  instructions.keys.each do |key|
    storage.delete(key)
  end
end

.configObject

Returns config namespace.



114
115
116
# File 'lib/retriever.rb', line 114

def config
  Config
end

.encrypted?Boolean

Check if keys used for the cache store is encrypted or not encrypted. Basis of key is namespaced to retriever as well as timestamped. (Timestamp comes from the instance start.)

Returns:

  • (Boolean)


108
109
110
# File 'lib/retriever.rb', line 108

def encrypted?
  config.encrypt || false
end

.fetch(target_name, *block_parameters) ⇒ Object

Fetch data based upon the execution result of the corresponding instruction identified by the target_name. If data cached is still valid, that data would be returned.

Parameters

  • :target_name - The name used to identify the instruction.

  • :*block_parameters - Parameters passed on to the block that is part of the instruction.

Examples

Retriever.fetch(:bone)


79
80
81
82
83
# File 'lib/retriever.rb', line 79

def fetch(target_name, *block_parameters)
  key = keyify(target_name)
  instruction = instructions[key]
  instruction.execute(storage, block_parameters)
end

.fetch!(target_name, *block_parameters) ⇒ Object

Fetch data based upon the execution result of the corresponding instruction identified by the target_name.

Parameters

  • :target_name - The name used to identify the instruction.

  • :*block_parameters - Parameters passed on to the block that is part of the instruction.

Examples

Retriever.fetch(:bone)


98
99
100
101
102
# File 'lib/retriever.rb', line 98

def fetch!(target_name, *block_parameters)
  key = keyify(target_name)
  instruction = instructions[key]
  instruction.execute!(storage, block_parameters)
end

.target(name, options = {}, &block) ⇒ Object

Setup a new target for retriever to catch.

Parameters

  • :name - Identifier used to call the block whenever you call fetch or fetch!.

  • :options - Options that modify basic instruction of simply caching the result of the block.

  • :block - The block to be executed with it’s result being cached.

Options

  • :validity - Sets length of validity of a certain cache. After the cache expires, retriever simply executes the block again.

    target :bone, :validity => 10.minutes
    

Example

Retriever.catch! do
  @counter = 0
  target :bone, :validity => 10.minutes do
    @counter += 1
  end
end

Retriever.fetch(:bone) # => 1
# After 10 minutes
Retreiver.fetch(:bone) # => 2


60
61
62
63
# File 'lib/retriever.rb', line 60

def target(name, options = {}, &block)
  key = keyify(name)
  instructions[key] = Instruction.new(key, options, block)
end