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
- #decr(field) ⇒ Object (also: #decrement)
- #decrby(field, decrement) ⇒ Object (also: #decrementby)
-
#delete! ⇒ Boolean
(also: #clear)
Deletes the entire Redis key.
-
#exists?(check_size: true) ⇒ Boolean
Checks if the calling object’s key exists in Redis.
-
#expire(ttl = nil) ⇒ Object
Sets a timeout on key.
-
#field_count ⇒ Integer
(also: #size)
Returns the number of fields in the main object hash.
- #hget(field) ⇒ Object
-
#hgetall ⇒ Object
(also: #all)
For parity with RedisType#hgetall.
- #hkeys ⇒ Object
- #hmset(hsh = {}) ⇒ Object
-
#hset(field, value) ⇒ Object
The number of fields that were added to the hash.
- #hstrlen(field) ⇒ Object (also: #hstrlength)
- #hvals ⇒ Object
- #incr(field) ⇒ Object (also: #increment)
- #incrby(field, increment) ⇒ Object (also: #incrementby)
- #incrbyfloat(field, increment) ⇒ Object (also: #incrementbyfloat)
- #key?(field) ⇒ Boolean (also: #has_key?)
- #move(db) ⇒ Object
-
#prefix ⇒ String
Retrieves the prefix for the current instance by delegating to its class.
-
#realttl ⇒ Integer
Retrieves the remaining time to live (TTL) for the object’s Redis key.
- #redistype ⇒ Object
-
#remove_field(field) ⇒ Integer
(also: #remove)
Removes a field from the hash stored at the Redis key.
-
#rename(newkey) ⇒ Object
Parity with RedisType#rename.
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
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
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.
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_count ⇒ Integer Also known as: size
Returns the number of fields in the main object hash
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 |
#hgetall ⇒ Object 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 |
#hkeys ⇒ Object
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.
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 |
#hvals ⇒ Object
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?
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 |
#prefix ⇒ String
Retrieves the prefix for the current instance by delegating to its class.
102 103 104 |
# File 'lib/familia/horreum/commands.rb', line 102 def prefix self.class.prefix end |
#realttl ⇒ Integer
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`.
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 |
#redistype ⇒ Object
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.
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 |