Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. In this tutorial, we’ll cover some commonly used Redis commands with examples.
Installing Redis
Before we get started, ensure you have Redis installed. You can install it on Linux using:
sudo apt update
sudo apt install redis-server
For other operating systems, you can follow the official installation guide.
Connecting to Redis
After installing Redis, you can connect to it using the redis-cli
command:
redis-cli
This will open the Redis command-line interface where you can execute various commands.
Common Redis Commands
1. SET and GET
The SET
command sets the value of a key, and GET
retrieves the value of a key.
SET mykey "Hello Redis"
GET mykey
Output:
"OK"
"Hello Redis"
2. KEYS
The KEYS
command is used to list all keys matching a pattern.
SET user:1:name "Alice"
SET user:2:name "Bob"
KEYS user:*
Output:
1) "user:1:name"
2) "user:2:name"
3. DEL
The DEL
command is used to delete a key.
DEL mykey
4. INCR and DECR
INCR
increments the integer value of a key by one, and DECR
decrements it by one.
SET counter 10
INCR counter
DECR counter
Output:
(integer) 11
(integer) 10
5. EXPIRE
EXPIRE
sets a key’s time to live in seconds.
SET tempkey "I will expire"
EXPIRE tempkey 30
6. LPUSH and LRANGE
LPUSH
adds an element to the head of a list, and LRANGE
retrieves elements from the list.
LPUSH mylist "one"
LPUSH mylist "two"
LPUSH mylist "three"
LRANGE mylist 0 -1
Output:
1) "three"
2) "two"
3) "one"
7. HMSET and HGETALL
HMSET
sets multiple fields of a hash, and HGETALL
retrieves all fields and values of a hash.
HMSET user:1 name "Alice" age 30
HGETALL user:1
Output:
1) "name"
2) "Alice"
3) "age"
4) "30"
8. SADD and SMEMBERS
SADD
adds members to a set, and SMEMBERS
retrieves all members of a set.
SADD myset "one"
SADD myset "two"
SADD myset "three"
SMEMBERS myset
Output:
1) "one"
2) "two"
3) "three"
9. ZADD and ZRANGE
ZADD
adds members with scores to a sorted set, and ZRANGE
retrieves elements in a sorted set by index.
ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZADD myzset 3 "three"
ZRANGE myzset 0 -1 WITHSCORES
Output:
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
10. FLUSHALL
FLUSHALL
deletes all keys from all databases.
FLUSHALL
These are just a few of the many commands available in Redis. For a complete list of commands and their usage, refer to the Redis Command Reference.
Conclusion
Redis is a powerful tool with a wide range of commands for managing various data structures. In this tutorial, we covered some basic and commonly used commands. Remember to always handle sensitive data with care, especially in production environments. For more advanced usage and features, refer to the Redis Documentation.
Learn More
Sure, here’s a list of 50 URLs related to Redis commands tutorials and resources:
- Redis Commands
- Redis Commands Tutorial on Tutorialspoint
- Redis Commands Cheat Sheet
- Redis Commands on RedisGreen
- Redis Commands with Examples on GeeksforGeeks
- Redis Commands Explained on Redis.io Blog
- Redis Command Line Tutorial on YouTube
- Redis Commands – An Overview
- Redis Commands and Operations
- Redis Commands Tutorial on Guru99
- Redis Command Reference
- Redis Commands with Examples on Programiz
- Redis Command Reference
- Redis Commands Documentation on Baeldung
- Redis Commands on DigitalOcean
- Redis Commands Tutorial on W3schools
- Redis Commands with Examples on Redis.io
- Redis Commands and Data Types
- Redis Commands with Examples on Edureka
- Redis Commands Quick Reference on GitHub
- Redis Commands and Data Types on JavaTpoint
- Redis Commands Overview on Tutorialbrain
- Redis Commands on DataFlair
- Redis Commands and Operations on Sanfoundry
- Redis Commands Cheat Sheet on LeetCode
- Redis Commands Tutorial on LinuxHint
- Redis Commands and Examples on Redis.io
- Redis Commands and Use Cases on InfoQ
- Redis Commands and Data Structures
- Redis Commands with Syntax on Fresh2Refresh
- Redis Commands and Tutorial on JournalDev
- Redis Commands and Introduction on ZetCode
- Redis Commands Explained with Examples on Browserling
- Redis Commands and Data Types on Codecademy
- Redis Commands Tutorial on Studytonight
- Redis Commands and Redis Data Types on TutorialsTeacher
- Redis Commands Overview on RedisGreen
- Redis Commands and Examples on Spring Framework Guru
- Redis Commands Tutorial on RedisGreen Blog
- Redis Commands and Transactions
- Redis Commands and Pipelining on CodeProject
- Redis Commands and Keys
- Redis Commands on Redis.io Documentation
- Redis Commands and Redisson
- Redis Commands and Lua Scripting
- Redis Commands Tutorial on Learning Journal
- Redis Commands with Real-Life Examples
- Redis Commands and Pub/Sub
- Redis Commands and HyperLogLog
- Redis Commands on Codecademy