module Redis::Commands::Hash

Direct including types

Defined in:

commands/hash.cr

Instance Method Summary

Instance Method Detail

def 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

[View source]
def 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

[View source]
def hget(key : String, field : String) #

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

redis.hset "person:jamie", email: "[email protected]"
redis.hget "person:jamie", "email"    # => "[email protected]"
redis.hget "person:jamie", "password" # => nil

[View source]
def hgetall(key : String) #

Return the entire hash stored at key as an Array

redis.hset "person:jamie", email: "[email protected]", name: "Jamie"
redis.hgetall "person:jamie"
# => ["email", "[email protected]", "name", "Jamie"]

[View source]
def 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

[View source]
def hmget(key : String, fields : Enumerable(String)) #

Return the values for fields in key as an Array

redis.hset "person:jamie", email: "[email protected]", name: "Jamie"
redis.hmget "person:jamie", %w[email name]             # => ["[email protected]", "Jamie"]
redis.hmget "person:jamie", %w[nonexistent fake-field] # => [nil, nil]

[View source]
def hmget(key : String, *fields : String) #

Return the values for fields in key as an Array

redis.hset "person:jamie", email: "[email protected]", name: "Jamie"
redis.hmget "person:jamie", "email", "name"             # => ["[email protected]", "Jamie"]
redis.hmget "person:jamie", "nonexistent", "fake-field" # => [nil, nil]

[View source]
def 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/


[View source]
def 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

[View source]
def 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 [email protected] name Jamie] # => 2
redis.hset "person:jamie", %w[email [email protected] admin true] # => 1
redis.hset "person:jamie", %w[admin false]                        # => 0

[View source]
def 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" => "[email protected]", "name" => "Jamie"} # => 2
redis.hset "person:jamie", {"email" => "[email protected]", "admin" => "true"} # => 1
redis.hset "person:jamie", {"admin" => "false"}                                # => 0

[View source]
def 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", "[email protected]", "name", "Jamie" # => 2
redis.hset "person:jamie", "email", "[email protected]", "admin", "true" # => 1
redis.hset "person:jamie", "admin", "false"                              # => 0

[View source]
def 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: "[email protected]", name: "Jamie" # => 2
redis.hset "person:jamie", email: "[email protected]", admin: "true" # => 1
redis.hset "person:jamie", admin: "false"                            # => 0

[View source]
def 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

[View source]