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

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

Overview

Methods for managing Redis strings.

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. See <redis.io/commands/append> for more details.



15
16
17
# File 'lib/protocol/redis/methods/strings.rb', line 15

def append(key, value)
  call("APPEND", key, value)
end

#bitcount(key, *range) ⇒ Object

Count set bits in a string. O(N). See <redis.io/commands/bitcount> for more details.



22
23
24
# File 'lib/protocol/redis/methods/strings.rb', line 22

def bitcount(key, *range)
  call("BITCOUNT", key, *range)
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one. O(1). See <redis.io/commands/decr> for more details.



29
30
31
# File 'lib/protocol/redis/methods/strings.rb', line 29

def decr(key)
  call("DECR", key)
end

#decrby(key, decrement) ⇒ Object

Decrement the integer value of a key by the given number. O(1). See <redis.io/commands/decrby> for more details.



37
38
39
# File 'lib/protocol/redis/methods/strings.rb', line 37

def decrby(key, decrement)
  call("DECRBY", key, decrement)
end

#get(key) ⇒ Object

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



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

def get(key)
  call("GET", key)
end

#getbit(key, offset) ⇒ Object

Returns the bit value at offset in the string value stored at key. O(1). See <redis.io/commands/getbit> for more details.



52
53
54
# File 'lib/protocol/redis/methods/strings.rb', line 52

def getbit(key, offset)
  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. See <redis.io/commands/getrange> for more details.



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

def getrange(key, start_index, end_index)
  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). See <redis.io/commands/getset> for more details.



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

def getset(key, value)
  call("GETSET", key, value)
end

#incr(key) ⇒ Object

Increment the integer value of a key by one. O(1). See <redis.io/commands/incr> for more details.



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

def incr(key)
  call("INCR", key)
end

#incrby(key, increment) ⇒ Object

Increment the integer value of a key by the given amount. O(1). See <redis.io/commands/incrby> for more details.



84
85
86
# File 'lib/protocol/redis/methods/strings.rb', line 84

def incrby(key, increment)
  call("INCRBY", key, increment)
end

#incrbyfloat(key, increment) ⇒ Object

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



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

def incrbyfloat(key, increment)
  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. See <redis.io/commands/mget> for more details.



99
100
101
# File 'lib/protocol/redis/methods/strings.rb', line 99

def mget(key, *keys)
  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. See <redis.io/commands/mset> for more details.



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

def mset(pairs)
  if pairs.is_a?(Hash)
    pairs = pairs.to_a.flatten
  end
  
  call("MSET", *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. See <redis.io/commands/msetnx> for more details.



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

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

#psetex(key, milliseconds, value) ⇒ Object

Set the value and expiration in milliseconds of a key. O(1). See <redis.io/commands/psetex> for more details.



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

def psetex(key, milliseconds, value)
  call("PSETEX", key, milliseconds, value)
end

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

Set the string value of a key. O(1). See <redis.io/commands/set> for more details.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/protocol/redis/methods/strings.rb', line 136

def set(key, value, update: nil, seconds: nil, milliseconds: nil)
  arguments = []
  
  if seconds
    arguments << "EX" << seconds
  end
  
  if milliseconds
    arguments << "PX" << milliseconds
  end
  
  if update == true
    arguments << "XX"
  elsif update == false
    arguments << "NX"
  end
  
  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). See <redis.io/commands/setbit> for more details.



161
162
163
# File 'lib/protocol/redis/methods/strings.rb', line 161

def setbit(key, offset, value)
  call("SETBIT", key, offset, value)
end

#setex(key, seconds, value) ⇒ Object

Set the value and expiration of a key. O(1). See <redis.io/commands/setex> for more details.



170
171
172
# File 'lib/protocol/redis/methods/strings.rb', line 170

def setex(key, seconds, value)
  call("SETEX", key, seconds, value)
end

#setnx(key, value) ⇒ Object

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



179
180
181
# File 'lib/protocol/redis/methods/strings.rb', line 179

def setnx(key, value)
  call("SETNX", key, value) == 1
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. See <redis.io/commands/setrange> for more details.



188
189
190
# File 'lib/protocol/redis/methods/strings.rb', line 188

def setrange(key, offset, value)
  call("SETRANGE", key, offset, value)
end

#strlen(key) ⇒ Object

Get the length of the value stored in a key. O(1). See <redis.io/commands/strlen> for more details.



195
196
197
# File 'lib/protocol/redis/methods/strings.rb', line 195

def strlen(key)
  call("STRLEN", key)
end