Class: Redis::Counter
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::Counter
- 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)
-
- (Object) key
readonly
Returns the value of attribute key.
-
- (Object) options
readonly
Returns the value of attribute options.
Instance Method Summary (collapse)
-
- (Object) decrement(by = 1, &block)
(also: #decr)
Decrement the counter atomically and return the new value.
-
- (Object) getset(to = options[:start])
Reset the counter to its starting value, and return previous value.
-
- (Object) increment(by = 1, &block)
(also: #incr)
Increment the counter atomically and return the new value.
-
- (Counter) initialize(key, *args)
constructor
A new instance of Counter.
- - (Boolean) nil?
-
- (Object) reset(to = options[:start])
Reset the counter to its starting value.
-
- (Object) to_s
Proxy methods to help make @object.counter == 10 work.
-
- (Object) value
(also: #get, #to_i)
Returns the current value of the counter.
Methods included from Helpers::CoreCommands
#delete, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #sort, #ttl, #type
Methods inherited from BaseObject
Constructor Details
- (Counter) initialize(key, *args)
A new instance of Counter
16 17 18 19 20 |
# File 'lib/redis/counter.rb', line 16 def initialize(key, *args) super(key, *args) @options[:start] ||= 0 redis.setnx(key, @options[:start]) unless @options[:start] == 0 || @options[:init] === false end |
Instance Attribute Details
- (Object) key (readonly)
Returns the value of attribute key
15 16 17 |
# File 'lib/redis/counter.rb', line 15 def key @key end |
- (Object) options (readonly)
Returns the value of attribute options
15 16 17 |
# File 'lib/redis/counter.rb', line 15 def @options end |
Instance Method Details
- (Object) decrement(by = 1, &block) 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 decr() for brevity.
64 65 66 67 |
# File 'lib/redis/counter.rb', line 64 def decrement(by=1, &block) val = redis.decrby(key, by).to_i block_given? ? rewindable_block(:increment, by, val, &block) : val end |
- (Object) getset(to = options[:start])
Reset the counter to its starting value, and return previous value. Use this to “reap” the counter and save it somewhere else. This is atomic in that no increments or decrements are lost if you process the returned value.
35 36 37 |
# File 'lib/redis/counter.rb', line 35 def getset(to=[:start]) redis.getset(key, to.to_i).to_i end |
- (Object) increment(by = 1, &block) 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.
53 54 55 56 |
# File 'lib/redis/counter.rb', line 53 def increment(by=1, &block) val = redis.incrby(key, by).to_i block_given? ? rewindable_block(:decrement, by, val, &block) : val end |
- (Boolean) nil?
74 |
# File 'lib/redis/counter.rb', line 74 def nil?; value.nil? end |
- (Object) reset(to = options[:start])
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=[:start]) redis.set key, to.to_i true # hack for redis-rb regression end |
- (Object) to_s
Proxy methods to help make @object.counter == 10 work
72 |
# File 'lib/redis/counter.rb', line 72 def to_s; value.to_s; end |
- (Object) value 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.
43 44 45 |
# File 'lib/redis/counter.rb', line 43 def value redis.get(key).to_i end |