module Redis::Commands::Hash
Direct including types
Defined in:
commands/hash.crInstance Method Summary
-
#hdel(key : String, fields : Enumerable(String))
Delete one or more
fields
from the givenkey
, returning the number of deleted fields. -
#hdel(key : String, *fields : String)
Delete one or more
fields
from the givenkey
, returning the number of deleted fields. -
#hget(key : String, field : String)
Return the value of
field
forkey
, if both exist. -
#hgetall(key : String)
Return the entire hash stored at
key
as anArray
-
#hincrby(key : String, field : String, increment : Int | String)
Increment the numeric value for
field
in the hash stored inkey
byincrement
, returning the new value. -
#hmget(key : String, fields : Enumerable(String))
Return the values for
fields
inkey
as anArray
-
#hmget(key : String, *fields : String)
Return the values for
fields
inkey
as anArray
-
#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/
-
#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 atkey
, allowing you to iterate through the keys in the hash without blocking the Redis server for too long at a time. -
#hset(key : String, fields : Enumerable(String))
Set the values for
fields
in the hash stored inkey
, returning the number of fields created (not updated) -
#hset(key : String, fields : ::Hash(String, String))
Set the values for
fields
in the hash stored inkey
, returning the number of fields created (not updated) -
#hset(key : String, *fields : String)
Set the values for
fields
in the hash stored inkey
, returning the number of fields created (not updated). -
#hset(key : String, **fields : String)
Set the values for
fields
in the hash stored inkey
, returning the number of fields created (not updated) -
#hsetnx(key : String, field : String, value : String)
Set
field
in the hash stored inkey
tovalue
if and only if it does not exist.
Instance Method Detail
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
Delete one or more fields
from the given key
, returning the number of
deleted fields.
redis.hdel "my-hash",
"pending",
"nonexistent-field"
# => 1
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
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"]
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
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]
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]
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/
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
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
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
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
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
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