String commands operate on key-value pairs where the value is a byte string. Kora supports all standard Redis string operations with identical semantics.
GET
Get the value of a key.
Syntax
Return value
The value stored at the key, or null if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> GET mykey
"Hello"
redis-cli> GET nonexistent
(nil)
Time complexity: O(1)
SET
Set the value of a key with optional expiration and conditional flags.
Syntax
SET key value [EX seconds] [PX milliseconds] [NX|XX]
Set the key’s time to live in seconds
Set the key’s time to live in milliseconds
Only set the key if it does not already exist
Only set the key if it already exists
Return value
OK if SET was executed correctly, or null if the SET operation was not performed due to NX or XX conditions
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> SET mykey "World" EX 10
OK
redis-cli> SET newkey "value" NX
OK
redis-cli> SET newkey "value2" NX
(nil)
Time complexity: O(1)
APPEND
Append a value to a key.
Syntax
Return value
The length of the string after the append operation
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> APPEND mykey " World"
(integer) 11
redis-cli> GET mykey
"Hello World"
Time complexity: O(1)
STRLEN
Get the length of the value stored in a key.
Syntax
Return value
The length of the string, or 0 if the key does not exist
Examples
redis-cli> SET mykey "Hello World"
OK
redis-cli> STRLEN mykey
(integer) 11
redis-cli> STRLEN nonexistent
(integer) 0
Time complexity: O(1)
INCR
Increment the integer value of a key by one.
Syntax
The key containing an integer value
Return value
The value of the key after incrementing
Examples
redis-cli> SET counter "10"
OK
redis-cli> INCR counter
(integer) 11
redis-cli> INCR counter
(integer) 12
redis-cli> INCR newcounter
(integer) 1
Time complexity: O(1)
DECR
Decrement the integer value of a key by one.
Syntax
The key containing an integer value
Return value
The value of the key after decrementing
Examples
redis-cli> SET counter "10"
OK
redis-cli> DECR counter
(integer) 9
redis-cli> DECR counter
(integer) 8
Time complexity: O(1)
INCRBY
Increment the integer value of a key by a given amount.
Syntax
The key containing an integer value
The amount to increment by
Return value
The value of the key after incrementing
Examples
redis-cli> SET counter "10"
OK
redis-cli> INCRBY counter 5
(integer) 15
redis-cli> INCRBY counter -3
(integer) 12
Time complexity: O(1)
DECRBY
Decrement the integer value of a key by a given amount.
Syntax
The key containing an integer value
The amount to decrement by
Return value
The value of the key after decrementing
Examples
redis-cli> SET counter "10"
OK
redis-cli> DECRBY counter 3
(integer) 7
Time complexity: O(1)
INCRBYFLOAT
Increment the float value of a key by a given amount.
Syntax
The key containing a numeric value
The amount to increment by
Return value
The value of the key after incrementing
Examples
redis-cli> SET price "10.50"
OK
redis-cli> INCRBYFLOAT price 2.25
"12.75"
redis-cli> INCRBYFLOAT price -1.5
"11.25"
Time complexity: O(1)
MGET
Get the values of all given keys.
Syntax
One or more keys to retrieve
Return value
Array of values at the specified keys. For keys that do not exist, null is returned
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> SET key2 "World"
OK
redis-cli> MGET key1 key2 nonexistent
1) "Hello"
2) "World"
3) (nil)
Time complexity: O(N) where N is the number of keys to retrieve
MSET
Set multiple keys to multiple values.
Syntax
MSET key value [key value ...]
The value for the corresponding key
Return value
Examples
redis-cli> MSET key1 "Hello" key2 "World"
OK
redis-cli> GET key1
"Hello"
redis-cli> GET key2
"World"
Time complexity: O(N) where N is the number of keys to set
SETNX
Set the value of a key only if it does not exist.
Syntax
Return value
1 if the key was set, 0 if the key was not set
Examples
redis-cli> SETNX mykey "Hello"
(integer) 1
redis-cli> SETNX mykey "World"
(integer) 0
redis-cli> GET mykey
"Hello"
Time complexity: O(1)
GETSET
Set the string value of a key and return its old value.
Syntax
Return value
The previous value stored at key, or null if the key did not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> GETSET mykey "World"
"Hello"
redis-cli> GET mykey
"World"
Time complexity: O(1)
Note: GETSET is deprecated. Use SET with the GET option instead.
GETRANGE
Get a substring of the string stored at a key.
Syntax
The key containing the string
The start offset (0-based, negative values count from the end)
The end offset (inclusive, negative values count from the end)
Return value
The substring of the value
Examples
redis-cli> SET mykey "This is a string"
OK
redis-cli> GETRANGE mykey 0 3
"This"
redis-cli> GETRANGE mykey -3 -1
"ing"
redis-cli> GETRANGE mykey 0 -1
"This is a string"
Time complexity: O(N) where N is the length of the returned string
SETRANGE
Overwrite part of a string at a key starting at the specified offset.
Syntax
SETRANGE key offset value
The byte offset to start writing at
Return value
The length of the string after the modification
Examples
redis-cli> SET key1 "Hello World"
OK
redis-cli> SETRANGE key1 6 "Kora"
(integer) 11
redis-cli> GET key1
"Hello Kora!"
Time complexity: O(1) not counting the time to copy the new string in place
GETDEL
Get the value of a key and delete the key.
Syntax
The key to get and delete
Return value
The value of the key, or null if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> GETDEL mykey
"Hello"
redis-cli> GET mykey
(nil)
Time complexity: O(1)
GETEX
Get the value of a key and optionally set its expiration.
Syntax
GETEX key [EX seconds | PX milliseconds | EXAT timestamp | PXAT timestamp_ms | PERSIST]
Set the key’s time to live in seconds
Set the key’s time to live in milliseconds
Set the key to expire at a Unix timestamp (seconds)
Set the key to expire at a Unix timestamp (milliseconds)
Remove the time to live associated with the key
Return value
The value of the key, or null if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> GETEX mykey EX 60
"Hello"
redis-cli> TTL mykey
(integer) 60
Time complexity: O(1)
MSETNX
Set multiple keys to multiple values, only if none of the keys exist.
Syntax
MSETNX key value [key value ...]
The value for the corresponding key
Return value
1 if all keys were set, 0 if no key was set (at least one key already existed)
Examples
redis-cli> MSETNX key1 "Hello" key2 "World"
(integer) 1
redis-cli> MSETNX key1 "foo" key3 "bar"
(integer) 0
redis-cli> MGET key1 key2 key3
1) "Hello"
2) "World"
3) (nil)
Time complexity: O(N) where N is the number of keys to set
Redis Compatibility
Kora implements all standard Redis string commands with identical semantics. Commands behave exactly as documented in the Redis specification.