Redis Commands (SET, GET, DEL, EXPIRE) 🎯

beginner
22 min

Redis Commands (SET, GET, DEL, EXPIRE) 🎯

Welcome to our comprehensive guide on Redis Commands! In this lesson, we'll delve into the essential Redis commands: SET, GET, DEL, and EXPIRE. These commands are fundamental for anyone looking to work with Redis, a popular open-source, in-memory data structure store.

What is Redis? 📝

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store, used as a database, cache, and message broker. It supports various data structures like strings, hashes, lists, sets, and more.

SET 💡

The SET command is used to store a key-value pair in Redis.

bash
SET key value
  • key: The unique name of the key
  • value: The value to be associated with the key

Example:

bash
SET name John

In this example, we're storing the key name with the value John.

GET 💡

The GET command is used to retrieve the value associated with a key.

bash
GET key
  • key: The name of the key

Example:

bash
GET name

This command will return the value John, which is associated with the key name.

DEL 💡

The DEL command is used to delete a key-value pair in Redis.

bash
DEL key
  • key: The name of the key to be deleted

Example:

bash
DEL name

This command will delete the key-value pair associated with the key name.

EXPIRE 💡

The EXPIRE command is used to set the time to live (TTL) for a key in seconds.

bash
EXPIRE key seconds
  • key: The name of the key
  • seconds: The time in seconds after which the key will be deleted

Example:

bash
EXPIRE name 60

In this example, we're setting the TTL for the key name to 60 seconds. After 60 seconds, the key will be automatically deleted from Redis.

Quick Quiz
Question 1 of 1

What command is used to store a key-value pair in Redis?

Quick Quiz
Question 1 of 1

What command is used to retrieve the value associated with a key in Redis?

Quick Quiz
Question 1 of 1

What command is used to delete a key-value pair in Redis?

Quick Quiz
Question 1 of 1

What command is used to set the time to live (TTL) for a key in Redis?

Stay tuned for our next lesson, where we'll explore more Redis commands and learn how to use them effectively in real-world projects! 🎉

📝 Remember: The SET, GET, DEL, and EXPIRE commands are fundamental for working with Redis. Use these commands to store, retrieve, delete, and manage the time to live of key-value pairs in Redis.

Happy learning, and see you in the next lesson! 🎯🎉