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.
13 14 15 16 |
# File 'lib/typhoeus/cache/redis.rb', line 13 def initialize(redis = ::Redis.new, = {}) @redis = redis @default_ttl = [:default_ttl] end |
Instance Method Details
#get(request) ⇒ Object
18 19 20 21 22 |
# 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) ⇒ Object
24 25 26 27 28 29 30 |
# 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 |