Class: Exchange::Cache::Rails

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

Overview

A class that cooperates with rails to cache the data from the exchange api in rails

Examples:

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

Exchange::Configuration.define do |c| 
  c.cache = :rails
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 the memcached client or calls the block and caches it in rails cache. 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 and calls it if no cached result is available

Raises:

Since:

  • 0.1



35
36
37
38
39
40
41
42
# File 'lib/exchange/cache/rails.rb', line 35

def cached api, opts={}, &block
  raise_caching_needs_block! unless block_given?
  
  result = client.fetch key(api, opts), :expires_in => config.expire == :daily ? 86400 : 3600, &block
  client.delete(key(api, opts)) unless result && !result.to_s.empty?
  
  result
end

#clientActiveSupport::Cache::Subclass

returns a Rails cache client. This has not to be stored since rails already memoizes it. Use this client to access rails cache data. For further explanation of use visit the rails documentation

Examples:

Exchange::Cache::Rails.client.set('FOO', 'BAR')

Returns:

  • (ActiveSupport::Cache::Subclass)

    an instance of the rails cache class (presumably a subclass of ActiveSupport::Cache)

Since:

  • 0.1



22
23
24
25
# File 'lib/exchange/cache/rails.rb', line 22

def client
  Exchange::GemLoader.new('rails').try_load unless defined?(::Rails)
  ::Rails.cache
end