Step by Step learning Redis
7 December, 2022
8
8
0
Contributors
Introduction
Salvatore Sanfilippo
•
•
•
•
•
Companies that use Redis
How does Redis work?
string
, a list
, a set
, a sorted set
, a hash
. These data types are supported natively by Redis and provide a rich set of commands for manipulating and querying the data.•
•
•
•
•
Installing Redis
Linux
Windows
macOS
Redis Server started
Using Redis
Basic Redis Commands
SET
command. For example, to set the key name
to the value John
, you can use the following command:OK
if the operation is successful.name
, you can use the GET
command:John
.DEL
command:1
if the operation is successful. Now, if you try to get the value of the key name
, it will return (nil)
.KEYS
command:DBSIZE
command:3
in this case.Setting Expiration Time
EXPIRE
command. For example, to set the expiration time of the key name
to 10 seconds, you can use the following command:1
if the operation is successful. Now, if you try to get the value of the key name
, it will return (nil)
after 10 seconds.TTL
command:-2
if the key does not exist, -1
if the key does not have an expiration time, or the number of seconds remaining before the key expires.Handling Lists
RPUSH
command. For example, to create a list named colors
and add the values red
, green
, and blue
to it, you can use the following command:RPUSH
refers to the right push operation. It will return 3
(the number of elements pushed) if the operation is successful.Note: Like RPUSH
there are other commands like LPUSH
(left push), LPOP
(left pop), and RPOP
(right pop) that can be used to push and pop elements from the list.
GET
. This is because you are dealing with lists, so you have to use the LRANGE
command:Handling Sets
SADD
command. For example, to create a set named employee_ids and add the values 1001, 1002, and 1003 to it, you can use the following command:SMEMBERS
command:SISMEMBER
command:SREM
command:Handling Hashes
HSET
command. For example, to create a hash named employee
and add the values id
, fname
, and lname
to it, you can use the following command:HGET
command:HKEYS
command:HVALS
command:HGETALL
command:HDEL
command:HEXISTS
command:flushall
. It will delete all the keys in the database. So, if you want to delete all the keys in the database, you can use the following command:Conclusion
•
•
•
SET
, GET
, DEL
, and KEYS
, to manage and manipulate data in Redis. By understanding and using these commands effectively, you can unlock the full potential of Redis to support your specific use cases.tutorials
tutorial
handsontutorial
develevate