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

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

Instance Method Summary collapse

Instance Method Details

#append(key, value) ⇒ Object

Append a value to a key. O(1). The amortized time complexity is O(1) assuming the appended value is small and the already present value is of any size, since the dynamic string library used by Redis will double the free space available on every reallocation.

Parameters:

  • key (Key)
  • value (String)

See Also:



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

def append(key, value)
  return call('APPEND', key, value)
end

#bitcount(key, *range) ⇒ Object

Count set bits in a string. O(N).

Parameters:

  • key (Key)

See Also:



39
40
41
# File 'lib/protocol/redis/methods/strings.rb', line 39

def bitcount(key, *range)
  return call('BITCOUNT', key, *range)
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one. O(1).

Parameters:

  • key (Key)

See Also:



46
47
48
# File 'lib/protocol/redis/methods/strings.rb', line 46

def decr(key)
  return call('DECR', key)
end

#decrby(key, decrement) ⇒ Object

Decrement the integer value of a key by the given number. O(1).

Parameters:

  • key (Key)
  • decrement (Integer)

See Also:



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

def decrby(key, decrement)
  return call('DECRBY', key, decrement)
end

#get(key) ⇒ Object

Get the value of a key. O(1).

Parameters:

  • key (Key)

See Also:



61
62
63
# File 'lib/protocol/redis/methods/strings.rb', line 61

def get(key)
  return call('GET', key)
end

#getbit(key, offset) ⇒ Object

Returns the bit value at offset in the string value stored at key. O(1).

Parameters:

  • key (Key)
  • offset (Integer)

See Also:



69
70
71
# File 'lib/protocol/redis/methods/strings.rb', line 69

def getbit(key, offset)
  return call('GETBIT', key, offset)
end

#getrange(key, start_index, end_index) ⇒ Object

Get a substring of the string stored at a key. O(N) where N is the length of the returned string. The complexity is ultimately determined by the returned length, but because creating a substring from an existing string is very cheap, it can be considered O(1) for small strings.

Parameters:

  • key (Key)
  • start (Integer)
  • end (Integer)

See Also:



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

def getrange(key, start_index, end_index)
  return call('GETRANGE', key, start_index, end_index)
end

#getset(key, value) ⇒ Object

Set the string value of a key and return its old value. O(1).

Parameters:

  • key (Key)
  • value (String)

See Also:



86
87
88
# File 'lib/protocol/redis/methods/strings.rb', line 86

def getset(key, value)
  return call('GETSET', key, value)
end

#incr(key) ⇒ Object

Increment the integer value of a key by one. O(1).

Parameters:

  • key (Key)

See Also:



93
94
95
# File 'lib/protocol/redis/methods/strings.rb', line 93

def incr(key)
  return call('INCR', key)
end

#incrby(key, increment) ⇒ Object

Increment the integer value of a key by the given amount. O(1).

Parameters:

  • key (Key)
  • increment (Integer)

See Also:



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

def incrby(key, increment)
  return call('INCRBY', key, increment)
end

#incrbyfloat(key, increment) ⇒ Object

Increment the float value of a key by the given amount. O(1).

Parameters:

  • key (Key)
  • increment (Double)

See Also:



109
110
111
# File 'lib/protocol/redis/methods/strings.rb', line 109

def incrbyfloat(key, increment)
  return call('INCRBYFLOAT', key, increment)
end

#mget(key, *keys) ⇒ Object

Get the values of all the given keys. O(N) where N is the number of keys to retrieve.

Parameters:

  • key (Key)

See Also:



116
117
118
# File 'lib/protocol/redis/methods/strings.rb', line 116

def mget(key, *keys)
  return call('MGET', key, *keys)
end

#mset(pairs) ⇒ Object

Set multiple keys to multiple values. O(N) where N is the number of keys to set.



122
123
124
125
# File 'lib/protocol/redis/methods/strings.rb', line 122

def mset(pairs)
  flattened_pairs = pairs.keys.zip(pairs.values).flatten
  return call('MSET', *flattened_pairs)
end

#msetnx(pairs) ⇒ Object

Set multiple keys to multiple values, only if none of the keys exist. O(N) where N is the number of keys to set.



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

def msetnx(pairs)
  flattened_pairs = pairs.keys.zip(pairs.values).flatten
  return call('MSETNX', *flattened_pairs)
end

#psetex(key, milliseconds, value) ⇒ Object

Set the value and expiration in milliseconds of a key. O(1).

Parameters:

  • key (Key)
  • milliseconds (Integer)
  • value (String)

See Also:



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

def psetex(key, milliseconds, value)
  return set key, value, milliseconds: milliseconds
end

#set(key, value, **options) ⇒ Object

Set the string value of a key. O(1).

Parameters:

  • key (Key)
  • value (String)
  • expiration (Enum)
  • condition (Enum)

See Also:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/protocol/redis/methods/strings.rb', line 149

def set(key, value, **options)
  arguments = []

  if options.has_key? :seconds
    arguments << 'EX'
    arguments << options[:seconds]
  end

  if options.has_key? :milliseconds
    arguments << 'PX'
    arguments << options[:milliseconds]
  end

  if options[:condition] == :nx
    arguments << 'NX'
  elsif options[:condition] == :xx
    arguments << 'XX'
  end

  return call('SET', key, value, *arguments)
end

#setbit(key, offset, value) ⇒ Object

Sets or clears the bit at offset in the string value stored at key. O(1).

Parameters:

  • key (Key)
  • offset (Integer)
  • value (Integer)

See Also:



176
177
178
# File 'lib/protocol/redis/methods/strings.rb', line 176

def setbit(key, offset, value)
  return call('SETBIT', key, offset, value)
end

#setex(key, seconds, value) ⇒ Object

Set the value and expiration of a key. O(1).

Parameters:

  • key (Key)
  • seconds (Integer)
  • value (String)

See Also:



185
186
187
# File 'lib/protocol/redis/methods/strings.rb', line 185

def setex(key, seconds, value)
  return set key, value, seconds: seconds
end

#setnx(key, value) ⇒ Object

Set the value of a key, only if the key does not exist. O(1).

Parameters:

  • key (Key)
  • value (String)

See Also:



193
194
195
# File 'lib/protocol/redis/methods/strings.rb', line 193

def setnx(key, value)
  return set key, value, condition: :nx
end

#setrange(key, offset, value) ⇒ Object

Overwrite part of a string at key starting at the specified offset. O(1), not counting the time taken to copy the new string in place. Usually, this string is very small so the amortized complexity is O(1). Otherwise, complexity is O(M) with M being the length of the value argument.

Parameters:

  • key (Key)
  • offset (Integer)
  • value (String)

See Also:



202
203
204
# File 'lib/protocol/redis/methods/strings.rb', line 202

def setrange(key, offset, value)
  return call('SETRANGE', key, offset, value)
end

#strlen(key) ⇒ Object

Get the length of the value stored in a key. O(1).

Parameters:

  • key (Key)

See Also:



209
210
211
# File 'lib/protocol/redis/methods/strings.rb', line 209

def strlen(key)
  return call('STRLEN', key)
end