Module: Protocol::Redis::Methods::Hashes

Defined in:
lib/protocol/redis/methods/hashes.rb

Overview

Methods for managing Redis hashes.

Instance Method Summary collapse

Instance Method Details

#hdel(key, *fields) ⇒ Object

Delete one or more hash fields. O(N) where N is the number of fields to be removed. See <redis.io/commands/hdel> for more details.



101
102
103
# File 'lib/protocol/redis/methods/hashes.rb', line 101

def hdel(key, *fields)
  call("HDEL", key, *fields)
end

#hexists(key, field) ⇒ Object

Determine if a hash field exists. O(1). See <redis.io/commands/hexists> for more details.



110
111
112
# File 'lib/protocol/redis/methods/hashes.rb', line 110

def hexists(key, field)
  call("HEXISTS", key, field) > 0
end

#hget(key, field) ⇒ Object

Get the value of a hash field. O(1). See <redis.io/commands/hget> for more details.



67
68
69
# File 'lib/protocol/redis/methods/hashes.rb', line 67

def hget(key, field)
  call("HGET", key, field)
end

#hgetall(key) ⇒ Object

Get all the fields and values in a hash. O(N) where N is the size of the hash. See <redis.io/commands/hgetall> for more details.



154
155
156
157
158
# File 'lib/protocol/redis/methods/hashes.rb', line 154

def hgetall(key)
  if pairs = call("HGETALL", key)
    pairs.each_slice(2).to_h
  end
end

#hincrby(key, field, increment) ⇒ Object

Increment the integer value of a hash field by the given number. O(1). See <redis.io/commands/hincrby> for more details.



120
121
122
# File 'lib/protocol/redis/methods/hashes.rb', line 120

def hincrby(key, field, increment)
  call("HINCRBY", key, field, increment)
end

#hincrbyfloat(key, field, increment) ⇒ Object

Increment the float value of a hash field by the given amount. O(1). See <redis.io/commands/hincrbyfloat> for more details.



130
131
132
# File 'lib/protocol/redis/methods/hashes.rb', line 130

def hincrbyfloat(key, field, increment)
  Float(call("HINCRBYFLOAT", key, field, increment))
end

#hkeys(key) ⇒ Object

Get all the fields in a hash. O(N) where N is the size of the hash. See <redis.io/commands/hkeys> for more details.



138
139
140
# File 'lib/protocol/redis/methods/hashes.rb', line 138

def hkeys(key)
  call("HKEYS", key)
end

#hlen(key) ⇒ Object

Get the number of fields in a hash. O(1). See <redis.io/commands/hlen> for more details.



17
18
19
# File 'lib/protocol/redis/methods/hashes.rb', line 17

def hlen(key)
  call("HLEN", key)
end

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields. O(N) where N is the number of fields being requested. See <redis.io/commands/hmget> for more details.



76
77
78
# File 'lib/protocol/redis/methods/hashes.rb', line 76

def hmget(key, *fields)
  call("HMGET", key, *fields)
end

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values. O(N) where N is the number of fields being set. See <redis.io/commands/hmset> for more details.



43
44
45
# File 'lib/protocol/redis/methods/hashes.rb', line 43

def hmset(key, *attrs)
  call("HMSET", key, *attrs)
end

#hscan(key, cursor = "0", match: nil, count: nil) ⇒ Object

Iterates fields of Hash types and their associated values. O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection. See <redis.io/commands/hscan/> for more details.



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/protocol/redis/methods/hashes.rb', line 164

def hscan(key, cursor = "0", match: nil, count: nil)
  arguments = [key, cursor]
  
  if match
    arguments.append("MATCH", match)
  end
  
  if count
    arguments.append("COUNT", count)
  end
  
  call("HSCAN", *arguments)
end

#hscan_each(key, cursor = "0", match: nil, count: nil, &block) ⇒ Object

Iterate over each field and the value of the hash, using HSCAN.



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/protocol/redis/methods/hashes.rb', line 179

def hscan_each(key, cursor = "0", match: nil, count: nil, &block)
  return enum_for(:hscan_each, key, cursor, match: match, count: count) unless block_given?
  
  while true
    cursor, data = hscan(key, cursor, match: match, count: count)
    
    data.each_slice(2, &block)
    
    break if cursor == "0"
  end
end

#hset(key, field, value) ⇒ Object

Set the string value of a hash field. O(1) for each field/value pair added, so O(N) to add N field/value pairs when the command is called with multiple field/value pairs. See <redis.io/commands/hset> for more details.



25
26
27
# File 'lib/protocol/redis/methods/hashes.rb', line 25

def hset(key, field, value)
  call("HSET", key, field, value)
end

#hsetnx(key, field, value) ⇒ Object

Set the value of a hash field, only if the field does not exist. O(1). See <redis.io/commands/hsetnx> for more details.



35
36
37
# File 'lib/protocol/redis/methods/hashes.rb', line 35

def hsetnx(key, field, value)
  call("HSETNX", key, field, value) > 0
end

#hvals(key) ⇒ Object

Get all the values in a hash. O(N) where N is the size of the hash. See <redis.io/commands/hvals> for more details.



146
147
148
# File 'lib/protocol/redis/methods/hashes.rb', line 146

def hvals(key)
  call("HVALS", key)
end

#mapped_hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields and return as array

See <#hmget> for more details.

Examples:

redis.mapped_hmget("hash", "f1", "f2")
  # => { "f1" => "v1", "f2" => "v2" }


91
92
93
94
# File 'lib/protocol/redis/methods/hashes.rb', line 91

def mapped_hmget(key, *fields)
  reply = hmget(key, *fields)
  Hash[fields.zip(reply)]
end

#mapped_hmset(key, hash) ⇒ Object

Set multiple hash fields to multiple values, by providing a hash

See <#hmset> for more details.

Examples:

redis.mapped_hmset("hash", { "f1" => "v1", "f2" => "v2" })
  # => "OK"


58
59
60
# File 'lib/protocol/redis/methods/hashes.rb', line 58

def mapped_hmset(key, hash)
  hmset(key, *hash.flatten)
end