Class: Sinatra::RedisCache::Cache
- Inherits:
-
Object
- Object
- Sinatra::RedisCache::Cache
- Defined in:
- lib/sinatra/redis-cache.rb
Instance Method Summary collapse
- #all_keys(params = {with_namespace: true}) ⇒ Object
- #del(keys) ⇒ Object
- #do(key, expires = config.default_expires, block) ⇒ Object
- #flush ⇒ Object
- #get(key) ⇒ Object
- #properties(key) ⇒ Object
- #store(key, object, expires = config.default_expires) ⇒ Object
- #ttl(key) ⇒ Object
Instance Method Details
#all_keys(params = {with_namespace: true}) ⇒ Object
100 101 102 |
# File 'lib/sinatra/redis-cache.rb', line 100 def all_keys(params={with_namespace: true}) redis.keys("#{namespace}:*").map{|k| params[:with_namespace] ? k : key_without_namespace(k) } end |
#del(keys) ⇒ Object
104 105 106 107 |
# File 'lib/sinatra/redis-cache.rb', line 104 def del(keys) debug_log "del #{keys.map{|k| key_without_namespace(k)}}" redis.del(keys) end |
#do(key, expires = config.default_expires, block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sinatra/redis-cache.rb', line 32 def do(key, expires=config.default_expires, block) debug_log "do #{key_without_namespace(key)}" if Sinatra::RedisCache::Config.environments.include? Sinatra::Base.settings.environment try = 0 begin redis.watch(key = key_with_namespace(key)) do |watched| object = get(key) unless object.empty? if object['locked'] raise SinatraRedisCacheKeyLocked end object['object'] else lock_key(key, watched, config.lock_timeout) new_object = block.call store(key, new_object, expires) new_object end end rescue SinatraRedisCacheKeyLocked sleeptime = (((try += 1)*50 + rand(100).to_f)/1000) debug_log "key is locked, waiting #{sleeptime}s for retry ##{try}." sleep sleeptime retry rescue SinatraRedisCacheKeyAlreadyLocked sleeptime = (((try += 1)*50 + rand(100).to_f)/1000) debug_log "failed to obtain lock, waiting #{sleeptime}s for retry ##{try}." sleep sleeptime retry end else # Just run the block without cache if we're not in an allowed environment block.call end end |
#flush ⇒ Object
109 110 111 112 113 |
# File 'lib/sinatra/redis-cache.rb', line 109 def flush unless (keys = all_keys).empty? del(keys) end end |
#get(key) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/sinatra/redis-cache.rb', line 68 def get(key) debug_log "get #{key_without_namespace(key)}" unless (hash = redis.hgetall(key_with_namespace(key))).nil? hash.each{|k,v| hash[k]=deserialize(v)} else false end end |
#properties(key) ⇒ Object
90 91 92 93 94 |
# File 'lib/sinatra/redis-cache.rb', line 90 def properties(key) unless (string = redis.hget(key_with_namespace(key), 'properties')).nil? deserialize(string) end end |
#store(key, object, expires = config.default_expires) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sinatra/redis-cache.rb', line 77 def store(key, object, expires=config.default_expires) debug_log "store #{key_without_namespace(key)}" properties = { created_at: Time.now.utc.to_i } redis.watch(key = key_with_namespace(key)) do |watched| watched.multi do |multi| multi.hset(key, 'object', serialize(object)) multi.hset(key, 'properties', serialize(properties)) multi.hdel(key, 'locked') multi.expire(key, expires) end end end |
#ttl(key) ⇒ Object
96 97 98 |
# File 'lib/sinatra/redis-cache.rb', line 96 def ttl(key) redis.ttl(key_with_namespace(key)) end |