Class: Smooth::Adapters::RedisCache
- Inherits:
-
Object
- Object
- Smooth::Adapters::RedisCache
- Defined in:
- lib/smooth/adapters/redis_cache.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #fetch(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(redis, options = {}) ⇒ RedisCache
constructor
A new instance of RedisCache.
- #write(key, value) ⇒ Object
Constructor Details
#initialize(redis, options = {}) ⇒ RedisCache
Returns a new instance of RedisCache.
6 7 8 9 10 11 12 13 |
# File 'lib/smooth/adapters/redis_cache.rb', line 6 def initialize redis, ={} if redis.is_a?(Hash) = redis redis = nil end @redis = redis || Redis.new() end |
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
4 5 6 |
# File 'lib/smooth/adapters/redis_cache.rb', line 4 def redis @redis end |
Instance Method Details
#fetch(key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/smooth/adapters/redis_cache.rb', line 24 def fetch key result = get(key) if result.nil? and block_given? fallback = yield result = write(key, fallback) end result end |
#get(key) ⇒ Object
15 16 17 |
# File 'lib/smooth/adapters/redis_cache.rb', line 15 def get key redis.get(key) end |
#write(key, value) ⇒ Object
19 20 21 22 |
# File 'lib/smooth/adapters/redis_cache.rb', line 19 def write key, value redis.set(key, value) value end |