Introduction to Redis CLI Commands
Redis command line interface is an initiative terminal program that is used to communicate commands between the user and the Redis server. It has main modes like interactive mode called read print loop where the user gives commands and gets replies and other command modes where Redis CLI comes into the picture and has added arguments where the reply is fetched to get standard output. In interactive mode, basic line editing functions are available in Redis CLI which gives a normal typing experience.
- Redis can be operated using two modes, interactive and command mode.
- Redis CLI command mode checks the latency of the Redis server.
- It reviews the scheduler latency of the host machine.
- It checks the stats on key hits using LRU workloads.
- It uses escaped string values and quoted strings to delimit the arguments using whitespace characters.
Overview of Redis CLI Command
Redis CLI is an open-source, free, in-memory database that is used to save the data as key-value pairs. So by using Redis, there are many in-built commands with a CLI interface and it is easy to use. By using this there are many special modes to install. The user can simulate replicas and give the replica stream to which it is received as primary and can also look at the display status and latency of the Redis server. The user can request latency frequency and a sample of ASCII spectrogram. There is an in-built command for each operation that makes Redis a unique choice. In Redis, all sorts of data structures can be implied like a sorted set, hash, list, set, and strings. But no float data or integer can be included. The string commands enable the user to compute string numbers as values.
How to Use Redis CLI Command?
To execute a Redis command and fetch the standard output at any terminal, the user can work on commands to segregate arguments.
$ Redis CLI INCR myname
The output of this command will be 6. Because the Redis reply can be given in any form like arrays, integers, strings, errors, nil, etc. The reply is executed between parentheses because the output can be used again as input to other commands or it signifies file redirection.
Redis CLI displays any extra information required for human readability to detect the standard output on the terminal. For all the other output, it will enable the raw output mode.
$ Redis-CLI INCR myname > /tmp / view.txt $ cat /tmp/ view.txt
How does Redis CLI Command Work?
If Redis CLI gives any command, the whitespace characters delimit the arguments and interactive mode, and newline gives the command for execution and parsing. To make input string values, which have non-executable characters or whitespace, the user can use escaped strings or quoted ones. Quoted values should be enclosed in single or double quotation marks and escape values are used to print not executable characters.
The escape sequence has a backslash followed by escape characters. The double-quoted strings that support the escape sequence are \ ‘’ as double quote, \n as new line, \r as carriage return, \t as horizontal tab, /b as backspace, /a as alert.
Single quotes presume it as literal and enable the escape sequence, \’ as a single quote or \\ as backlash.
127.0.0.1:6379> SET myname "Hello\myWorld" OK 127.0.0.1:6379> GET myname
Redis CLI Command Installation
These are the below steps to install Redis:
1. The system packages should be updated.
$sudo apt-get update
Then it should be upgraded and refresh the environment.
$ sudo apt –get upgrade
2. Install Redis package using.
$ sudo apt –get install Redis server
3. Start and enable the Redis service.
$ sudo systemctl enable Redis service $ sudo systemctl start Redis service
4. Once the Redis is installed, check it by executing the CLI command.
$ Redis –CLI
The output should be like this:
So, this is the port number and IP address of the Redis server.
5. Navigate to Redis CLI and execute all the Redis commands and check the database by using ping, to test that the server is up. If it is up, it gives output as PONG.
6. Connect to Redis CLI using a custom port. Redis server can also be executed as the customized port in some instances. But if it is not given, the Redis CLI uses the default port as 6379.
7. Redis offers the user sixteen databases from 0 to 15 indexes. So, he can switch to the required database using the select command.
127.0.0.1.6379 > select 12
Hence these are the steps to install Redis on the local machine.
Examples of Redis CLI
Here are the standard examples of Redis CLI. They are get and set, setex and psetex, setnx and setxx, del, append, strlen, setrange, getrange, incr, incrby, incrbyfloat, mset, mget, msetnx, decr, and decrby.
Use set to assign a value to key.
Use get to retrieve the value from a key.
127.0.0.1:6379 > SET site "educba"
127.0.0.1:6379 > GET site
If there is no key, the output will be nil.
If a key doesn’t exist, you’ll see “nil” as the output.
127.0.0.1:6379 > GET hike
Given below are the FAQs mentioned:
Q1. How to delete a key?
Answer: By using the DEL option, the user can delete a key and the output will signify how many keys are deleted.
Q2. How to check the Redis database?
Answer: The Redis server has a default 16 database and executes the Redis CLI config get the database. Using interactive mode, the number of the database is viewed within square braces.
Q3. How to check all the available Redis keys?
Answer: To view all the keys in the Redis data store, using keys commands, it follows the specific pattern and searches for all the keys that go with the given pattern. It is also done with an asterisk to get all the matching values.
Multiple operations can be executed with Redis CLI command mode which is time-saving and easy to use and manage. Hence it is efficient for higher dimensional data management.
This is a guide to Redis CLI Commands. Here we discuss the introduction, and how to use redis CLI command. working and examples. You may also have a look at the following articles to learn more –