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? ⇒ Boolean
Checks if the calling object’s key exists in Redis and has a non-zero size.
-
#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
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
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`.
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_count ⇒ Integer Also known as: size
Returns the number of fields in the main object hash
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 |
#hgetall ⇒ Object 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 |
#hkeys ⇒ Object
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.
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 |
#hvals ⇒ Object
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?
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 |
#prefix ⇒ String
Retrieves the prefix for the current instance by delegating to its class.
98 99 100 |
# File 'lib/familia/horreum/commands.rb', line 98 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`.
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 |
#redistype ⇒ Object
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 |