Module: Valkey::Commands::HashCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/hash_commands.rb
Overview
This module contains commands on the Hash data type.
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, seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set a timeout on one or more hash fields.
-
#hexpireat(key, unix_time, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set the expiration for one or more hash fields as a UNIX timestamp.
-
#hexpiretime(key, *fields) ⇒ Array<Integer>
Get the expiration Unix timestamp in seconds for one or more hash fields.
-
#hget(key, field) ⇒ String?
Get the value of a hash field.
-
#hgetall(key) ⇒ Hash
Get all the fields and values in a hash.
-
#hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ String, Array<String, nil>
Get the value of one or more hash fields and optionally set their expiration time.
-
#hincrby(key, field, increment) ⇒ Integer
Increment the integer value of a hash field by the given 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, nil>
Get the values of all the given hash fields.
-
#hmset(key, *args) ⇒ String
Set multiple hash fields to multiple values.
-
#hpersist(key, *fields) ⇒ Array<Integer>
Remove the expiration from one or more hash fields.
-
#hpexpire(key, milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set a timeout on one or more hash fields in milliseconds.
-
#hpexpireat(key, unix_time_ms, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set the expiration for one or more hash fields as a UNIX timestamp in milliseconds.
-
#hpexpiretime(key, *fields) ⇒ Array<Integer>
Get the expiration Unix timestamp in milliseconds for one or more hash fields.
-
#hpttl(key, *fields) ⇒ Array<Integer>
Get the time to live in milliseconds of one or more hash fields.
-
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or multiple 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.
-
#hsetex(key, field, value, seconds) ⇒ Integer
Set the string value of a hash field and set its expiration time in seconds.
-
#hsetnx(key, field, value) ⇒ Boolean
Set the string value of a hash field, only if the field does not exist.
-
#hstrlen(key, field) ⇒ Integer
Get the string length of the value associated with field in the hash stored at key.
-
#httl(key, *fields) ⇒ Array<Integer>
Get the time to live in seconds of one or more hash 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 multiple hash fields to multiple values.
Instance Method Details
#hdel(key, *fields) ⇒ Integer
Delete one or more hash fields.
19 20 21 22 |
# File 'lib/valkey/commands/hash_commands.rb', line 19 def hdel(key, *fields) fields.flatten!(1) send_command(RequestType::HDEL, [key].concat(fields)) end |
#hexists(key, field) ⇒ Boolean
Determine if a hash field exists.
33 34 35 |
# File 'lib/valkey/commands/hash_commands.rb', line 33 def hexists(key, field) send_command(RequestType::HEXISTS, [key, field], &Utils::Boolify) end |
#hexpire(key, seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set a timeout on one or more hash fields.
394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/valkey/commands/hash_commands.rb', line 394 def hexpire(key, seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [key, Integer(seconds)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt args.push("FIELDS", fields.length, *fields) send_command(RequestType::HEXPIRE, args) end |
#hexpireat(key, unix_time, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set the expiration for one or more hash fields as a UNIX timestamp.
424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/valkey/commands/hash_commands.rb', line 424 def hexpireat(key, unix_time, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [key, Integer(unix_time)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt args.push("FIELDS", fields.length, *fields) send_command(RequestType::HEXPIREAT, args) end |
#hexpiretime(key, *fields) ⇒ Array<Integer>
Get the expiration Unix timestamp in seconds for one or more hash fields.
565 566 567 568 569 570 |
# File 'lib/valkey/commands/hash_commands.rb', line 565 def hexpiretime(key, *fields) fields.flatten!(1) args = [key, "FIELDS", fields.length, *fields] send_command(RequestType::HEXPIRETIME, args) end |
#hget(key, field) ⇒ String?
Get the value of a hash field.
46 47 48 |
# File 'lib/valkey/commands/hash_commands.rb', line 46 def hget(key, field) send_command(RequestType::HGET, [key, field]) end |
#hgetall(key) ⇒ Hash
Get all the fields and values in a hash.
58 59 60 |
# File 'lib/valkey/commands/hash_commands.rb', line 58 def hgetall(key) send_command(RequestType::HGET_ALL, [key], &Utils::Hashify) end |
#hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ String, Array<String, nil>
Get the value of one or more hash fields and optionally set their expiration time.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/valkey/commands/hash_commands.rb', line 361 def hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) fields.flatten!(1) args = [key] args << "EX" << ex if ex args << "PX" << px if px args << "EXAT" << exat if exat args << "PXAT" << pxat if pxat args << "PERSIST" if persist args.push("FIELDS", fields.length, *fields) send_command(RequestType::HGETEX, args) do |reply| fields.length == 1 ? reply[0] : reply end end |
#hincrby(key, field, increment) ⇒ Integer
Increment the integer value of a hash field by the given number.
72 73 74 |
# File 'lib/valkey/commands/hash_commands.rb', line 72 def hincrby(key, field, increment) send_command(RequestType::HINCR_BY, [key, field, Integer(increment)]) end |
#hincrbyfloat(key, field, increment) ⇒ Float
Increment the numeric value of a hash field by the given float number.
86 87 88 |
# File 'lib/valkey/commands/hash_commands.rb', line 86 def hincrbyfloat(key, field, increment) send_command(RequestType::HINCR_BY_FLOAT, [key, field, Float(increment)], &Utils::Floatify) end |
#hkeys(key) ⇒ Array<String>
Get all the fields in a hash.
98 99 100 |
# File 'lib/valkey/commands/hash_commands.rb', line 98 def hkeys(key) send_command(RequestType::HKEYS, [key]) end |
#hlen(key) ⇒ Integer
Get the number of fields in a hash.
110 111 112 |
# File 'lib/valkey/commands/hash_commands.rb', line 110 def hlen(key) send_command(RequestType::HLEN, [key]) end |
#hmget(key, *fields, &blk) ⇒ Array<String, nil>
Get the values of all the given hash fields.
125 126 127 128 |
# File 'lib/valkey/commands/hash_commands.rb', line 125 def hmget(key, *fields, &blk) fields.flatten!(1) send_command(RequestType::HMGET, [key].concat(fields), &blk) end |
#hmset(key, *args) ⇒ String
Set multiple hash fields to multiple values.
163 164 165 |
# File 'lib/valkey/commands/hash_commands.rb', line 163 def hmset(key, *args) send_command(RequestType::HMSET, [key].concat(args)) end |
#hpersist(key, *fields) ⇒ Array<Integer>
Remove the expiration from one or more hash fields.
508 509 510 511 512 513 |
# File 'lib/valkey/commands/hash_commands.rb', line 508 def hpersist(key, *fields) fields.flatten!(1) args = [key, "FIELDS", fields.length, *fields] send_command(RequestType::HPERSIST, args) end |
#hpexpire(key, milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set a timeout on one or more hash fields in milliseconds.
454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/valkey/commands/hash_commands.rb', line 454 def hpexpire(key, milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [key, Integer(milliseconds)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt args.push("FIELDS", fields.length, *fields) send_command(RequestType::HPEXPIRE, args) end |
#hpexpireat(key, unix_time_ms, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>
Set the expiration for one or more hash fields as a UNIX timestamp in milliseconds.
484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/valkey/commands/hash_commands.rb', line 484 def hpexpireat(key, unix_time_ms, *fields, nx: nil, xx: nil, gt: nil, lt: nil) fields.flatten!(1) args = [key, Integer(unix_time_ms)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt args.push("FIELDS", fields.length, *fields) send_command(RequestType::HPEXPIREAT, args) end |
#hpexpiretime(key, *fields) ⇒ Array<Integer>
Get the expiration Unix timestamp in milliseconds for one or more hash fields.
584 585 586 587 588 589 |
# File 'lib/valkey/commands/hash_commands.rb', line 584 def hpexpiretime(key, *fields) fields.flatten!(1) args = [key, "FIELDS", fields.length, *fields] send_command(RequestType::HPEXPIRETIME, args) end |
#hpttl(key, *fields) ⇒ Array<Integer>
Get the time to live in milliseconds of one or more hash fields.
546 547 548 549 550 551 |
# File 'lib/valkey/commands/hash_commands.rb', line 546 def hpttl(key, *fields) fields.flatten!(1) args = [key, "FIELDS", fields.length, *fields] send_command(RequestType::HPTTL, args) end |
#hrandfield(key, count = nil, withvalues: false, with_values: withvalues) ⇒ nil, ...
Get one or multiple random fields from a hash.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/valkey/commands/hash_commands.rb', line 204 def hrandfield(key, count = nil, withvalues: false, with_values: withvalues) raise ArgumentError, "count argument must be specified" if with_values && count.nil? args = [key] args << Integer(count) if count args << "WITHVALUES" if with_values if with_values send_command(RequestType::HRAND_FIELD, args) do |reply| # Handle both ARRAY (flat) and MAP (already pairs) response types if reply.is_a?(Array) && !reply.empty? && reply.first.is_a?(Array) && reply.first.size == 2 # Already in pairs format (from MAP response): [[field, value], ...] reply elsif reply.is_a?(Array) && reply.respond_to?(:each_slice) # ARRAY response: flat array of field-value pairs, convert to pairs reply.each_slice(2).to_a else # Fallback: try Pairify Utils::Pairify.call(reply) end end else send_command(RequestType::HRAND_FIELD, args) end end |
#hscan(key, cursor, **options) ⇒ String, Array<[String, String]>
Scan a hash
See the [Valkey Server HSCAN documentation](valkey.io/commands/hscan/) for further details
244 245 246 247 248 |
# File 'lib/valkey/commands/hash_commands.rb', line 244 def hscan(key, cursor, **) _scan(RequestType::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
See the [Valkey Server HSCAN documentation](valkey.io/commands/hscan/) for further details
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/valkey/commands/hash_commands.rb', line 264 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.
284 285 286 287 288 |
# File 'lib/valkey/commands/hash_commands.rb', line 284 def hset(key, *attrs) attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash) send_command(RequestType::HSET, [key, *attrs]) end |
#hsetex(key, field, value, seconds) ⇒ Integer
Set the string value of a hash field and set its expiration time in seconds.
341 342 343 |
# File 'lib/valkey/commands/hash_commands.rb', line 341 def hsetex(key, field, value, seconds) send_command(RequestType::HSETEX, [key, "EX", Integer(seconds), "FIELDS", 1, field, value]) end |
#hsetnx(key, field, value) ⇒ Boolean
Set the string value of a hash field, only if the field does not exist.
300 301 302 |
# File 'lib/valkey/commands/hash_commands.rb', line 300 def hsetnx(key, field, value) send_command(RequestType::HSET_NX, [key, field, value], &Utils::Boolify) end |
#hstrlen(key, field) ⇒ Integer
Get the string length of the value associated with field in the hash stored at key.
314 315 316 |
# File 'lib/valkey/commands/hash_commands.rb', line 314 def hstrlen(key, field) send_command(RequestType::HSTRLEN, [key, field]) end |
#httl(key, *fields) ⇒ Array<Integer>
Get the time to live in seconds of one or more hash fields.
527 528 529 530 531 532 |
# File 'lib/valkey/commands/hash_commands.rb', line 527 def httl(key, *fields) fields.flatten!(1) args = [key, "FIELDS", fields.length, *fields] send_command(RequestType::HTTL, args) end |
#hvals(key) ⇒ Array<String>
Get all the values in a hash.
326 327 328 |
# File 'lib/valkey/commands/hash_commands.rb', line 326 def hvals(key) send_command(RequestType::HVALS, [key]) end |
#mapped_hmget(key, *fields) ⇒ Hash
Get the values of all the given hash fields.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/valkey/commands/hash_commands.rb', line 141 def mapped_hmget(key, *fields) fields.flatten!(1) hmget(key, fields) do |reply| if reply.is_a?(Array) fields.zip(reply).to_h else reply end end end |
#mapped_hmset(key, hash) ⇒ String
Set multiple hash fields to multiple values.
178 179 180 |
# File 'lib/valkey/commands/hash_commands.rb', line 178 def mapped_hmset(key, hash) hmset(key, *hash.flatten) end |