class Typhoeus::Cache::Redis
This module provides a simple way to cache HTTP responses in Redis
.
Public Class Methods
new(redis = ::Redis.new, options = {})
click to toggle source
@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 [ Integer ] default_ttl
The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
# File lib/typhoeus/cache/redis.rb, line 13 def initialize(redis = ::Redis.new, options = {}) @redis = redis @default_ttl = options[:default_ttl] end
Public Instance Methods
get(request)
click to toggle source
# File lib/typhoeus/cache/redis.rb, line 18 def get(request) serialized_response = @redis.get(request.cache_key) return unless serialized_response Marshal.load(serialized_response) end
set(request, response)
click to toggle source
# File lib/typhoeus/cache/redis.rb, line 24 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