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:

redis-cli

If your Redis server is running on a different host or port, you can specify the host and port like this:

redis-cli -h <hostname> -p <port>

For example, to connect to a Redis server running on 192.168.1.100 at port 6379, you would use:

redis-cli -h 192.168.1.100 -p 6379

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.

PING

Expected output:

PONG

SET and GET

The SET command sets the value of a key, and the GET command retrieves the value of a key.

SET mykey "Hello, Redis!"
GET mykey

Expected output:

OK
"Hello, Redis!"

DEL

The DEL command deletes a key.

DEL mykey

Expected output:

(integer) 1

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.

KEYS *

Expected output (example):

1) "mykey"
2) "anotherkey"

EXISTS

The EXISTS command checks if a key exists.

EXISTS mykey

Expected output:

(integer) 1

EXPIRE

The EXPIRE command sets a timeout on a key. After the timeout, the key will be automatically deleted.

SET tempkey "This is temporary"
EXPIRE tempkey 10

Expected output:

OK
(integer) 1

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:

HELP <command>

For example, to get help on the SET command:

HELP SET

Expected output:

1) "SET key value [EX seconds] [PX milliseconds] [NX|XX]"
2) "Set the string value of a key"

Practical Exercises

Exercise 1: Basic Key-Value Operations

  1. Connect to the Redis CLI.
  2. Set a key student with the value John Doe.
  3. Retrieve the value of the key student.
  4. Delete the key student.
  5. Check if the key student exists.

Solution:

redis-cli
SET student "John Doe"
GET student
DEL student
EXISTS student

Exercise 2: Key Expiration

  1. Set a key session with the value active.
  2. Set an expiration of 5 seconds on the key session.
  3. Wait for 6 seconds.
  4. Check if the key session exists.

Solution:

redis-cli
SET session "active"
EXPIRE session 5
# Wait for 6 seconds
EXISTS session

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.

© Copyright 2024. All rights reserved