Class: Smooth::Adapters::RedisCache

Inherits:
Object
  • Object
show all
Defined in:
lib/smooth/adapters/redis_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}
  if redis.is_a?(Hash)
    options = redis
    redis = nil
  end

  @redis = redis || Redis.new(options)
end

Instance Attribute Details

#redisObject

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