Class: Exchange::Cache::Memory

Inherits:
Base
  • Object
show all
Defined in:
lib/exchange/cache/memory.rb

Overview

A class that uses instance variables on the cache singleton class to store values in memory

Examples:

Activate caching via memory by setting the cache in the configuration to :memory

Exchange::Configuration.define do |c| 
  c.cache = :memory
end

Author:

  • Beat Richartz

Since:

  • 0.1

Version:

  • 0.1

Instance Method Summary collapse

Instance Method Details

#cached(api, opts = {}) { ... } ⇒ Object

returns either cached data from an instance variable or calls the block and caches it in an instance variable. This method has to be the same in all the cache classes in order for the configuration binding to work

Parameters:

  • api (Exchange::ExternalAPI::Subclass)

    The API class the data has to be stored for

  • opts (Hash) (defaults to: {})

    the options to cache with

Options Hash (opts):

  • :at (Time)

    the historic time of the exchange rates to be cached

Yields:

  • This method takes a mandatory block with an arity of 0 for caching

Raises:

Since:

  • 0.1


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/exchange/cache/memory.rb', line 24

def cached api, opts={}, &block
  ivar_name = instance_variable_name(api, opts)

  result = instance_variable_get(ivar_name) 
  
  unless result && !result.to_s.empty?
    result = super

    if result && !result.to_s.empty?
      instance_variable_set(ivar_name, result)
    end 

    clean!
  end
  
  opts[:plain] ? result.cachify : result
end