Module: Redis::Commands::Hashes
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | lib/redis/commands/hashes.rb |
Instance Method Summary
-
#hdel(key, *fields) ⇒ Integer
Delete one or more hash fields.
-
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
-
#hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Setsthe time to live in seconds for one or more fields. -
#hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Setsthe expiration for one or more fields as an absolute Unix timestamp in seconds. -
#hexpiretime(key, *fields) ⇒ Array<Integer>
Returns the expiration time of one or more fields as an absolute Unix timestamp in seconds.
-
#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.
-
#hgetdel(key, *fields) ⇒ Array<String, nil>
Get the value of one or more hash fields and delete them.
-
#hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ Array<String, nil>
Get the value of one or more hash fields and optionally set their expiration.
-
#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.
-
#hpersist(key, *fields) ⇒ Array<Integer>
Removes the expiration from one or more fields, making them persistent.
-
#hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Setsthe time to live in milliseconds for one or more fields. -
#hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Setsthe expiration for one or more fields as an absolute Unix timestamp in milliseconds. -
#hpexpiretime(key, *fields) ⇒ Array<Integer>
Returns the expiration time of one or more fields as an absolute Unix timestamp in milliseconds.
-
#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.
-
#hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) ⇒ Integer
Set the value of one or more hash fields, and optionally their expiration.
-
#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.
-
#hash_field_expiration_condition(nx, xx, gt, lt)
private
The HEXPIRE command family accepts a single optional condition token (
[NX | XX | GT | LT]); the server rejects combinations, so fail fast in Ruby.
Instance Method Details
#hash_field_expiration_condition(nx, xx, gt, lt) (private)
The HEXPIRE command family accepts a single optional condition token
([NX | XX | GT | LT]); the server rejects combinations, so fail fast in Ruby.
# File 'lib/redis/commands/hashes.rb', line 653
def hash_field_expiration_condition(nx, xx, gt, lt) condition = [] condition << "NX" if nx condition << "XX" if xx condition << "GT" if gt condition << "LT" if lt raise ArgumentError, "only one of :nx, :xx, :gt, :lt can be specified" if condition.size > 1 condition end
#hdel(key, *fields) ⇒ Integer
Delete one or more hash fields.
# File 'lib/redis/commands/hashes.rb', line 157
def hdel(key, *fields) fields.flatten!(1) send_command([:hdel, key].concat(fields)) end
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
# File 'lib/redis/commands/hashes.rb', line 267
def hexists(key, field) send_command([:hexists, key, field], &Boolify) end
#hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ 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.
# File 'lib/redis/commands/hashes.rb', line 385
def hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [:hexpire, key, Integer(ttl)] args.concat(hash_field_expiration_condition(nx, xx, gt, lt)) args.concat(['FIELDS', fields.length, *fields]) send_command(args) end
#hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Sets the expiration for one or more fields as an absolute Unix
timestamp in seconds. A timestamp in the past deletes the field
immediately.
See https://redis.io/docs/latest/commands/hexpireat/#return-information for array reply.
# File 'lib/redis/commands/hashes.rb', line 477
def hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [:hexpireat, key, Integer(unix_time_seconds)] args.concat(hash_field_expiration_condition(nx, xx, gt, lt)) args.concat(['FIELDS', fields.length, *fields]) send_command(args) end
#hexpiretime(key, *fields) ⇒ Array<Integer>
Returns the expiration time of one or more fields as an absolute Unix timestamp in seconds.
See https://redis.io/docs/latest/commands/hexpiretime/#return-information for array reply.
# File 'lib/redis/commands/hashes.rb', line 529
def hexpiretime(key, *fields) fields.flatten!(1) send_command([:hexpiretime, key, 'FIELDS', fields.length, *fields]) end
#hget(key, field) ⇒ String
Get the value of a hash field.
# File 'lib/redis/commands/hashes.rb', line 75
def hget(key, field) send_command([:hget, key, field]) end
#hgetall(key) ⇒ Hash<String, String>
Get all the fields and values in a hash.
# File 'lib/redis/commands/hashes.rb', line 311
def hgetall(key) send_command([:hgetall, key], &Hashify) end
#hgetdel(key, *fields) ⇒ Array<String, nil>
Get the value of one or more hash fields and delete them.
# File 'lib/redis/commands/hashes.rb', line 173
def hgetdel(key, *fields) fields.flatten!(1) send_command([:hgetdel, key, 'FIELDS', fields.length, *fields]) end
#hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ Array<String, nil>
Get the value of one or more hash fields and optionally set their expiration. HGETEX is similar to HMGET, but is a write command with additional options. When no options are provided, HGETEX behaves like HMGET.
# File 'lib/redis/commands/hashes.rb', line 199
def hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) if [ex, px, exat, pxat, persist].count { |option| option } > 1 raise ArgumentError, "ex, px, exat, pxat, and persist are mutually exclusive" end fields.flatten!(1) args = [:hgetex, key] args << "EX" << Integer(ex) if ex args << "PX" << Integer(px) if px args << "EXAT" << Integer(exat) if exat args << "PXAT" << Integer(pxat) if pxat args << "PERSIST" if persist args.concat(['FIELDS', fields.length, *fields]) send_command(args) 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.
# File 'lib/redis/commands/hashes.rb', line 634
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.
# File 'lib/redis/commands/hashes.rb', line 645
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.
# File 'lib/redis/commands/hashes.rb', line 591
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.
# File 'lib/redis/commands/hashes.rb', line 618
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.
# File 'lib/redis/commands/hashes.rb', line 277
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.
# File 'lib/redis/commands/hashes.rb', line 287
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.
# File 'lib/redis/commands/hashes.rb', line 295
def hkeys(key) send_command([:hkeys, key]) end
#hlen(key) ⇒ Integer
Get the number of fields in a hash.
# 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.
# File 'lib/redis/commands/hashes.rb', line 90
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.
# File 'lib/redis/commands/hashes.rb', line 51
def hmset(key, *attrs) send_command([:hmset, key] + attrs) end
#hpersist(key, *fields) ⇒ Array<Integer>
Removes the expiration from one or more fields, making them persistent.
See https://redis.io/docs/latest/commands/hpersist/#return-information for array reply.
# File 'lib/redis/commands/hashes.rb', line 565
def hpersist(key, *fields) fields.flatten!(1) send_command([:hpersist, key, 'FIELDS', fields.length, *fields]) 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.
# File 'lib/redis/commands/hashes.rb', line 430
def hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [:hpexpire, key, Integer(ttl)] args.concat(hash_field_expiration_condition(nx, xx, gt, lt)) args.concat(['FIELDS', fields.length, *fields]) send_command(args) end
#hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Sets the expiration for one or more fields as an absolute Unix
timestamp in milliseconds. A timestamp in the past deletes the field
immediately.
See https://redis.io/docs/latest/commands/hpexpireat/#return-information for array reply.
# File 'lib/redis/commands/hashes.rb', line 507
def hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [:hpexpireat, key, Integer(unix_time_milliseconds)] args.concat(hash_field_expiration_condition(nx, xx, gt, lt)) args.concat(['FIELDS', fields.length, *fields]) send_command(args) end
#hpexpiretime(key, *fields) ⇒ Array<Integer>
Returns the expiration time of one or more fields as an absolute Unix timestamp in milliseconds.
See https://redis.io/docs/latest/commands/hpexpiretime/#return-information for array reply.
# File 'lib/redis/commands/hashes.rb', line 547
def hpexpiretime(key, *fields) fields.flatten!(1) send_command([:hpexpiretime, key, 'FIELDS', fields.length, *fields]) 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.
# File 'lib/redis/commands/hashes.rb', line 451
def hpttl(key, *fields) fields.flatten!(1) 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.
# File 'lib/redis/commands/hashes.rb', line 139
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, ...
[ GitHub ]
# File 'lib/redis/commands/hashes.rb', line 331
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
[ GitHub ]
# File 'lib/redis/commands/hashes.rb', line 355
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.
# 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) return 0 if attrs.empty? send_command([:hset, key, *attrs]) end
#hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) ⇒ Integer
Set the value of one or more hash fields, and optionally their expiration. Existing
values for the given fields are overwritten, and any previous TTL on those fields is
discarded unless :keepttl is given.
# File 'lib/redis/commands/hashes.rb', line 240
def hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) raise ArgumentError, "fnx and fxx are mutually exclusive" if fnx && fxx if [ex, px, exat, pxat, keepttl].count { |option| option } > 1 raise ArgumentError, "ex, px, exat, pxat, and keepttl are mutually exclusive" end attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash) attrs.flatten!(1) args = [:hsetex, key] args << "FNX" if fnx args << "FXX" if fxx args << "EX" << Integer(ex) if ex args << "PX" << Integer(px) if px args << "EXAT" << Integer(exat) if exat args << "PXAT" << Integer(pxat) if pxat args << "KEEPTTL" if keepttl args.concat(['FIELDS', attrs.length / 2, *attrs]) send_command(args) end
#hsetnx(key, field, value) ⇒ Boolean
Set the value of a hash field, only if the field does not exist.
# File 'lib/redis/commands/hashes.rb', line 36
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.
# File 'lib/redis/commands/hashes.rb', line 406
def httl(key, *fields) fields.flatten!(1) send_command([:httl, key, 'FIELDS', fields.length, *fields]) end
#hvals(key) ⇒ Array<String>
Get all the values in a hash.
# File 'lib/redis/commands/hashes.rb', line 303
def hvals(key) send_command([:hvals, key]) end
#mapped_hmget(key, *fields) ⇒ Hash
Get the values of all the given hash fields.
# File 'lib/redis/commands/hashes.rb', line 106
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.
# File 'lib/redis/commands/hashes.rb', line 66
def mapped_hmset(key, hash) hmset(key, hash.flatten) end