Protocol::Redis SourceProtocolRedisMethodsStrings

module 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.

Implementation

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

def bitcount(key, *range)

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

Implementation

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

def decr(key)

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

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).

Implementation

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

def get(key)

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

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).

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.

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).

Implementation

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

def incr(key)

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

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).

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).

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.

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.

Implementation

def mset(pairs)
	flattened_pairs = pairs.keys.zip(pairs.values).flatten
	
	call('MSET', *flattened_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.

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).

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).

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).

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).

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).

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.

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).

Implementation

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