Redis - Keys

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:

Screenshot-from-2023-08-05-11-05-32

Screenshot-from-2023-08-05-11-11-43

2. GET

Returns value for the respective key

Syntax: GET key

Example:

Screenshot-from-2023-08-05-11-10-19

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:

Screenshot-from-2023-08-05-11-20-09

4. RENAME

Renames the key name and returns OK

Syntax: RENAME key new_key

Example:

Screenshot-from-2023-08-05-12-00-26

5. TYPE

Returns the type of key

Syntax: TYPE key

Example:

Screenshot-from-2023-08-05-11-47-00

6. EXISTS

Returns 1 if exists and 0 if not

Syntax: EXISTS key [key ...]

Example:

Screenshot-from-2023-08-05-12-02-45

7. DEL

Returns with 1 if deleted and 0 if key does not exists

Syntax: DEL key [key ...]

Example:

Screenshot-from-2023-08-05-11-26-17

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:

Screenshot-from-2023-08-05-12-04-15