Class: Storyblok::Cache::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/storyblok/cache/redis.rb

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  ttl: 60 * 60 * 24
}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Redis

Returns a new instance of Redis.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/storyblok/cache/redis.rb', line 8

def initialize(*args)
  options = args.last.is_a?(::Hash) ? args.pop : {}

  @redis = options.delete(:redis) || begin
    if defined?(::Redis)
      ::Redis.current
    else
      raise "Redis.current could not be found. Supply :redis option or make sure Redis.current is available."
    end
  end

  @options = DEFAULT_CONFIGURATION.merge(options)
end

Instance Method Details

#cache(key, expire = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/storyblok/cache/redis.rb', line 22

def cache(key, expire = nil)
  if expire == 0
    return yield(self)
  end

  expire ||= @options[:ttl]

  if (value = get(key)).nil?
    value = yield(self)
    set(key, value, expire)
  end

  value
end

#get(key) ⇒ Object



37
38
39
# File 'lib/storyblok/cache/redis.rb', line 37

def get(key)
  @redis.get(key)
end

#set(key, value, expire = false) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/storyblok/cache/redis.rb', line 41

def set(key, value, expire = false)
  if expire
    @redis.setex(key, expire, value)
  else
    @redis.set(key, value)
  end
end