class Redstruct::Counter
Additional counter operations, using redis string values.
Attributes
@return [Integer] the default increment value of the counter, defaults to 1
@return [Integer] the default maximum value of the counter, leave nil unless you want the cycle effect
Public Class Methods
@param [Integer] by the default increment value @param [Integer, nil] max the default max value of the counter, leave nil unless you want cyclical counters
Redstruct::Struct::new
# File lib/redstruct/counter.rb, line 16 def initialize(by: 1, max: nil, **options) super(**options) @default_increment = by @max = max end
Public Instance Methods
Decrements the counter by the given value. If max is given, will loop around and start again from 0 (will decrement by (current_value + by) % max). @param [Integer, nil] by defaults to @increment, used to increment the underlying counter @param [Integer, nil] max if non-nil, the counter will loop and start over from 0 when it reaches max @example
pry> counter.decrement(by: 10, max: 5) # returns 0, since 10 % 5 == 0 pry> counter.decrement(by: 9, max: 5) # returns -4
@return [Integer] the updated value
# File lib/redstruct/counter.rb, line 68 def decrement(by: nil, max: nil) by ||= @default_increment by = -by.to_i max ||= @max max = -max unless max.nil? return increment(by: by, max: max) end
@return [Integer] the stored value as an integer
Redstruct::String#get
# File lib/redstruct/counter.rb, line 23 def get return super.to_i end
@param [#to_i] value the object to store @return [Integer] the old value before setting it
Redstruct::String#getset
# File lib/redstruct/counter.rb, line 37 def getset(value) return super(value.to_i).to_i end
Increments the counter by the given value. If max is given, will loop around and start again from 0 (will increment by (current_value + by) % max). @param [Integer, nil] by defaults to @increment, used to increment the underlying counter @param [Integer, nil] max if non-nil, the counter will loop and start over from 0 when it reaches max @example
pry> counter.increment(by: 10, max: 5) # returns 0, since 10 % 5 == 0 pry> counter.increment(by: 9, max: 5) # returns 4
@return [Integer] the updated value
# File lib/redstruct/counter.rb, line 48 def increment(by: nil, max: nil) by ||= @default_increment max ||= @max value = if max.nil? self.connection.incrby(@key, by.to_i).to_i else ring_increment_script(keys: @key, argv: [by.to_i, max.to_i]).to_i end return value end
@!visibility private
Redstruct::Struct#inspectable_attributes
# File lib/redstruct/counter.rb, line 92 def inspectable_attributes super.merge(max: @max, by: @default_increment) end
Sets the new value, converting it to int first @see Redstruct::String#set
@param [#to_i] value the updated counter value @return [Boolean] True if set, false otherwise
Redstruct::String#set
# File lib/redstruct/counter.rb, line 31 def set(value, **options) super(value.to_i, **options) end