class Progressrus::Store::Redis
Constants
- BACKEND_EXCEPTIONS
Attributes
interval[R]
name[R]
prefix[R]
redis[R]
Public Class Methods
new(instance, prefix: "progressrus", interval: 1, now: Time.now)
click to toggle source
# File lib/progressrus/store/redis.rb, line 8 def initialize(instance, prefix: "progressrus", interval: 1, now: Time.now) @name = :redis @redis = instance @persisted_ats = Hash.new({}) @interval = interval @prefix = prefix end
Public Instance Methods
find(scope, id)
click to toggle source
# File lib/progressrus/store/redis.rb, line 40 def find(scope, id) value = redis.hget(key(scope), id) return unless value Progressrus.new(**deserialize(value)) rescue *BACKEND_EXCEPTIONS => e raise Progressrus::Store::BackendError.new(e) end
flush(scope, id = nil)
click to toggle source
# File lib/progressrus/store/redis.rb, line 49 def flush(scope, id = nil) if id redis.hdel(key(scope), id) else redis.del(key(scope)) end rescue *BACKEND_EXCEPTIONS => e raise Progressrus::Store::BackendError.new(e) end
persist(progress, now: Time.now, force: false, expires_at: nil)
click to toggle source
# File lib/progressrus/store/redis.rb, line 16 def persist(progress, now: Time.now, force: false, expires_at: nil) if outdated?(progress) || force key_for_scope = key(progress.scope) redis.pipelined do redis.hset(key_for_scope, progress.id, progress.to_serializeable.to_json) redis.expireat(key_for_scope, expires_at.to_i) if expires_at end @persisted_ats[progress.scope][progress.id] = now end rescue *BACKEND_EXCEPTIONS => e raise Progressrus::Store::BackendError.new(e) end
scope(scope)
click to toggle source
# File lib/progressrus/store/redis.rb, line 31 def scope(scope) scope = redis.hgetall(key(scope)) scope.each_pair { |id, value| scope[id] = Progressrus.new(**deserialize(value)) } rescue *BACKEND_EXCEPTIONS => e raise Progressrus::Store::BackendError.new(e) end
Private Instance Methods
deserialize(value)
click to toggle source
# File lib/progressrus/store/redis.rb, line 69 def deserialize(value) JSON.parse(value, symbolize_names: true).merge(persisted: true) end
key(scope)
click to toggle source
# File lib/progressrus/store/redis.rb, line 61 def key(scope) if prefix.respond_to?(:call) prefix.call(scope) else "#{prefix}:#{scope.join(":")}" end end
outdated?(progress, now: Time.now)
click to toggle source
# File lib/progressrus/store/redis.rb, line 73 def outdated?(progress, now: Time.now) (now - interval).to_i >= persisted_at(progress).to_i end
persisted_at(progress)
click to toggle source
# File lib/progressrus/store/redis.rb, line 77 def persisted_at(progress) @persisted_ats[progress.scope][progress.id] end