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.



16
17
18
# File 'lib/wrest/caching/redis.rb', line 16

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

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, response) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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