Redis is an open-source, in-memory data structure store that is used as a database, cache, and message broker. It is known for being fast, reliable, and easy to use. Redis processes data structures in-memory, which makes it extremely fast when compared to disk-based databases. Redis stores data persistently on disk, which ensures that data is not lost during power failures or server outages. Redis commands are grouped into different categories, including string manipulation, sorted sets, hashes, and more. This comprehensive guide will cover some of the most useful Redis commands and syntax.
Redis provides a variety of commands to manipulate strings. Strings in Redis are binary safe, which means that any kind of binary data can be stored in them.
The SET command is used to set the value of a key. If the key already exists, the value is overwritten.
SET key value [EX seconds] [PX milliseconds] [NX|XX]
In this example, we will set the value of a key called "user" to "John".
SET user John
The GET command is used to retrieve the value of a key.
GET key
In this example, we will retrieve the value of the key called "user".
GET user
Redis provides sorted sets, which are similar to sets, but each element has an associated score.
The ZADD command is used to add one or more members to a sorted set, or update the score for members that already exist.
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
In this example, we will add two members to a sorted set called "scores".
ZADD scores 10 John 20 Jane
The ZRANGE command is used to retrieve a range of members from a sorted set, by index.
ZRANGE key start stop [WITHSCORES]
In this example, we will retrieve the members with a score between 10 and 20 from a sorted set called "scores".
ZRANGE scores 0 -1 WITHSCORES
Redis provides hashes, which are similar to dictionaries, where each field is associated with a value.
The HSET command is used to set the value of a field in a hash.
HSET key field value
In this example, we will set the value of the field "age" in a hash called "user" to 30.
HSET user age 30
The HGET command is used to retrieve the value of a field in a hash.
HGET key field
In this example, we will retrieve the value of the field "age" from a hash called "user".
HGET user age
Redis is a powerful and versatile tool that can be used as a database, cache, and message broker. It provides a variety of commands for manipulating different data structures, including strings, sorted sets, and hashes. By understanding these commands and their syntax, you can make the most out of Redis and improve the performance of your applications.