Module: Protocol::Redis::Methods::Strings
- Defined in:
- lib/protocol/redis/methods/strings.rb
Instance Method Summary collapse
-
#append(key, value) ⇒ Object
Append a value to a key.
-
#bitcount(key, *range) ⇒ Object
Count set bits in a string.
-
#decr(key) ⇒ Object
Decrement the integer value of a key by one.
-
#decrby(key, decrement) ⇒ Object
Decrement the integer value of a key by the given number.
-
#get(key) ⇒ Object
Get the value of a key.
-
#getbit(key, offset) ⇒ Object
Returns the bit value at offset in the string value stored at key.
-
#getrange(key, start_index, end_index) ⇒ Object
Get a substring of the string stored at a key.
-
#getset(key, value) ⇒ Object
Set the string value of a key and return its old value.
-
#incr(key) ⇒ Object
Increment the integer value of a key by one.
-
#incrby(key, increment) ⇒ Object
Increment the integer value of a key by the given amount.
-
#incrbyfloat(key, increment) ⇒ Object
Increment the float value of a key by the given amount.
-
#mget(key, *keys) ⇒ Object
Get the values of all the given keys.
-
#mset(pairs) ⇒ Object
Set multiple keys to multiple values.
-
#msetnx(pairs) ⇒ Object
Set multiple keys to multiple values, only if none of the keys exist.
-
#psetex(key, milliseconds, value) ⇒ Object
Set the value and expiration in milliseconds of a key.
-
#set(key, value, update: nil, seconds: nil, milliseconds: nil) ⇒ Object
Set the string value of a key.
-
#setbit(key, offset, value) ⇒ Object
Sets or clears the bit at offset in the string value stored at key.
-
#setex(key, seconds, value) ⇒ Object
Set the value and expiration of a key.
-
#setnx(key, value) ⇒ Boolean
Set the value of a key, only if the key does not exist.
-
#setrange(key, offset, value) ⇒ Object
Overwrite part of a string at key starting at the specified offset.
-
#strlen(key) ⇒ Object
Get the length of the value stored in a key.
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.
32 33 34 |
# File 'lib/protocol/redis/methods/strings.rb', line 32 def append(key, value) call('APPEND', key, value) end |
#bitcount(key, *range) ⇒ Object
Count set bits in a string. O(N).
39 40 41 |
# File 'lib/protocol/redis/methods/strings.rb', line 39 def bitcount(key, *range) call('BITCOUNT', key, *range) end |
#decr(key) ⇒ Object
Decrement the integer value of a key by one. O(1).
46 47 48 |
# File 'lib/protocol/redis/methods/strings.rb', line 46 def decr(key) call('DECR', key) end |
#decrby(key, decrement) ⇒ Object
Decrement the integer value of a key by the given number. O(1).
54 55 56 |
# File 'lib/protocol/redis/methods/strings.rb', line 54 def decrby(key, decrement) call('DECRBY', key, decrement) end |
#get(key) ⇒ Object
Get the value of a key. O(1).
61 62 63 |
# File 'lib/protocol/redis/methods/strings.rb', line 61 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).
69 70 71 |
# File 'lib/protocol/redis/methods/strings.rb', line 69 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.
78 79 80 |
# File 'lib/protocol/redis/methods/strings.rb', line 78 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).
86 87 88 |
# File 'lib/protocol/redis/methods/strings.rb', line 86 def getset(key, value) call('GETSET', key, value) end |
#incr(key) ⇒ Object
Increment the integer value of a key by one. O(1).
93 94 95 |
# File 'lib/protocol/redis/methods/strings.rb', line 93 def incr(key) call('INCR', key) end |
#incrby(key, increment) ⇒ Object
Increment the integer value of a key by the given amount. O(1).
101 102 103 |
# File 'lib/protocol/redis/methods/strings.rb', line 101 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).
109 110 111 |
# File 'lib/protocol/redis/methods/strings.rb', line 109 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.
116 117 118 |
# File 'lib/protocol/redis/methods/strings.rb', line 116 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.
122 123 124 125 126 |
# File 'lib/protocol/redis/methods/strings.rb', line 122 def mset(pairs) flattened_pairs = pairs.keys.zip(pairs.values).flatten 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.
130 131 132 133 134 |
# File 'lib/protocol/redis/methods/strings.rb', line 130 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).
141 142 143 |
# File 'lib/protocol/redis/methods/strings.rb', line 141 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).
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 151 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).
176 177 178 |
# File 'lib/protocol/redis/methods/strings.rb', line 176 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).
185 186 187 |
# File 'lib/protocol/redis/methods/strings.rb', line 185 def setex(key, seconds, value) call('SETEX', key, seconds, value) end |
#setnx(key, value) ⇒ Boolean
Set the value of a key, only if the key does not exist. O(1).
194 195 196 |
# File 'lib/protocol/redis/methods/strings.rb', line 194 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.
203 204 205 |
# File 'lib/protocol/redis/methods/strings.rb', line 203 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).
210 211 212 |
# File 'lib/protocol/redis/methods/strings.rb', line 210 def strlen(key) call('STRLEN', key) end |