Class: RedisRds::Counter

Inherits:
String show all
Defined in:
lib/redis_rds/counter.rb

Constant Summary collapse

RING_INCREMENT_SCRIPT =

Disable warning since one cannot freeze multiline heredocs… rubocop: disable Style/MutableConstant

<<~LUA
  local by = tonumber(ARGV[1])
  local max = tonumber(ARGV[2])
  local current = redis.call('get', KEYS[1])
  local value = current and tonumber(current) or 0

  value = (value + by) % max
  redis.call('set', KEYS[1], value)

  return value
LUA

Instance Attribute Summary

Attributes inherited from Object

#redis_key

Instance Method Summary collapse

Methods inherited from String

#append, #decr, #decrby, #get, #incr, #length, #set, #setex, #setnx

Methods inherited from Object

configure, #connection, connection, #delete, #dump, #exists?, #expire, #expireat, flushdb, #initialize, #namespace, #persist, #pttl, #ttl, #type

Constructor Details

This class inherits a constructor from RedisRds::Object

Instance Method Details

#incrby(increment, max: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/redis_rds/counter.rb', line 17

def incrby(increment, max: nil)
  value = if max.nil?
    super(increment)
  else
    ring_increment_script(increment.to_i, max.to_i).to_i
  end

  return value
end