module Strings
Methods for managing Redis strings.
Definitions
def append(key, value)
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 https://redis.io/commands/append for more details.
Signature
-
parameter
key
Key
-
parameter
value
String
Implementation
def append(key, value)
call("APPEND", key, value)
end
def bitcount(key, *range)
Count set bits in a string. O(N). See https://redis.io/commands/bitcount for more details.
Signature
-
parameter
key
Key
Implementation
def bitcount(key, *range)
call("BITCOUNT", key, *range)
end
def decr(key)
Decrement the integer value of a key by one. O(1). See https://redis.io/commands/decr for more details.
Signature
-
parameter
key
Key
Implementation
def decr(key)
call("DECR", key)
end
def decrby(key, decrement)
Decrement the integer value of a key by the given number. O(1). See https://redis.io/commands/decrby for more details.
Signature
-
parameter
key
Key
-
parameter
decrement
Integer
Implementation
def decrby(key, decrement)
call("DECRBY", key, decrement)
end
def get(key)
Get the value of a key. O(1). See https://redis.io/commands/get for more details.
Signature
-
parameter
key
Key
Implementation
def get(key)
call("GET", key)
end
def getbit(key, offset)
Returns the bit value at offset in the string value stored at key. O(1). See https://redis.io/commands/getbit for more details.
Signature
-
parameter
key
Key
-
parameter
offset
Integer
Implementation
def getbit(key, offset)
call("GETBIT", key, offset)
end
def getrange(key, start_index, end_index)
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 https://redis.io/commands/getrange for more details.
Signature
-
parameter
key
Key
-
parameter
start
Integer
-
parameter
end
Integer
Implementation
def getrange(key, start_index, end_index)
call("GETRANGE", key, start_index, end_index)
end
def getset(key, value)
Set the string value of a key and return its old value. O(1). See https://redis.io/commands/getset for more details.
Signature
-
parameter
key
Key
-
parameter
value
String
Implementation
def getset(key, value)
call("GETSET", key, value)
end
def incr(key)
Increment the integer value of a key by one. O(1). See https://redis.io/commands/incr for more details.
Signature
-
parameter
key
Key
Implementation
def incr(key)
call("INCR", key)
end
def incrby(key, increment)
Increment the integer value of a key by the given amount. O(1). See https://redis.io/commands/incrby for more details.
Signature
-
parameter
key
Key
-
parameter
increment
Integer
Implementation
def incrby(key, increment)
call("INCRBY", key, increment)
end
def incrbyfloat(key, increment)
Increment the float value of a key by the given amount. O(1). See https://redis.io/commands/incrbyfloat for more details.
Signature
-
parameter
key
Key
-
parameter
increment
Double
Implementation
def incrbyfloat(key, increment)
call("INCRBYFLOAT", key, increment)
end
def mget(key, *keys)
Get the values of all the given keys. O(N) where N is the number of keys to retrieve. See https://redis.io/commands/mget for more details.
Signature
-
parameter
key
Key
Implementation
def mget(key, *keys)
call("MGET", key, *keys)
end
def mset(pairs)
Set multiple keys to multiple values. O(N) where N is the number of keys to set. See https://redis.io/commands/mset for more details.
Implementation
def mset(pairs)
if pairs.is_a?(Hash)
pairs = pairs.to_a.flatten
end
call("MSET", *pairs)
end
def msetnx(pairs)
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 https://redis.io/commands/msetnx for more details.
Implementation
def msetnx(pairs)
flattened_pairs = pairs.keys.zip(pairs.values).flatten
call("MSETNX", *flattened_pairs)
end
def psetex(key, milliseconds, value)
Set the value and expiration in milliseconds of a key. O(1). See https://redis.io/commands/psetex for more details.
Signature
-
parameter
key
Key
-
parameter
milliseconds
Integer
-
parameter
value
String
Implementation
def psetex(key, milliseconds, value)
call("PSETEX", key, milliseconds, value)
end
def set(key, value, update: nil, seconds: nil, milliseconds: nil)
Set the string value of a key. O(1). See https://redis.io/commands/set for more details.
Signature
-
parameter
key
Key
-
parameter
value
String
-
parameter
expiration
Enum
-
parameter
update
Boolean, nil
If true, only update elements that already exist (never add elements). If false, don't update existing elements (only add new elements).
Implementation
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
def setbit(key, offset, value)
Sets or clears the bit at offset in the string value stored at key. O(1). See https://redis.io/commands/setbit for more details.
Signature
-
parameter
key
Key
-
parameter
offset
Integer
-
parameter
value
Integer
Implementation
def setbit(key, offset, value)
call("SETBIT", key, offset, value)
end
def setex(key, seconds, value)
Set the value and expiration of a key. O(1). See https://redis.io/commands/setex for more details.
Signature
-
parameter
key
Key
-
parameter
seconds
Integer
-
parameter
value
String
Implementation
def setex(key, seconds, value)
call("SETEX", key, seconds, value)
end
def setnx(key, value)
Set the value of a key, only if the key does not exist. O(1). See https://redis.io/commands/setnx for more details.
Signature
-
returns
Boolean
if the key was set.
-
parameter
key
Key
-
parameter
value
String
Implementation
def setnx(key, value)
call("SETNX", key, value) == 1
end
def setrange(key, offset, value)
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 https://redis.io/commands/setrange for more details.
Signature
-
parameter
key
Key
-
parameter
offset
Integer
-
parameter
value
String
Implementation
def setrange(key, offset, value)
call("SETRANGE", key, offset, value)
end
def strlen(key)
Get the length of the value stored in a key. O(1). See https://redis.io/commands/strlen for more details.
Signature
-
parameter
key
Key
Implementation
def strlen(key)
call("STRLEN", key)
end