Module: Familia::Horreum::Commands

Included in:
Familia::Horreum
Defined in:
lib/familia/horreum/commands.rb

Overview

Methods that call Database commands (InstanceMethods)

NOTE: There is no hgetall for Horreum. This is because Horreum is a single hash in Database that we aren’t meant to have be working on in memory for more than, making changes -> committing. To emphasize this, instead of “refreshing” the object with hgetall, just load the object again.

Instance Method Summary collapse

Instance Method Details

#current_expirationInteger

Retrieves the remaining time to live (TTL) for the object’s dbkey.

This method accesses the ovjects Database client to obtain the TTL of dbkey. If debugging is enabled, it logs the TTL retrieval operation using Familia.trace.

Returns:

  • (Integer)

    The TTL of the key in seconds. Returns -1 if the key does not exist or has no associated expire time.



71
72
73
74
# File 'lib/familia/horreum/commands.rb', line 71

def current_expiration
  Familia.trace :CURRENT_EXPIRATION, dbclient, uri, caller(1..1) if Familia.debug?
  dbclient.ttl dbkey
end

#datatypeObject



86
87
88
89
# File 'lib/familia/horreum/commands.rb', line 86

def datatype
  Familia.trace :DATATYPE, dbclient, uri, caller(1..1) if Familia.debug?
  dbclient.type dbkey(suffix)
end

#decr(field) ⇒ Object Also known as: decrement



155
156
157
# File 'lib/familia/horreum/commands.rb', line 155

def decr(field)
  dbclient.hdecr field
end

#decrby(field, decrement) ⇒ Object Also known as: decrementby



150
151
152
# File 'lib/familia/horreum/commands.rb', line 150

def decrby(field, decrement)
  dbclient.decrby dbkey(suffix), field, decrement
end

#delete!Boolean Also known as: clear

Deletes the entire dbkey

Returns:

  • (Boolean)

    true if the key was deleted, false otherwise



172
173
174
175
176
# File 'lib/familia/horreum/commands.rb', line 172

def delete!
  Familia.trace :DELETE!, dbclient, uri, caller(1..1) if Familia.debug?
  ret = dbclient.del dbkey
  ret.positive?
end

#exists?(check_size: true) ⇒ Boolean

Note:

The default behavior maintains backward compatibility by treating empty hashes as non-existent. Use check_size: false for pure key existence checking.

Checks if the calling object’s key exists in Redis.

Examples:

Check existence with size validation (default behavior)

some_object.exists?                    # => false for empty hashes
some_object.exists?(check_size: true)  # => false for empty hashes

Check existence only

some_object.exists?(check_size: false)  # => true for empty hashes

Parameters:

  • check_size (Boolean) (defaults to: true)

    When true (default), also verifies the hash has a non-zero size. When false, only checks key existence regardless of content.

Returns:

  • (Boolean)

    Returns true if the key exists in Redis. When check_size is true, also requires the hash to have at least one field.



41
42
43
44
45
# File 'lib/familia/horreum/commands.rb', line 41

def exists?(check_size: true)
  key_exists = self.class.dbclient.exists?(dbkey)
  return key_exists unless check_size
  key_exists && !size.zero?
end

#expire(default_expiration = nil) ⇒ Object

Sets a timeout on key. After the timeout has expired, the key will automatically be deleted. Returns 1 if the timeout was set, 0 if key does not exist or the timeout could not be set.



58
59
60
61
62
# File 'lib/familia/horreum/commands.rb', line 58

def expire(default_expiration = nil)
  default_expiration ||= self.class.default_expiration
  Familia.trace :EXPIRE, dbclient, default_expiration, caller(1..1) if Familia.debug?
  dbclient.expire dbkey, default_expiration.to_i
end

#field_countInteger Also known as: size

Returns the number of fields in the main object hash

Returns:

  • (Integer)

    number of fields



49
50
51
# File 'lib/familia/horreum/commands.rb', line 49

