Class: Typhoeus::Cache::Redis
- Inherits:
-
Object
- Object
- Typhoeus::Cache::Redis
- Defined in:
- lib/typhoeus/cache/redis.rb
Overview
This module provides a simple way to cache HTTP responses in Redis.
Instance Method Summary collapse
- #get(request) ⇒ Object
-
#initialize(redis = ::Redis.new, options = {}) ⇒ Redis
constructor
A new instance of Redis.
- #set(request, response) ⇒ Object
Constructor Details
#initialize(redis = ::Redis.new, options = {}) ⇒ Redis
Returns a new instance of Redis.
15 16 17 18 |
# File 'lib/typhoeus/cache/redis.rb', line 15 def initialize(redis = ::Redis.new, = {}) @redis = redis @default_ttl = [:default_ttl] end |
Instance Method Details
#get(request) ⇒ Object
20 21 22 23 24 |
# File 'lib/typhoeus/cache/redis.rb', line 20 def get(request) serialized_response = @redis.get(request.cache_key) return unless serialized_response Marshal.load(serialized_response) end |
#set(request, response) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/typhoeus/cache/redis.rb', line 26 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 |