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.
-
#hexpire(key, ttl, *fields) ⇒ Array<Integer>
Sets the time to live in seconds for one or more fields.
-
#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.
-
#himport_discard(fieldset_name) ⇒ Integer
Remove
fieldset_namefrom this connection's session. -
#himport_discard_all ⇒ Integer
Remove all fieldsets from this connection's session.
-
#himport_prepare(fieldset_name, *fields) ⇒ String
Register an ordered list of hash field names under
fieldset_namefor use by subsequent #himport_set calls on the same connection. -
#himport_set(key, fieldset_name, *values) ⇒ String
Create or fully replace the hash at
keyusing the field list registered underfieldset_nameon this connection. -
#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.
-
#hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Sets the time to live in milliseconds for one or more fields.
-
#hpttl(key, *fields) ⇒ Array<Integer>
Returns the time to live in milliseconds for one or more fields.
-
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or more random fields from a hash.
-
#hscan(key, cursor, **options) ⇒ 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.
-
#httl(key, *fields) ⇒ Array<Integer>
Returns the time to live in seconds for one or more fields.
-
#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.
156 157 158 159 |
# File 'lib/redis/commands/hashes.rb', line 156 def hdel(key, *fields) fields.flatten!(1) send_command([:hdel, key].concat(fields)) end |
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
166 167 168 |
# File 'lib/redis/commands/hashes.rb', line 166 def hexists(key, field) send_command([:hexists, key, field], &Boolify) end |
#hexpire(key, ttl, *fields) ⇒ Array<Integer>
Sets the time to live in seconds for one or more fields.
See https://redis.io/docs/latest/commands/hexpire/#return-information for array reply.
277 278 279 |
# File 'lib/redis/commands/hashes.rb', line 277 def hexpire(key, ttl, *fields) send_command([:hexpire, key, ttl, 'FIELDS', fields.length, *fields]) 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.
210 211 212 |
# File 'lib/redis/commands/hashes.rb', line 210 def hgetall(key) send_command([:hgetall, key], &Hashify) end |
#himport_discard(fieldset_name) ⇒ Integer
HIMPORT support is experimental: the client API may change in a future minor release without a major version bump.
Remove fieldset_name from this connection's session. Keys already
written through the fieldset are not affected.
406 407 408 |
# File 'lib/redis/commands/hashes.rb', line 406 def himport_discard(fieldset_name) send_command([:himport, "DISCARD", fieldset_name]) end |
#himport_discard_all ⇒ Integer
HIMPORT support is experimental: the client API may change in a future minor release without a major version bump.
Remove all fieldsets from this connection's session.
417 418 419 |
# File 'lib/redis/commands/hashes.rb', line 417 def himport_discard_all send_command([:himport, "DISCARDALL"]) end |
#himport_prepare(fieldset_name, *fields) ⇒ String
HIMPORT support is experimental: the client API may change in a future minor release without a major version bump.
Register an ordered list of hash field names under fieldset_name for
use by subsequent #himport_set calls on the same connection.
Fieldsets are server-side session state scoped to the current physical
connection: they vanish on disconnect or RESET and are invisible to
other connections. See the README "Bulk hash ingestion (HIMPORT)"
section for connection-scoping guidance. Re-preparing an existing
fieldset_name silently replaces it. Field order is preserved as
given; it defines the positional pairing used by #himport_set.
363 364 365 366 367 368 |
# File 'lib/redis/commands/hashes.rb', line 363 def himport_prepare(fieldset_name, *fields) fields.flatten!(1) raise ArgumentError, "fields must not be empty" if fields.empty? send_command([:himport, "PREPARE", fieldset_name].concat(fields)) end |
#himport_set(key, fieldset_name, *values) ⇒ String
HIMPORT support is experimental: the client API may change in a future minor release without a major version bump.
Create or fully replace the hash at key using the field list
registered under fieldset_name on this connection. Values pair
positionally with the fields given to #himport_prepare; the value
count must equal the field count.
The fieldset must exist on the executing connection, otherwise the server replies with a "no such fieldset" error.
390 391 392 393 394 395 |
# File 'lib/redis/commands/hashes.rb', line 390 def himport_set(key, fieldset_name, *values) values.flatten!(1) raise ArgumentError, "values must not be empty" if values.empty? send_command([:himport, "SET", key, fieldset_name].concat(values)) end |
#hincrby(key, field, increment) ⇒ Integer
Increment the integer value of a hash field by the given integer number.
176 177 178 |
# File 'lib/redis/commands/hashes.rb', line 176 def hincrby(key, field, increment) send_command([:hincrby, key, field, Integer(increment)]) end |
#hincrbyfloat(key, field, increment) ⇒ Float
Increment the numeric value of a hash field by the given float number.
186 187 188 |
# File 'lib/redis/commands/hashes.rb', line 186 def hincrbyfloat(key, field, increment) send_command([:hincrbyfloat, key, field, Float(increment)], &Floatify) end |
#hkeys(key) ⇒ Array<String>
Get all the fields in a hash.
194 195 196 |
# File 'lib/redis/commands/hashes.rb', line 194 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 92 |
# File 'lib/redis/commands/hashes.rb', line 89 def hmget(key, *fields, &blk) fields.flatten!(1) send_command([:hmget, key].concat(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 |
#hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Sets the time to live in milliseconds for one or more fields.
See https://redis.io/docs/latest/commands/hpexpire/#return-information for array reply.
315 316 317 318 319 320 321 322 323 324 |
# File 'lib/redis/commands/hashes.rb', line 315 def hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) args = [:hpexpire, key, ttl] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt args.concat(['FIELDS', fields.length, *fields]) send_command(args) end |
#hpttl(key, *fields) ⇒ Array<Integer>
Returns the time to live in milliseconds for one or more fields.
See https://redis.io/docs/latest/commands/hpttl/#return-information for array reply.
338 339 340 |
# File 'lib/redis/commands/hashes.rb', line 338 def hpttl(key, *fields) send_command([:hpttl, key, 'FIELDS', fields.length, *fields]) end |
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or more random fields from a hash.
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/redis/commands/hashes.rb', line 138 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, ...
Scan a hash
See the Redis Server HSCAN documentation for further details
230 231 232 233 234 235 236 237 238 |
# File 'lib/redis/commands/hashes.rb', line 230 def hscan(key, cursor, **) _scan(:hscan, cursor, [key], **) do |reply| if [:novalues] reply else [reply[0], reply[1].each_slice(2).to_a] end end end |
#hscan_each(key, **options, &block) ⇒ Enumerator
Scan a hash
See the Redis Server HSCAN documentation for further details
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/redis/commands/hashes.rb', line 254 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 |
#httl(key, *fields) ⇒ Array<Integer>
Returns the time to live in seconds for one or more fields.
See https://redis.io/docs/latest/commands/httl/#return-information for array reply.
293 294 295 |
# File 'lib/redis/commands/hashes.rb', line 293 def httl(key, *fields) send_command([:httl, key, 'FIELDS', fields.length, *fields]) end |
#hvals(key) ⇒ Array<String>
Get all the values in a hash.
202 203 204 |
# File 'lib/redis/commands/hashes.rb', line 202 def hvals(key) send_command([:hvals, key]) end |
#mapped_hmget(key, *fields) ⇒ Hash
Get the values of all the given hash fields.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/redis/commands/hashes.rb', line 105 def mapped_hmget(key, *fields) fields.flatten!(1) 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.flatten) end |