Class: Redis::Counter

Inherits:
BaseObject 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, #marshal, #move, #persist, #rename, #renamenx, #sort, #ttl, #type, #unmarshal

Methods inherited from BaseObject

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

Constructor Details

#initialize(key, *args) ⇒ Counter

Returns a new instance of Counter.

Raises:

  • (ArgumentError)

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

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 Attribute Details

#keyObject (readonly)

Returns the value of attribute key


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

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
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.


106
107
108
109
110
111
# File 'lib/redis/counter.rb', line 106

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.


86
87
88
89
90
91
# File 'lib/redis/counter.rb', line 86

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 = options[:start]) ⇒ 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.


38
39
40
# File 'lib/redis/counter.rb', line 38

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.


97
98
99
100
101
102
# File 'lib/redis/counter.rb', line 97

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.


72
73
74
75
76
77
# File 'lib/redis/counter.rb', line 72

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


125
126
127
128
129
130
131
# File 'lib/redis/counter.rb', line 125

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

#nil?Boolean

Returns:

  • (Boolean)

118
119
120
# File 'lib/redis/counter.rb', line 118

def nil?
  !redis.exists(key)
end

#reset(to = options[:start]) ⇒ 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).


27
28
29
30
31
32
# File 'lib/redis/counter.rb', line 27

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.


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

def to_f
  redis.get(key).to_f
end

#to_sObject

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


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

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.


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

def value
  redis.get(key).to_i
end

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


51
52
53
54
55
56
57
58
59
# File 'lib/redis/counter.rb', line 51

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