This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
module Typhoeus
|
||||
module Cache
|
||||
# This module provides a simple way to cache HTTP responses using Dalli.
|
||||
class Dalli
|
||||
# @example Set Dalli as the Typhoeus cache backend
|
||||
# Typhoeus::Config.cache = Typhoeus::Cache::Dalli.new
|
||||
#
|
||||
# @param [ Dalli::Client ] client
|
||||
# A connection to the cache server. Defaults to `Dalli::Client.new`
|
||||
# @param [ Hash ] options
|
||||
# Options
|
||||
# @option options [ Integer ] :default_ttl
|
||||
# The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
|
||||
def initialize(client = ::Dalli::Client.new, options = {})
|
||||
@client = client
|
||||
@default_ttl = options[:default_ttl]
|
||||
end
|
||||
|
||||
def get(request)
|
||||
@client.get(request.cache_key)
|
||||
end
|
||||
|
||||
def set(request, response)
|
||||
@client.set(request.cache_key, response, request.cache_ttl || @default_ttl)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
module Typhoeus
|
||||
module Cache
|
||||
# This module provides a simple way to cache HTTP responses in using the Rails cache.
|
||||
class Rails
|
||||
# @example Use the Rails cache setup to cache Typhoeus responses.
|
||||
# Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
|
||||
#
|
||||
# @param [ ActiveSupport::Cache::Store ] cache
|
||||
# A Rails cache backend. Defaults to Rails.cache.
|
||||
# @param [ Hash ] options
|
||||
# Options
|
||||
# @option options [ Integer ] :default_ttl
|
||||
# The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
|
||||
def initialize(cache = ::Rails.cache, options = {})
|
||||
@cache = cache
|
||||
@default_ttl = options[:default_ttl]
|
||||
end
|
||||
|
||||
def get(request)
|
||||
@cache.read(request)
|
||||
end
|
||||
|
||||
def set(request, response)
|
||||
@cache.write(request.cache_key, response, :expires_in => request.cache_ttl || @default_ttl)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
module Typhoeus
|
||||
module Cache
|
||||
# This module provides a simple way to cache HTTP responses in Redis.
|
||||
class Redis
|
||||
# @example Set Redis as the Typhoeus cache backend
|
||||
# Typhoeus::Config.cache = Typhoeus::Cache::Redis.new
|
||||
#
|
||||
# @param [ Redis ] redis
|
||||
# A connection to Redis. Defaults to `Redis.new`, which uses the
|
||||
# `REDIS_URL` environment variable to connect
|
||||
# @param [ Hash ] options
|
||||
# Options
|
||||
# @option options [ Integer ] :default_ttl
|
||||
# The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
|
||||
def initialize(redis = ::Redis.new, options = {})
|
||||
@redis = redis
|
||||
@default_ttl = options[:default_ttl]
|
||||
end
|
||||
|
||||
def get(request)
|
||||
serialized_response = @redis.get(request.cache_key)
|
||||
return unless serialized_response
|
||||
Marshal.load(serialized_response)
|
||||
end
|
||||
|
||||
def set(request, response)
|
||||
ttl = request.cache_ttl || @default_ttl
|
||||
key = request.cache_key
|
||||
serialized_response = Marshal.dump(response)
|
||||
@redis.set(key, serialized_response)
|
||||
@redis.expire(key, ttl) if ttl
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user