module

Redis::Commands::Hash

Instance methods

hdel(key : String, fields : Enumerable(String))

Delete one or more fields from the given key, returning the number of deleted fields.

fields = %w[pending nonexistent-field]
redis.hdel "my-hash", fields
# => 1
Source
hdel(key : String, *fields : String)

Delete one or more fields from the given key, returning the number of deleted fields.

redis.hdel "my-hash",
  "pending",
  "nonexistent-field"
# => 1
Source
hget(key : String, field : String)

Return the value of field for key, if both exist.

redis.hset "person:jamie", email: "jamie@example.com"
redis.hget "person:jamie", "email"    # => "jamie@example.com"
redis.hget "person:jamie", "password" # => nil
Source
hgetall(key : String)

Return the entire hash stored at key as an Array

redis.hset "person:jamie", email: "jamie@example.com", name: "Jamie"
redis.hgetall "person:jamie"
# => ["email", "jamie@example.com", "name", "Jamie"]
Source
hincrby(key : String, field : String, increment : Int | String)

Increment the numeric value for field in the hash stored in key by increment, returning the new value.

id = 1234
redis.hincrby "posts:#{id}", "likes", 1 # => 1
redis.hincrby "posts:#{id}", "likes", 1 # => 2
Source
hmget(key : String, fields : Enumerable(String))

Return the values for fields in key as an Array

redis.hset "person:jamie", email: "jamie@example.com", name: "Jamie"
redis.hmget "person:jamie", %w[email name]             # => ["jamie@example.com", "Jamie"]
redis.hmget "person:jamie", %w[nonexistent fake-field] # => [nil, nil]
Source
hmget(key : String, *fields : String)

Return the values for fields in key as an Array

redis.hset "person:jamie", email: "jamie@example.com", name: "Jamie"
redis.hmget "person:jamie", "email", "name"             # => ["jamie@example.com", "Jamie"]
redis.hmget "person:jamie", "nonexistent", "fake-field" # => [nil, nil]
Source
hmset(key : String, data : ::Hash(String, String))

DEPRECATED The Redis HMSET command is deprecated. Use HSET instead. This method will be removed in v1.0.0 of this shard. See https://redis.io/commands/hmset/

Source
hscan(key : String, cursor : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil)

Execute the HSCAN command to return a subset of keys in the hash stored at key, allowing you to iterate through the keys in the hash without blocking the Redis server for too long at a time.

NOTE: You probably want to use Redis::Client#hscan_each instead of using this method directly.

cursor = ""
until cursor == "0"
  response = redis.hscan(key, cursor)
  cursor, fields = response.as(Array)
  cursor = cursor.as(String)
  fields.as(Array).each do |field|
    # Do something with that hash field...
  end
end
Source
hset(key : String, fields : Enumerable(String))

Set the values for fields in the hash stored in key, returning the number of fields created (not updated)

NOTE: fields MUST contain an even number of elements

redis.hset "person:jamie", %w[email jamie@example.com name Jamie] # => 2
redis.hset "person:jamie", %w[email jamie@example.dev admin true] # => 1
redis.hset "person:jamie", %w[admin false]                        # => 0
Source
hset(key : String, fields : ::Hash(String, String))

Set the values for fields in the hash stored in key, returning the number of fields created (not updated)

redis.hset "person:jamie", {"email" => "jamie@example.com", "name" => "Jamie"} # => 2
redis.hset "person:jamie", {"email" => "jamie@example.dev", "admin" => "true"} # => 1
redis.hset "person:jamie", {"admin" => "false"}                                # => 0
Source
hset(key : String, *fields : String)

Set the values for fields in the hash stored in key, returning the number of fields created (not updated).

NOTE: You MUST pass an even number of arguments to fields

redis.hset "person:jamie", "email", "jamie@example.com", "name", "Jamie" # => 2
redis.hset "person:jamie", "email", "jamie@example.dev", "admin", "true" # => 1
redis.hset "person:jamie", "admin", "false"                              # => 0
Source
hset(key : String, **fields : String)

Set the values for fields in the hash stored in key, returning the number of fields created (not updated)

redis.hset "person:jamie", email: "jamie@example.com", name: "Jamie" # => 2
redis.hset "person:jamie", email: "jamie@example.dev", admin: "true" # => 1
redis.hset "person:jamie", admin: "false"                            # => 0
Source
hsetnx(key : String, field : String, value : String)

Set field in the hash stored in key to value if and only if it does not exist. Returns 1 if the field was set, 0 if it was not.

id = 1234

redis.hsetnx "job:#{id}", "locked_at", Time.utc.to_rfc3339 # => 1
# Returned 1, lock succeeds

redis.hsetnx "job:#{id}", "locked_at", Time.utc.to_rfc3339 # => 0
# Returned 0, lock did not succeed, so the job is already being processed
Source