Module: RedisCluster::Function::Hash

Included in:
RedisCluster::Function
Defined in:
lib/redis_cluster/function/hash.rb

Overview

Hash implement redis hashes commands. There will be some adjustment for cluster. see redis.io/commands#hash. Most of the code are copied from github.com/redis/redis-rb/blob/master/lib/redis.rb.

SETTER = [:hdel, :hincrby, :hincrbyfloat, :hmset, :hset, :hsetnx] GETTER = [:hexists, :hget, :hgetall, :hkeys, :hlen, :hmget, :hstrlen, :hvals, :hscan]

Instance Method Summary collapse

Instance Method Details

#hdel(key, field) ⇒ Fixnum

Delete one or more hash fields.

Parameters:

Returns:

  • (Fixnum)

    the number of fields that were removed from the hash



21
22
23
# File 'lib/redis_cluster/function/hash.rb', line 21

def hdel(key, field)
  call(key, [:hdel, key, field])
end

#hexists(key, field) ⇒ Boolean

Determine if a hash field exists.

Parameters:

Returns:

  • (Boolean)

    whether or not the field exists in the hash



83
84
85
# File 'lib/redis_cluster/function/hash.rb', line 83

def hexists(key, field)
  call(key, [:hexists, key, field], transform: Redis::Boolify, read: true)
end

#hget(key, field) ⇒ String

Get the value of a hash field.

Parameters:

Returns:



92
93
94
# File 'lib/redis_cluster/function/hash.rb', line 92

def hget(key, field)
  call(key, [:hget, key, field], read: true)
end

#hgetall(key) ⇒ Hash<String, String>

Get all the fields and values in a hash.

Parameters:

Returns:



100
101
102
# File 'lib/redis_cluster/function/hash.rb', line 100

def hgetall(key)
  call(key, [:hgetall, key], transform: Redis::Hashify, read: true)
end

#hincrby(key, field, increment) ⇒ Fixnum

Increment the integer value of a hash field by the given integer number.

Parameters:

Returns:

  • (Fixnum)

    value of the field after incrementing it



31
32
33
# File 'lib/redis_cluster/function/hash.rb', line 31

def hincrby(key, field, increment)
  call(key, [:hincrby, key, field, increment])
end

#hincrbyfloat(key, field, increment) ⇒ Float

Increment the numeric value of a hash field by the given float number.

Parameters:

Returns:

  • (Float)

    value of the field after incrementing it



41
42
43
# File 'lib/redis_cluster/function/hash.rb', line 41

def hincrbyfloat(key, field, increment)
  call(key, [:hincrbyfloat, key, field, increment], transform: Redis::Floatify)
end

#hkeys(key) ⇒ Array<String>

Get all the fields in a hash.

Parameters:

Returns:



108
109
110
# File 'lib/redis_cluster/function/hash.rb', line 108

def hkeys(key)
  call(key, [:hkeys, key], read: true)
end

#hlen(key) ⇒ Fixnum

Get the number of fields in a hash.

Parameters:

Returns:

  • (Fixnum)

    number of fields in the hash



124
125
126
# File 'lib/redis_cluster/function/hash.rb', line 124

def hlen(key)
  call(key, [:hlen, key], read: true)
end

#hmget(key, *fields) ⇒ Array<String>

Get the values of all the given hash fields.

Examples:

redis.hmget("hash", "f1", "f2")
  # => ["v1", "v2"]

Parameters:

Returns:

  • (Array<String>)

    an array of values for the specified fields



137
138
139
# File 'lib/redis_cluster/function/hash.rb', line 137

def hmget(key, *fields)
  call(key, [:hmget, key] + fields, read: true)
end

#hmset(key, *attrs) ⇒ String

Set one or more hash values.

Examples:

redis.hmset("hash", "f1", "v1", "f2", "v2")
  # => "OK"

Parameters:

  • key (String)
  • attrs (Array<String>)

    array of fields and values

Returns:



54
55
56
# File 'lib/redis_cluster/function/hash.rb', line 54

def hmset(key, *attrs)
  call(key, [:hmset, key] + attrs)
end

#hset(key, field, value) ⇒ Boolean

Set the string value of a hash field.

Parameters:

Returns:

  • (Boolean)

    whether or not the field was added to the hash



64
65
66
# File 'lib/redis_cluster/function/hash.rb', line 64

def hset(key, field, value)
  call(key, [:hset, key, field, value], transform: Redis::Boolify)
end

#hsetnx(key, field, value) ⇒ Boolean

Set the value of a hash field, only if the field does not exist.

Parameters:

Returns:

  • (Boolean)

    whether or not the field was added to the hash



74
75
76
# File 'lib/redis_cluster/function/hash.rb', line 74

def hsetnx(key, field, value)
  call(key, [:hsetnx, key, field, value], transform: Redis::Boolify)
end

#hstrlen(key, field) ⇒ Fixnum

Returns the string length of the value associated with field in the hash stored at key.

Parameters:

Returns:

  • (Fixnum)

    String lenght



146
147
148
# File 'lib/redis_cluster/function/hash.rb', line 146

def hstrlen(key, field)
  call(key, [:hstrlen, key, field], read: true)
end

#hvals(key) ⇒ Array<String>

Get all the values in a hash.

Parameters:

Returns:



116
117
118
# File 'lib/redis_cluster/function/hash.rb', line 116

def hvals(key)
  call(key, [:hvals, key], read: true)
end