Class: RedisHA::Counter

Inherits:
Base
  • Object
show all
Defined in:
lib/redis_ha/crdt/counter.rb

Constant Summary collapse

DEFAULT_MERGE_STRATEGY =

this lambda defines how the individual response hashes are merged the default is to select the maximum value

->(v) { v.map(&:to_i).max }

Instance Attribute Summary

Attributes inherited from Base

#key, #pool

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RedisHA::Base

Instance Method Details

#decr(n = 1) ⇒ Object



12
13
14
15
# File 'lib/redis_ha/crdt/counter.rb', line 12

def decr(n = 1)
  pool.decrby(@key, n)
  true
end

#getObject



22
23
24
25
# File 'lib/redis_ha/crdt/counter.rb', line 22

def get
  versions = pool.get(@key).compact
  merge_strategy[versions].to_i
end

#incr(n = 1) ⇒ Object



7
8
9
10
# File 'lib/redis_ha/crdt/counter.rb', line 7

def incr(n = 1)
  pool.incrby(@key, n)
  true
end

#merge_strategyObject



27
28
29
# File 'lib/redis_ha/crdt/counter.rb', line 27

def merge_strategy
  @merge_strategy || DEFAULT_MERGE_STRATEGY
end

#set(n) ⇒ Object



17
18
19
20
# File 'lib/redis_ha/crdt/counter.rb', line 17

def set(n)
  pool.set(@key, n)
  true
end