Class: Familia::Counter

Inherits:
StringKey show all
Defined in:
lib/familia/data_type/types/counter.rb

Instance Attribute Summary collapse

Attributes included from Settings

#current_key_version, #default_expiration, #delim, #encryption_hkdf_salt, #encryption_hkdf_salt_history, #encryption_keys, #encryption_personalization, #encryption_personalization_history, #logical_database, #prefix, #raise_on_unsaved_parent_write, #schema_path, #schema_validator, #schemas, #strict_write_order, #suffix, #transaction_mode

Instance Method Summary collapse

Methods included from DataType::ScalarBase

#scalar_type?

Methods included from Features::Autoloader

autoload_files, included, normalize_to_config_name

Methods included from DataType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value, #strip_legacy_json_encoding

Methods included from DataType::DatabaseCommands

#current_expiration, #delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #type

Methods included from DataType::Connection

#dbclient, #dbkey, #uri

Methods included from Familia::Connection::Behavior

#connect, #create_dbclient, #multi, #normalize_uri, #pipeline, #pipelined, #transaction, #uri=, #url, #url=, #with_connection_scope

Methods included from Settings

#configure, #default_suffix, #dirty_write_warnings, #dirty_write_warnings=, #pipelined_mode, #pipelined_mode=

Methods included from Base

add_feature, #as_json, #expired?, #expires?, find_feature, #generate_id, #to_json, #to_s, #ttl, #update_expiration, #uuid

Constructor Details

#initialize(*args) ⇒ Counter

Returns a new instance of Counter.



7
8
9
10
# File 'lib/familia/data_type/types/counter.rb', line 7

def initialize(*args)
  super
  @opts[:default] ||= 0
end

Instance Attribute Details

#features_enabled ⇒ Object (readonly) Originally defined in module Features

Returns the value of attribute features_enabled.

#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

#parent ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute parent.

#prefix ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute prefix.

#suffix ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute suffix.

#uri(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute uri.

Instance Method Details

#atomic_increment_and_get(amount = 1) ⇒ Object



51
52
53
# File 'lib/familia/data_type/types/counter.rb', line 51

def atomic_increment_and_get(amount = 1)
  incrementby(amount)
end

#increment_if_less_than(threshold, amount = 1) ⇒ Integer, false

Increments the counter by +amount+ only while its value is below +threshold+, atomically, server-side.

Parameters:

  • threshold (Integer) —

    the exclusive upper bound checked before incrementing

  • amount (Integer) (defaults to: 1) —

    the increment

Returns:

  • (Integer, false) —

    the new value, or false when the counter had already reached +threshold+

Raises:

  • (Familia::OperationModeError) —

    inside a transaction or pipeline, where the EVAL is only queued and returns a Redis::Future. Callers branch on the verdict (for example to enforce a rate limit), and a truthy Future would report the increment as allowed before it runs.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/familia/data_type/types/counter.rb', line 32

def increment_if_less_than(threshold, amount = 1)
  Familia.assert_replies_available!('Counter#increment_if_less_than')

  lua = <<~LUA
    local current = tonumber(redis.call('GET', KEYS[1]) or '0')
    if current < tonumber(ARGV[1]) then
      return redis.call('INCRBY', KEYS[1], ARGV[2])
    end
    return nil
  LUA
  result = dbclient.eval(lua, keys: [dbkey], argv: [threshold, amount])
  if result
    update_expiration
    result.to_i
  else
    false
  end
end

#reset(val = 0) ⇒ Boolean, Redis::Future

Enhanced counter semantics

Returns:

  • (Boolean, Redis::Future) —

    true when SET replied OK. Inside a transaction or pipeline, the SET Future (resolves to "OK").



16
17
18
# File 'lib/familia/data_type/types/counter.rb', line 16

def reset(val = 0)
  Familia.transform_reply(set(val)) { |reply| reply.to_s.eql?('OK') }
end

#value ⇒ Integer, Redis::Future

Returns the counter value. Inside a transaction or pipeline, the GET Future (resolves to the raw value).

Returns:

  • (Integer, Redis::Future) —

    the counter value. Inside a transaction or pipeline, the GET Future (resolves to the raw value).



62
63
64
# File 'lib/familia/data_type/types/counter.rb', line 62

def value
  Familia.transform_reply(super, &:to_i)
end

#value=(val) ⇒ Object

Override to ensure integer serialization



56
57
58
# File 'lib/familia/data_type/types/counter.rb', line 56

def value=(val)
  super(val.to_i)
end