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



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

def decr(field)
  redis.hdecr field
end

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



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

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



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

def delete!
  Familia.trace :DELETE!, redis, redisuri, caller(1..1) if Familia.debug?
  ret = redis.del rediskey
  ret.positive?
end

#exists?Boolean

Checks if the calling object’s key exists in Redis and has a non-zero size.

This method retrieves the Redis URI associated with the calling object’s class using ‘Familia.redis_uri_by_class`. It then checks if the specified key exists in Redis and that its size is not zero. If debugging is enabled, it logs the existence check using `Familia.trace`.

Examples:

if some_object.exists?
  # perform action
end

Returns:

  • (Boolean)

    Returns ‘true` if the key exists in Redis and its size is not zero, otherwise `false`.



37
38
39
40
41
# File 'lib/familia/horreum/commands.rb', line 37

def exists?
  true_or_false = self.class.redis.exists?(rediskey) && !size.zero?
  Familia.trace :EXISTS, redis, "#{key} #{true_or_false.inspect}", caller(1..1) if Familia.debug?
  true_or_false
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.



54
55
56
57
58
# File 'lib/familia/horreum/commands.rb', line 54

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



45
46
47
# File 'lib/familia/horreum/commands.rb', line 45

def field_count
  redis.hlen rediskey
end

#hget(field) ⇒ Object



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

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



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

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

#hkeysObject



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

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

#hmset(hsh = {}) ⇒ Object



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

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.



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

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



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

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

#hvalsObject



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

def hvals
  redis.hvals rediskey(suffix)
end

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



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

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

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



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

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

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



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

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

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

Returns:

  • (Boolean)


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

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.



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

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.



67
68
69
70
# File 'lib/familia/horreum/commands.rb', line 67

def realttl
  Familia.trace :REALTTL, redis, redisuri, caller(1..1) if Familia.debug?
  redis.ttl rediskey
end

#redistypeObject



82
83
84
85
# File 'lib/familia/horreum/commands.rb', line 82

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



76
77
78
79
# File 'lib/familia/horreum/commands.rb', line 76

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



88
89
90
91
# File 'lib/familia/horreum/commands.rb', line 88

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