Class: Progressrus::Store::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/progressrus/store/redis.rb

Constant Summary collapse

BACKEND_EXCEPTIONS =
[ ::Redis::BaseError ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, prefix: "progressrus", interval: 1, now: Time.now) ⇒ Redis

Returns a new instance of Redis.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



6
7
8
# File 'lib/progressrus/store/redis.rb', line 6

def interval
  @interval
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/progressrus/store/redis.rb', line 6

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/progressrus/store/redis.rb', line 6

def prefix
  @prefix
end

#redisObject (readonly)

Returns the value of attribute redis.



6
7
8
# File 'lib/progressrus/store/redis.rb', line 6

def redis
  @redis
end

Instance Method Details

#find(scope, id) ⇒ Object



40
41
42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
38
# 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