Class: Wayfarer::Redis::Counter

Inherits:
Object
  • Object
show all
Includes:
Resettable
Defined in:
lib/wayfarer/redis/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Resettable

#reset!

Constructor Details

#initialize(task) ⇒ Counter

Returns a new instance of Counter.



10
11
12
13
# File 'lib/wayfarer/redis/counter.rb', line 10

def initialize(task)
  @task = task
  @redis_pool = task[:redis_pool]
end

Instance Attribute Details

#taskObject (readonly)

Returns the value of attribute task.



8
9
10
# File 'lib/wayfarer/redis/counter.rb', line 8

def task
  @task
end

Instance Method Details

#decrementObject



27
28
29
30
31
# File 'lib/wayfarer/redis/counter.rb', line 27

def decrement
  redis_pool.with { |conn| conn.decr(redis_key) }.tap do |val|
    @callback&.call if val == 0
  end
end

#incrementObject



23
24
25
# File 'lib/wayfarer/redis/counter.rb', line 23

def increment
  redis_pool.with { |conn| conn.incr(redis_key) }
end

#redis_keyObject



15
16
17
# File 'lib/wayfarer/redis/counter.rb', line 15

def redis_key
  "wayfarer-counter-#{@task.batch}"
end

#valueObject



19
20
21
# File 'lib/wayfarer/redis/counter.rb', line 19

def value
  redis_pool.with { |conn| conn.get(redis_key) }.to_i
end