In this section, we will cover the basics of using the Redis Command Line Interface (CLI). The Redis CLI is a powerful tool that allows you to interact with your Redis server directly from the command line. This is essential for performing administrative tasks, debugging, and testing your Redis commands.
Table of Contents
Connecting to Redis CLI
To start using the Redis CLI, you need to connect to your Redis server. Assuming you have Redis installed and running on your local machine, you can connect to the Redis CLI by simply typing:
If your Redis server is running on a different host or port, you can specify the host and port like this:
For example, to connect to a Redis server running on 192.168.1.100
at port 6379
, you would use:
Basic Commands
Once connected, you can start issuing commands. Here are some basic commands to get you started:
PING
The PING
command checks if the Redis server is running and responsive.
Expected output:
SET and GET
The SET
command sets the value of a key, and the GET
command retrieves the value of a key.
Expected output:
DEL
The DEL
command deletes a key.
Expected output:
Working with Keys
Redis keys are the fundamental building blocks for storing data. Here are some commands to manage keys:
KEYS
The KEYS
command lists all keys matching a given pattern.
Expected output (example):
EXISTS
The EXISTS
command checks if a key exists.
Expected output:
EXPIRE
The EXPIRE
command sets a timeout on a key. After the timeout, the key will be automatically deleted.
Expected output:
Using Help and Documentation
Redis CLI provides built-in help for commands. You can use the HELP
command to get information about any Redis command.
Command Help
To get help on a specific command, use:
For example, to get help on the SET
command:
Expected output:
Practical Exercises
Exercise 1: Basic Key-Value Operations
- Connect to the Redis CLI.
- Set a key
student
with the valueJohn Doe
. - Retrieve the value of the key
student
. - Delete the key
student
. - Check if the key
student
exists.
Solution:
Exercise 2: Key Expiration
- Set a key
session
with the valueactive
. - Set an expiration of 5 seconds on the key
session
. - Wait for 6 seconds.
- Check if the key
session
exists.
Solution:
Summary
In this section, we covered the basics of using the Redis CLI, including how to connect to the Redis server, execute basic commands, manage keys, and use the built-in help system. These foundational skills are essential for interacting with Redis and performing various administrative tasks. In the next module, we will dive deeper into Redis data structures and how to use them effectively.
Redis Course
Module 1: Introduction to Redis
Module 2: Redis Data Structures
Module 3: Redis Commands and Operations
Module 4: Redis Persistence
Module 5: Redis Security
Module 6: Redis Performance Optimization
Module 7: Redis Clustering and High Availability
Module 8: Redis Modules and Extensions
- Introduction to Redis Modules
- Popular Redis Modules
- Creating Custom Modules
- Using Redis with Other Technologies