Class: Redis::Counter

Inherits:
Object
  • Object
show all
Includes:
Helpers::CoreCommands
Defined in:
lib/redis/counter.rb

Overview

Class representing a Redis counter. This functions like a proxy class, in that you can say @object.counter_name to get the value and then directly, or you can use the counter :foo class method in your class to define a counter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CoreCommands

#delete, #exists?, #expire, #expireat, #move, #rename, #renamenx, #type

Constructor Details

#initialize(key, *args) ⇒ Counter

Returns a new instance of Counter.



14
15
16
17
18
19
20
# File 'lib/redis/counter.rb', line 14

def initialize(key, *args)
  @key = key
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @redis = args.first || $redis
  @options[:start] ||= 0
  @redis.setnx(key, @options[:start]) unless @options[:start] == 0 || @options[:init] === false
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/redis/counter.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/redis/counter.rb', line 13

def options
  @options
end

#redisObject (readonly)

Returns the value of attribute redis.



13
14
15
# File 'lib/redis/counter.rb', line 13

def redis
  @redis
end

Instance Method Details

#decrement(by = 1, &block) ⇒ Object Also known as: decr

Decrement the counter atomically and return the new value. If passed a block, that block will be evaluated with the new value of the counter as an argument. If the block returns nil or throws an exception, the counter will automatically be incremented to its previous value. This method is aliased as incr() for brevity.



56
57
58
59
# File 'lib/redis/counter.rb', line 56

def decrement(by=1, &block)
  val = redis.decrby(key, by).to_i
  block_given? ? rewindable_block(:increment, val, &block) : val
end

#increment(by = 1, &block) ⇒ Object Also known as: incr

Increment the counter atomically and return the new value. If passed a block, that block will be evaluated with the new value of the counter as an argument. If the block returns nil or throws an exception, the counter will automatically be decremented to its previous value. This method is aliased as incr() for brevity.



45
46
47
48
# File 'lib/redis/counter.rb', line 45

def increment(by=1, &block)
  val = redis.incrby(key, by).to_i
  block_given? ? rewindable_block(:decrement, val, &block) : val
end

#nil?Boolean

Returns:

  • (Boolean)


67
# File 'lib/redis/counter.rb', line 67

def nil?; value.nil? end

#reset(to = ) ⇒ Object

Reset the counter to its starting value. Not atomic, so use with care. Normally only useful if you’re discarding all sub-records associated with a parent and starting over (for example, restarting a game and disconnecting all players).



26
27
28
29
# File 'lib/redis/counter.rb', line 26

def reset(to=options[:start])
  redis.set key, to.to_i
  true  # hack for redis-rb regression
end

#to_sObject Also known as: to_str

Proxy methods to help make @object.counter == 10 work



64
# File 'lib/redis/counter.rb', line 64

def to_s; value.to_s; end

#valueObject Also known as: get, to_i

Returns the current value of the counter. Normally just calling the counter will lazily fetch the value, and only update it if increment or decrement is called. This forces a network call to redis-server to get the current value.



35
36
37
# File 'lib/redis/counter.rb', line 35

def value
  redis.get(key).to_i
end