Redis - Basic Commands
SET
The SET command can be used to set a key-value pair. Learn More
Time Complexity: O(1)
Example usage:
To set expiration time (in second) for a key, the parameter EX can be used. The command TTL can be used to view the expiration of a key (Time to Live):
KEYS
Keys
The KEYS command was used in the past for listing the keys. It is no longer recommended to use the KEYS command, as it blocks the whole database operation until it outputs the data and can cause massive problems in production environments with lots of data (it's slow and takes large amount of memory with large dataset). Use SCAN instead.
The KEYS command is not recommended to be used in production, but can be used for testing with smaller amount of data. Learn More
Time Complexity: O(N) with N being the number of keys in the database.
Example usage:
SCAN
Keys
The KEYS command was used in the past for listing the keys. It is no longer recommended to use the KEYS command, as it blocks the whole database operation until it outputs the data and can cause massive problems in production environments with lots of data (it's slow and takes large amount of memory with large dataset). Use SCAN instead.
The SCAN command can be used to view the keys. The command uses cursor to navigate through the keys. You need to pass it the cursor starting position (initially 0) and can also pass some other extra options, like filter and count. Sorting / retrieving data in a specific order is not supported. Learn More
Time Complexity: O(1) for the call, and O(N) where N = number of keys in the database.
Example usage:
Example Output
Defaults to 10
By default, when not passing the COUNT parameter, it will list approximately 10 keys.
Example Output
Hint, or requirement
Note that the COUNT parameter can return more or less keys than specified. The COUNT is merely a recommendation for Redis and it can return back different amount of elements that you wanted it to, but it tries to be very close to the specified value.
DBSIZE
The DBSIZE command can be used to determine how many keys are stored in the database. Learn More
Time Complexity: O(1)
Example:
GET
The GET command can be used to get value for a key. Learn More
Time Complexity: O(1)
Example usage:
MGET & MSET
Time Complexity: O(N) where N is the amount of elements retrieved or set.
The MGET and MSET commands are the same as GET and SET, but they can be used to set or get multiple elements (M standing for multiple). They work the same as GET and SET but you can just pass them multiple elements.
DEL
The DEL command can be used to remove a key. Learn More
Time Complexity: Removing a single key is always O(1). If multiple keys are passed, then this operation works as O(N).
Example usage:
EXPIRE & TTL
The EXPIRE command can be used to set key to expire (to be removed) after given time. Learn More
The TTL command can be used to determine when will the key expire. Learn More
Time Complexity: O(1)
Example usage:
RENAME
The RENAME command can be used to rename a key. Learn More
Time Complexity: O(1)
Example usage:
TYPE & OBJECT ENCODING
The commands TYPE and OBJECT ENCODING can be used to determine the type and encoding of the given key.
Learn more about type, object encoding.
Time Complexity: O(1)
To determine the type of a key:
Notice that even the numbers that you have defined are being internally stored as string data types.
To actually determine the object encoding of the given string, we can use the OBJECT ENCODING command:
INCR & DECR
Strings stored as integers can be increment or decremented using the INCR, DECR, INCRBY, and DECRBY commands.
Learn more about INCR, DECR, INCRBY, DECRBY
Time Complexity: O(1)
To increment a value by one:
To decrement a value by one:
And to increment or decrement by a given value, we can use these commands:
STRLEN
The STRLEN command can be used to determine the count of characters in a string. Learn More
Time Complexity: O(1)
Example:
FLUSHDB
The FLUSHDB command can be used to remove all keys from the database. USE WITH CAUTION! Learn More
Time Complexity: O(N)
Example: