Class: Redis

Inherits:
Object
  • Object
show all
Defined in:
app/redis.rb

Instance Method Summary collapse

Instance Method Details

#cache(params) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/redis.rb', line 33

def cache(params)
  key = params[:key] || raise(":key parameter is required!")
  expire = params[:expire] || nil
  recalculate = params[:recalculate] || nil
  timeout = params[:timeout] || nil
  default = params[:default] || nil

  if (value = get(key)).nil? || recalculate
    begin
      value = Timeout::timeout(timeout) { yield(self) }
    rescue Timeout::Error
      value = default
    end

    set(key, value)
    expire(key, expire) if expire
    value
  else
    value
  end
end