Class: Typhoeus::Cache::Dalli

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

Overview

This module provides a simple way to cache HTTP responses using Dalli.

Since:

  • 0.5.0

Instance Method Summary collapse

Constructor Details

#initialize(client = ::Dalli::Client.new, options = {}) ⇒ Dalli

Returns a new instance of Dalli.

Examples:

Set Dalli as the Typhoeus cache backend

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

Parameters:

  • client (Dalli::Client) (defaults to: ::Dalli::Client.new)

    A connection to the cache server. Defaults to ‘Dalli::Client.new`

  • 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/dalli.rb', line 14

def initialize(client = ::Dalli::Client.new, options = {})
  @client = client
  @default_ttl = options[:default_ttl]
end

Instance Method Details

#get(request) ⇒ Object

Since:

  • 0.5.0



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

def get(request)
  @client.get(request.cache_key)
end

#set(request, response) ⇒ Object

Since:

  • 0.5.0



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

def set(request, response)
  @client.set(request.cache_key, response, request.cache_ttl || @default_ttl)
end