def field_count
  dbclient.hlen dbkey
end

#hget(field) ⇒ Object



108
109
110
111
# File 'lib/familia/horreum/commands.rb', line 108

def hget(field)
  Familia.trace :HGET, dbclient, field, caller(1..1) if Familia.debug?
  dbclient.hget dbkey(suffix), field
end

#hgetallObject Also known as: all

For parity with DataType#hgetall



102
103
104
105
# File 'lib/familia/horreum/commands.rb', line 102

def hgetall
  Familia.trace :HGETALL, dbclient, uri, caller(1..1) if Familia.debug?
  dbclient.hgetall dbkey(suffix)
end

#hkeysObject



126
127
128
129
# File 'lib/familia/horreum/commands.rb', line 126

def hkeys
  Familia.trace :HKEYS, dbclient, 'uri', caller(1..1) if Familia.debug?
  dbclient.hkeys dbkey(suffix)
end

#hmset(hsh = {}) ⇒ Object



120
121
122
123
124
# File 'lib/familia/horreum/commands.rb', line 120

def hmset(hsh={})
  hsh ||= self.to_h
  Familia.trace :HMSET, dbclient, hsh, caller(1..1) if Familia.debug?
  dbclient.hmset dbkey(suffix), hsh
end

#hset(field, value) ⇒ Object

Returns The number of fields that were added to the hash. If the field already exists, this will return 0.

Returns:

  • The number of fields that were added to the hash. If the field already exists, this will return 0.



115
116
117
118
# File 'lib/familia/horreum/commands.rb', line 115

def hset(field, value)
  Familia.trace :HSET, dbclient, field, caller(1..1) if Familia.debug?
  dbclient.hset dbkey, field, value
end

#hstrlen(field) ⇒ Object Also known as: hstrlength



160
161
162
# File 'lib/familia/horreum/commands.rb', line 160

def hstrlen(field)
  dbclient.hstrlen dbkey(suffix), field
end

#hvalsObject



131
132
133
# File 'lib/familia/horreum/commands.rb', line 131

def hvals
  dbclient.hvals dbkey(suffix)
end

#incr(field) ⇒ Object Also known as: increment



135
136
137
# File 'lib/familia/horreum/commands.rb', line 135

def incr(field)
  dbclient.hincrby dbkey(suffix), field, 1
end

#incrby(field, increment) ⇒ Object Also known as: incrementby



140
141
142
# File 'lib/familia/horreum/commands.rb', line 140

def incrby(field, increment)
  dbclient.hincrby dbkey(suffix), field, increment
end

#incrbyfloat(field, increment) ⇒ Object Also known as: incrementbyfloat



145
146
147
# File 'lib/familia/horreum/commands.rb', line 145

def incrbyfloat(field, increment)
  dbclient.hincrbyfloat dbkey(suffix), field, increment
end

#key?(field) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


165
166
167
# File 'lib/familia/horreum/commands.rb', line 165

def key?(field)
  dbclient.hexists dbkey(suffix), field
end

#move(logical_database) ⇒ Object



21
22
23
# File 'lib/familia/horreum/commands.rb', line 21

def move(logical_database)
  dbclient.move dbkey, logical_database
end

#prefixString

Retrieves the prefix for the current instance by delegating to its class.

Examples:

instance.prefix

Returns:

  • (String)

    The prefix associated with the class of the current instance.



97
98
99
# File 'lib/familia/horreum/commands.rb', line 97

def prefix
  self.class.prefix
end

#remove_field(field) ⇒ Integer Also known as: remove

Removes a field from the hash stored at the dbkey.

Parameters:

  • field (String)

    The field to remove from the hash.

Returns:

  • (Integer)

    The number of fields that were removed from the hash (0 or 1).



80
81
82
83
# File 'lib/familia/horreum/commands.rb', line 80

def remove_field(field)
  Familia.trace :HDEL, dbclient, field, caller(1..1) if Familia.debug?
  dbclient.hdel dbkey, field
end