Class: Blix::Rest::RedisCache

Inherits:
Cache
  • Object
show all
Defined in:
lib/blix/rest/redis_cache.rb

Constant Summary collapse

STORE_PREFIX =
'blixcache'

Instance Attribute Summary

Attributes inherited from Cache

#params

Instance Method Summary collapse

Methods inherited from Cache

#[], #[]=, #initialize

Constructor Details

This class inherits a constructor from Blix::Rest::Cache

Instance Method Details

#_all_keys(name = nil) ⇒ Object



80
81
82
# File 'lib/blix/rest/redis_cache.rb', line 80

def _all_keys(name = nil)
  redis.keys("#{_prefix}#{name}*") || []
end

#_decode(msg) ⇒ Object



111
112
113
# File 'lib/blix/rest/redis_cache.rb', line 111

def _decode(msg)
  Marshal.load(msg)
end

#_encode(data) ⇒ Object



107
108
109
# File 'lib/blix/rest/redis_cache.rb', line 107

def _encode(data)
  Marshal.dump(data)
end

#_key(name) ⇒ Object




57
58
59
# File 'lib/blix/rest/redis_cache.rb', line 57

def _key(name)
  _prefix + name
end

#_optsObject



61
62
63
64
65
66
67
68
# File 'lib/blix/rest/redis_cache.rb', line 61

def _opts
  @_opts ||= begin
    o = ::Blix::Rest::StringHash.new
    o[:prefix] = STORE_PREFIX
    o.merge!(params)
    o
  end
end

#_prefixObject



70
71
72
# File 'lib/blix/rest/redis_cache.rb', line 70

def _prefix
  @_prefix ||= _opts[:prefix]
end

#cleanup(opts = nil) ⇒ Object

delete expired sessions from the store. this should be handled automatically by redis if the ttl is set on save correctly



105
# File 'lib/blix/rest/redis_cache.rb', line 105

def cleanup(opts = nil); end

#clearObject

clear all data from the cache



20
21
22
# File 'lib/blix/rest/redis_cache.rb', line 20

def clear
  reset
end

#delete(id) ⇒ Object



51
52
53
# File 'lib/blix/rest/redis_cache.rb', line 51

def delete(id)
  redis.del(_key(id)) > 0
end

#get(id) ⇒ Object

retrieve data from the cache



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blix/rest/redis_cache.rb', line 25

def get(id)
  key = _key(id)
  str = redis.get(key)
  data = str && begin
                  _decode(str)
                rescue StandardError
                   redis.del( key)
                   nil
                end
  redis.expire(key, _opts[:expire_secs]) if data && _opts[:reset_expire_on_get] && _opts.key?(:expire_secs)
  data
end

#get_expiry_secsObject



121
122
123
# File 'lib/blix/rest/redis_cache.rb', line 121

def get_expiry_secs
  _opts[:expire_secs] || _opts['expire_secs']
end

#get_expiry_timeObject



115
116
117
118
119
# File 'lib/blix/rest/redis_cache.rb', line 115

def get_expiry_time
  if expire = _opts[:expire_secs] || _opts['expire_secs']
    Time.now - expire
  end
end

#key?(id) ⇒ Boolean

is key present in the cache

Returns:

  • (Boolean)


47
48
49
# File 'lib/blix/rest/redis_cache.rb', line 47

def key?(id)
  redis.get(_key(id)) != nil
end

#lengthObject

the number of sessions in the store



99
100
101
# File 'lib/blix/rest/redis_cache.rb', line 99

def length
  _all_keys.length
end

#redisObject

the redis session store



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/blix/rest/redis_cache.rb', line 85

def redis
  @redis ||= begin
    r = Redis.new
    begin
      r.ping
    rescue Exception => e
      Blix::Rest.logger.error "cannot reach redis server:#{e}"
      raise
    end
    r
  end
end

#reset(name = nil) ⇒ Object

remove all sessions from the store



75
76
77
78
# File 'lib/blix/rest/redis_cache.rb', line 75

def reset(name = nil)
  keys = _all_keys(name)
  redis.del(*keys) unless keys.empty?
end

#set(id, data) ⇒ Object

set data in the cache



39
40
41
42
43
44
# File 'lib/blix/rest/redis_cache.rb', line 39

def set(id, data)
  params = {}
  params[:ex] = _opts[:expire_secs] if _opts.key?(:expire_secs)
  redis.set(_key(id), _encode(data), **params)
  data
end