Module: Protocol::Redis::Cluster::Methods::Strings

Defined in:
lib/protocol/redis/cluster/methods/strings.rb

Overview

Provides Redis String commands for cluster environments. String operations are routed to the appropriate shard based on the key.

Instance Method Summary collapse

Instance Method Details

#append(key, value, role: :master) ⇒ Object

Append a value to a key.



18
19
20
21
22
23
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 18

def append(key, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("APPEND", key, value)
end

#bitcount(key, *range, role: :master) ⇒ Object

Count set bits in a string.



30
31
32
33
34
35
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 30

def bitcount(key, *range, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("BITCOUNT", key, *range)
end

#decr(key, role: :master) ⇒ Object

Decrement the integer value of a key by one.



41
42
43
44
45
46
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 41

def decr(key, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("DECR", key)
end

#decrby(key, decrement, role: :master) ⇒ Object

Decrement the integer value of a key by the given number.



53
54
55
56
57
58
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 53

def decrby(key, decrement, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("DECRBY", key, decrement)
end

#get(key, role: :master) ⇒ Object

Get the value of a key.



64
65
66
67
68
69
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 64

def get(key, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("GET", key)
end

#getbit(key, offset, role: :master) ⇒ Object

Get the bit value at offset in the string value stored at key.



76
77
78
79
80
81
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 76

def getbit(key, offset, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("GETBIT", key, offset)
end

#getrange(key, start_index, end_index, role: :master) ⇒ Object

Get a substring of the string stored at a key.



89
90
91
92
93
94
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 89

def getrange(key, start_index, end_index, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("GETRANGE", key, start_index, end_index)
end

#getset(key, value, role: :master) ⇒ Object

Set the string value of a key and return its old value.



101
102
103
104
105
106
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 101

def getset(key, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("GETSET", key, value)
end

#incr(key, role: :master) ⇒ Object

Increment the integer value of a key by one.



112
113
114
115
116
117
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 112

def incr(key, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("INCR", key)
end

#incrby(key, increment, role: :master) ⇒ Object

Increment the integer value of a key by the given amount.



124
125
126
127
128
129
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 124

def incrby(key, increment, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("INCRBY", key, increment)
end

#incrbyfloat(key, increment, role: :master) ⇒ Object

Increment the float value of a key by the given amount.



136
137
138
139
140
141
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 136

def incrbyfloat(key, increment, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("INCRBYFLOAT", key, increment)
end

#mget(*keys, role: :master) ⇒ Object

Get the values of all the given keys. Uses the cluster-aware multi-key handling from generic methods.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 148

def mget(*keys, role: :master)
  return [] if keys.empty?
  
  results = Array.new(keys.size)
  key_to_index = keys.each_with_index.to_h
  
  clients_for(*keys, role: role) do |client, grouped_keys|
    values = client.call("MGET", *grouped_keys)
    grouped_keys.each_with_index do |key, i|
      results[key_to_index[key]] = values[i]
    end
  end
  
  return results
end

#mset(pairs, role: :master) ⇒ Object

Set multiple keys to multiple values. Redis will return a CROSSSLOT error if keys span multiple slots.



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 169

def mset(pairs, role: :master)
  return if pairs.empty?
  
  if pairs.is_a?(Hash)
    pairs = pairs.to_a.flatten
  end
  
  slot = slot_for(pairs.first)
  client = client_for(slot, role)
  
  return client.call("MSET", *pairs)
end

#msetnx(pairs, role: :master) ⇒ Object

Set multiple keys to multiple values, only if none exist. Redis will return a CROSSSLOT error if keys span multiple slots.



187
188
189
190
191
192
193
194
195
196
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 187

def msetnx(pairs, role: :master)
  keys = pairs.keys
  return 0 if keys.empty?
  
  flattened_pairs = pairs.keys.zip(pairs.values).flatten
  slot = slot_for(keys.first)
  client = client_for(slot, role)
  
  return client.call("MSETNX", *flattened_pairs)
end

#psetex(key, milliseconds, value, role: :master) ⇒ Object

Set the value and expiration in milliseconds of a key.



204
205
206
207
208
209
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 204

def psetex(key, milliseconds, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("PSETEX", key, milliseconds, value)
end

#set(key, value, update: nil, seconds: nil, milliseconds: nil, role: :master) ⇒ Object

Set the string value of a key.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 219

def set(key, value, update: nil, seconds: nil, milliseconds: nil, role: :master)
  arguments = []
  
  if seconds
    arguments << "EX" << seconds
  end
  
  if milliseconds
    arguments << "PX" << milliseconds
  end
  
  if update == true
    arguments << "XX"
  elsif update == false
    arguments << "NX"
  end
  
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("SET", key, value, *arguments)
end

#setbit(key, offset, value, role: :master) ⇒ Object

Set or clear the bit at offset in the string value stored at key.



248
249
250
251
252
253
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 248

def setbit(key, offset, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("SETBIT", key, offset, value)
end

#setex(key, seconds, value, role: :master) ⇒ Object

Set the value and expiration of a key.



261
262
263
264
265
266
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 261

def setex(key, seconds, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("SETEX", key, seconds, value)
end

#setnx(key, value, role: :master) ⇒ Object

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



273
274
275
276
277
278
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 273

def setnx(key, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("SETNX", key, value) == 1
end

#setrange(key, offset, value, role: :master) ⇒ Object

Overwrite part of a string at key starting at the specified offset.



286
287
288
289
290
291
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 286

def setrange(key, offset, value, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("SETRANGE", key, offset, value)
end

#strlen(key, role: :master) ⇒ Object

Get the length of the value stored in a key.



297
298
299
300
301
302
# File 'lib/protocol/redis/cluster/methods/strings.rb', line 297

def strlen(key, role: :master)
  slot = slot_for(key)
  client = client_for(slot, role)
  
  return client.call("STRLEN", key)
end