Class: Redstruct::Counter

Inherits:
String show all
Defined in:
lib/redstruct/counter.rb

Overview

Additional counter operations, using redis string values.

Instance Attribute Summary collapse

Attributes inherited from Struct

#key

Instance Method Summary collapse

Methods inherited from String

#delete_if_equals, #delete_if_equals_script, #length, #slice

Methods inherited from Struct

#delete, #dump, #exists?, #expire, #expire_at, #persist, #restore, #ttl, #type

Constructor Details

#initialize(by: 1, max: nil, **options) ⇒ Counter

Returns a new instance of Counter

Parameters:

  • by (Integer)

    the default increment value

  • max (Integer, nil)

    the default max value of the counter, leave nil unless you want cyclical counters



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

def initialize(by: 1, max: nil, **options)
  super(**options)
  @default_increment = by
  @max = max
end

Instance Attribute Details

#default_incrementInteger (readonly)

Returns the default increment value of the counter, defaults to 1

Returns:

  • (Integer)

    the default increment value of the counter, defaults to 1



9
10
11
# File 'lib/redstruct/counter.rb', line 9

def default_increment
  @default_increment
end

#maxInteger (readonly)

Returns the default maximum value of the counter, leave nil unless you want the cycle effect

Returns:

  • (Integer)

    the default maximum value of the counter, leave nil unless you want the cycle effect



12
13
14
# File 'lib/redstruct/counter.rb', line 12

def max
  @max
end

Instance Method Details

#decrement(by: nil, max: nil) ⇒ Integer

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).

Examples:

pry> counter.decrement(by: 10, max: 5) # returns 0, since 10 % 5 == 0
pry> counter.decrement(by: 9, max: 5) # returns -4

Parameters:

  • by (Integer, nil)

    defaults to @increment, used to increment the underlying counter

  • max (Integer, nil)

    if non-nil, the counter will loop and start over from 0 when it reaches max

Returns:

  • (Integer)

    the updated value



68
69
70
71
72
73
74
75
76
# 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

#getInteger

Returns the stored value as an integer

Returns:

  • (Integer)

    the stored value as an integer



23
24
25
# File 'lib/redstruct/counter.rb', line 23

def get
  return super.to_i
end

#getset(value) ⇒ Integer

Returns the old value before setting it

Parameters:

  • value (#to_i)

    the object to store

Returns:

  • (Integer)

    the old value before setting it



37
38
39
# File 'lib/redstruct/counter.rb', line 37

def getset(value)
  return super(value.to_i).to_i
end

#increment(by: nil, max: nil) ⇒ Integer

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).

Examples:

pry> counter.increment(by: 10, max: 5) # returns 0, since 10 % 5 == 0
pry> counter.increment(by: 9, max: 5) # returns 4

Parameters:

  • by (Integer, nil)

    defaults to @increment, used to increment the underlying counter

  • max (Integer, nil)

    if non-nil, the counter will loop and start over from 0 when it reaches max

Returns:

  • (Integer)

    the updated value



48
49
50
51
52
53
54
55
56
57
58
59
# 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

#set(value, **options) ⇒ Boolean

Sets the new value, converting it to int first

Parameters:

  • value (#to_i)

    the updated counter value

Returns:

  • (Boolean)

    True if set, false otherwise

See Also:



31
32
33
# File 'lib/redstruct/counter.rb', line 31

def set(value, **options)
  super(value.to_i, **options)
end