Class: Redis::Counter

Inherits:
BaseObject show all
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

Attributes inherited from BaseObject

#key, #options

Instance Method Summary collapse

Methods inherited from BaseObject

#allow_expiration, #as_json, #redis, #set_expiration, #to_hash, #to_json

Methods included from Helpers::CoreCommands

#delete, #exists, #exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #serializer, #ttl, #type, #unmarshal

Constructor Details

#initialize(key, *args) ⇒ Counter

Returns a new instance of Counter.

Raises:

  • (ArgumentError)


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

def initialize(key, *args)
  super(key, *args)
  @options[:start] ||= @options[:default] || 0
  raise ArgumentError, "Marshalling redis counters does not make sense" if @options[:marshal]
  redis.setnx(key, @options[:start]) unless @options[:start] == 0 || @options[:init] === false
end

Instance Method Details

#decrbyfloat(by = 1.0, &block) ⇒ Object

Decrement a floating point counter atomically. Redis uses separate API’s to interact with integers vs floats.



102
103
104
105
106
107
# File 'lib/redis/counter.rb', line 102

def decrbyfloat(by=1.0, &block)
  allow_expiration do
    val = redis.incrbyfloat(key, -by).to_f
    block_given? ? rewindable_block(:incrbyfloat, by, val, &block) : val
  end
end

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

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.



82
83
84
85
86
87
# File 'lib/redis/counter.rb', line 82

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

#getset(to = ) ⇒ Object

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.



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

def getset(to=options[:start])
  redis.getset(key, to.to_i).to_i
end

#incrbyfloat(by = 1.0, &block) ⇒ Object

Increment a floating point counter atomically. Redis uses separate API’s to interact with integers vs floats.



93
94
95
96
97
98
# File 'lib/redis/counter.rb', line 93

def incrbyfloat(by=1.0, &block)
  allow_expiration do
    val = redis.incrbyfloat(key, by).to_f
    block_given? ? rewindable_block(:decrbyfloat, by, val, &block) : val
  end
end

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

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.



68
69
70
71
72
73
# File 'lib/redis/counter.rb', line 68

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

#mObject

Math ops This needs to handle +/- either actual integers or other Redis::Counters



121
122
123
124
125
126
127
# File 'lib/redis/counter.rb', line 121

%w(+ - == < > <= >=).each do |m|
  class_eval <<-EndOverload
    def #{m}(what)
      value.to_i #{m} what.to_i
    end
  EndOverload
end

#nil?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/redis/counter.rb', line 114

def nil?
  !redis.exists?(key)
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).



23
24
25
26
27
28
# File 'lib/redis/counter.rb', line 23

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

#to_fObject

Like .value but casts to float since Redis addresses these differently.



59
60
61
# File 'lib/redis/counter.rb', line 59

def to_f
  redis.get(key).to_f
end

#to_sObject

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



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

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.



42
43
44
# File 'lib/redis/counter.rb', line 42

def value
  redis.get(key).to_i
end

#value=(val) ⇒ Object Also known as: set



47
48
49
50
51
52
53
54
55
# File 'lib/redis/counter.rb', line 47

def value=(val)
  allow_expiration do
    if val.nil?
      delete
    else
      redis.set key, val
    end
  end
end