Module: Familia::Horreum::Commands

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

Overview

Methods that call Redis commands (InstanceMethods)

NOTE: There is no hgetall for Horreum. This is because Horreum is a single hash in Redis 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

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



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

def decr(field)
  redis.hdecr field
end

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



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

def decrby(field, decrement)
  redis.decrby rediskey(suffix), field, decrement
end

#delete!Boolean Also known as: clear

Deletes the entire Redis key

Returns:

  • (Boolean)

    true if the key was deleted, false otherwise



177
178
179
180
181
# File 'lib/familia/horreum/commands.rb', line 177

def delete!
  Familia.trace :DELETE!, redis, redisuri, caller(1..1) if Familia.debug?
  ret = redis.del rediskey
  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.redis.exists?(rediskey)
  return key_exists unless check_size
  key_exists && !size.zero?
end

#expire(ttl = 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(ttl = nil)
  ttl ||= self.class.ttl
  Familia.trace :EXPIRE, redis, ttl, caller(1..1) if Familia.debug?
  redis.expire rediskey, ttl.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
  redis.hlen rediskey
end

#hget(field) ⇒ Object



113
114
115
116
# File 'lib/familia/horreum/commands.rb', line 113

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

#hgetallObject Also known as: all

For parity with RedisType#hgetall



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

def hgetall
  Familia.trace :HGETALL, redis, redisuri, caller(1..1) if Familia.debug?
  redis.hgetall rediskey(suffix)
end

#hkeysObject



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

def hkeys
  Familia.trace :HKEYS, redis, 'redisuri', caller(1..1) if Familia.debug?
  redis.hkeys rediskey(suffix)
end

#hmset(hsh = {}) ⇒ Object



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

def hmset(hsh={})
  hsh ||= self.to_h
  Familia.trace :HMSET, redis, hsh, caller(1..1) if Familia.debug?
  redis.hmset rediskey(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.



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

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

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



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

def hstrlen(field)
  redis.hstrlen rediskey(suffix), field
end

#hvalsObject



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

def hvals
  redis.hvals rediskey(suffix)
end

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



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

def incr(field)
  redis.hincrby rediskey(suffix), field, 1
end

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



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

def incrby(field, increment)
  redis.hincrby rediskey(suffix), field, increment
end

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



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

def incrbyfloat(field, increment)
  redis.hincrbyfloat rediskey(suffix), field, increment
end

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

Returns:

  • (Boolean)


170
171
172
# File 'lib/familia/horreum/commands.rb', line 170

def key?(field)
  redis.hexists rediskey(suffix), field
end

#move(db) ⇒ Object



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

def move(db)
  redis.move rediskey, db
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.



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

def prefix
  self.class.prefix
end

#realttlInteger

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

This method accesses the ovjects Redis client to obtain the TTL of ‘rediskey`. 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 realttl
  Familia.trace :REALTTL, redis, redisuri, caller(1..1) if Familia.debug?
  redis.ttl rediskey
end

#redistypeObject



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

def redistype
  Familia.trace :REDISTYPE, redis, redisuri, caller(1..1) if Familia.debug?
  redis.type rediskey(suffix)
end

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

Removes a field from the hash stored at the Redis key.

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, redis, field, caller(1..1) if Familia.debug?
  redis.hdel rediskey, field
end

#rename(newkey) ⇒ Object

Parity with RedisType#rename



92
93
94
95
# File 'lib/familia/horreum/commands.rb', line 92

def rename(newkey)
  Familia.trace :RENAME, redis, "#{rediskey} -> #{newkey}", caller(1..1) if Familia.debug?
  redis.rename rediskey, newkey
end