Class: Wrest::Caching::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/wrest/caching/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis_options = {}) ⇒ Redis

Returns a new instance of Redis.



14
15
16
# File 'lib/wrest/caching/redis.rb', line 14

def initialize(redis_options = {})
  @redis = ::Redis.new(redis_options)
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
21
22
# File 'lib/wrest/caching/redis.rb', line 18

def [](key)
  value = @redis.get(key)
  unmarshalled_value = value.nil? ? nil : YAML::load(value)
  unmarshalled_value
end

#[]=(key, response) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/wrest/caching/redis.rb', line 24

def []=(key, response)
  marshalled_response = YAML::dump(response)
  @redis.set(key, marshalled_response)
  unless response.expired?
    @redis.expire(key, response.freshness_lifetime)
  end
end

#delete(key) ⇒ Object



32
33
34
35
36
# File 'lib/wrest/caching/redis.rb', line 32

def delete(key)
  value = self[key]
  @redis.del(key)
  return value
end