Module: Redis::Commands::Hashes
- Included in:
- Redis::Commands
- Defined in:
- lib/redis/commands/hashes.rb
Instance Method Summary collapse
-
#hdel(key, *fields) ⇒ Integer
Delete one or more hash fields.
-
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
-
#hget(key, field) ⇒ String
Get the value of a hash field.
-
#hgetall(key) ⇒ Hash<String, String>
Get all the fields and values in a hash.
-
#hincrby(key, field, increment) ⇒ Integer
Increment the integer value of a hash field by the given integer number.
-
#hincrbyfloat(key, field, increment) ⇒ Float
Increment the numeric value of a hash field by the given float number.
-
#hkeys(key) ⇒ Array<String>
Get all the fields in a hash.
-
#hlen(key) ⇒ Integer
Get the number of fields in a hash.
-
#hmget(key, *fields, &blk) ⇒ Array<String>
Get the values of all the given hash fields.
-
#hmset(key, *attrs) ⇒ String
Set one or more hash values.
-
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or more random fields from a hash.
-
#hscan(key, cursor, **options) ⇒ String, Array<[String, String]>
Scan a hash.
-
#hscan_each(key, **options, &block) ⇒ Enumerator
Scan a hash.
-
#hset(key, *attrs) ⇒ Integer
Set one or more hash values.
-
#hsetnx(key, field, value) ⇒ Boolean
Set the value of a hash field, only if the field does not exist.
-
#hvals(key) ⇒ Array<String>
Get all the values in a hash.
-
#mapped_hmget(key, *fields) ⇒ Hash
Get the values of all the given hash fields.
-
#mapped_hmset(key, hash) ⇒ String
Set one or more hash values.
Instance Method Details
#hdel(key, *fields) ⇒ Integer
Delete one or more hash fields.
154 155 156 |
# File 'lib/redis/commands/hashes.rb', line 154 def hdel(key, *fields) send_command([:hdel, key, *fields]) end |
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
163 164 165 |
# File 'lib/redis/commands/hashes.rb', line 163 def hexists(key, field) send_command([:hexists, key, field], &Boolify) end |
#hget(key, field) ⇒ String
Get the value of a hash field.
74 75 76 |
# File 'lib/redis/commands/hashes.rb', line 74 def hget(key, field) send_command([:hget, key, field]) end |
#hgetall(key) ⇒ Hash<String, String>
Get all the fields and values in a hash.
207 208 209 |
# File 'lib/redis/commands/hashes.rb', line 207 def hgetall(key) send_command([:hgetall, key], &Hashify) end |
#hincrby(key, field, increment) ⇒ Integer
Increment the integer value of a hash field by the given integer number.
173 174 175 |
# File 'lib/redis/commands/hashes.rb', line 173 def hincrby(key, field, increment) send_command([:hincrby, key, field, increment]) end |
#hincrbyfloat(key, field, increment) ⇒ Float
Increment the numeric value of a hash field by the given float number.
183 184 185 |
# File 'lib/redis/commands/hashes.rb', line 183 def hincrbyfloat(key, field, increment) send_command([:hincrbyfloat, key, field, increment], &Floatify) end |
#hkeys(key) ⇒ Array<String>
Get all the fields in a hash.
191 192 193 |
# File 'lib/redis/commands/hashes.rb', line 191 def hkeys(key) send_command([:hkeys, key]) end |
#hlen(key) ⇒ Integer
Get the number of fields in a hash.
10 11 12 |
# File 'lib/redis/commands/hashes.rb', line 10 def hlen(key) send_command([:hlen, key]) end |
#hmget(key, *fields, &blk) ⇒ Array<String>
Get the values of all the given hash fields.
89 90 91 |
# File 'lib/redis/commands/hashes.rb', line 89 def hmget(key, *fields, &blk) send_command([:hmget, key] + fields, &blk) end |
#hmset(key, *attrs) ⇒ String
Set one or more hash values.
50 51 52 |
# File 'lib/redis/commands/hashes.rb', line 50 def hmset(key, *attrs) send_command([:hmset, key] + attrs) end |
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or more random fields from a hash.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/redis/commands/hashes.rb', line 136 def hrandfield(key, count = nil, withvalues: false, with_values: withvalues) if with_values && count.nil? raise ArgumentError, "count argument must be specified" end args = [:hrandfield, key] args << count if count args << "WITHVALUES" if with_values parser = Pairify if with_values send_command(args, &parser) end |
#hscan(key, cursor, **options) ⇒ String, Array<[String, String]>
Scan a hash
222 223 224 225 226 |
# File 'lib/redis/commands/hashes.rb', line 222 def hscan(key, cursor, **) _scan(:hscan, cursor, [key], **) do |reply| [reply[0], reply[1].each_slice(2).to_a] end end |
#hscan_each(key, **options, &block) ⇒ Enumerator
Scan a hash
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/redis/commands/hashes.rb', line 239 def hscan_each(key, **, &block) return to_enum(:hscan_each, key, **) unless block_given? cursor = 0 loop do cursor, values = hscan(key, cursor, **) values.each(&block) break if cursor == "0" end end |
#hset(key, *attrs) ⇒ Integer
Set one or more hash values.
23 24 25 26 27 |
# File 'lib/redis/commands/hashes.rb', line 23 def hset(key, *attrs) attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash) send_command([:hset, key, *attrs]) end |
#hsetnx(key, field, value) ⇒ Boolean
Set the value of a hash field, only if the field does not exist.
35 36 37 |
# File 'lib/redis/commands/hashes.rb', line 35 def hsetnx(key, field, value) send_command([:hsetnx, key, field, value], &Boolify) end |
#hvals(key) ⇒ Array<String>
Get all the values in a hash.
199 200 201 |
# File 'lib/redis/commands/hashes.rb', line 199 def hvals(key) send_command([:hvals, key]) end |
#mapped_hmget(key, *fields) ⇒ Hash
Get the values of all the given hash fields.
104 105 106 107 108 109 110 111 112 |
# File 'lib/redis/commands/hashes.rb', line 104 def mapped_hmget(key, *fields) hmget(key, *fields) do |reply| if reply.is_a?(Array) Hash[fields.zip(reply)] else reply end end end |
#mapped_hmset(key, hash) ⇒ String
Set one or more hash values.
65 66 67 |
# File 'lib/redis/commands/hashes.rb', line 65 def mapped_hmset(key, hash) hmset(key, hash.to_a.flatten) end |