Redis - Keys
Redis allows the storage and retrieval of data in key-value pairs, where the values can be integers, strings, booleans, floats, etc. Keys in Redis are used to perform operations such as creating, storing, retrieving, updating, and deleting data to access or manage the values stored.
Syntax:
127.0.0.1:6379> COMMAND key_name
Here, the command can be any command like SET, GET, etc.
Syntax and Commands of Redis Keys
1. SET
Returns OK if key value is set
Syntax: SET key value
Example:
2. GET
Returns value for the respective key
Syntax: GET key
Example:
3. KEYS
Different patterns can be used to retrieve the keys, some syntax are given below
? - for single character
* - for all or remaining
[ae] - matches with either 'a' or 'e'
[^e] - matches with any character except 'e'
[a-c] - if matched with character between 'a' and 'c'
Syntax: KEYS pattern
Example:
4. RENAME
Renames the key name and returns OK
Syntax: RENAME key new_key
Example:
5. TYPE
Returns the type of key
Syntax: TYPE key
Example:
6. EXISTS
Returns 1 if exists and 0 if not
Syntax: EXISTS key [key ...]
Example:
7. DEL
Returns with 1 if deleted and 0 if key does not exists
Syntax: DEL key [key ...]
Example:
8. EXPIRE
Expires the key within the given seconds and returns with 1 if expiration done and 0 if key does not exists
Syntax: EXPIRE key seconds
Example:
Subscribe to my newsletter
Read articles from Lavleen Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Lavleen Agrawal
Lavleen Agrawal
Software Developer