Class: Typhoeus::Cache::Rails

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

Overview

This module provides a simple way to cache HTTP responses in using the Rails cache.

Since:

  • 0.5.0

Instance Method Summary collapse

Constructor Details

#initialize(cache = ::Rails.cache, options = {}) ⇒ Rails

Returns a new instance of Rails.

Examples:

Use the Rails cache setup to cache Typhoeus responses.

Typhoeus::Config.cache = Typhoeus::Cache::Rails.new

Parameters:

  • cache (ActiveSupport::Cache::Store) (defaults to: ::Rails.cache)

    A Rails cache backend. Defaults to Rails.cache.

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

    Options

Options Hash (options):

  • :default_ttl (Integer)

    The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.

Since:

  • 0.5.0



14
15
16
17
# File 'lib/typhoeus/cache/rails.rb', line 14

def initialize(cache = ::Rails.cache, options = {})
  @cache = cache
  @default_ttl = options[:default_ttl]
end

Instance Method Details

#get(request) ⇒ Object

Since:

  • 0.5.0



19
20
21
# File 'lib/typhoeus/cache/rails.rb', line 19

def get(request)
  @cache.read(request)
end

#set(request, response) ⇒ Object

Since:

  • 0.5.0



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

def set(request, response)
  @cache.write(request.cache_key, response, :expires_in => request.cache_ttl || @default_ttl)
end