Redis Common Command
Bài đăng này đã không được cập nhật trong 4 năm

In my previous article, I wrote about Intergrate Redis on Rails which I used redis to store user token. However, it didn't metion about its basic command line.
Therefore, in this article I am going to talk about some of those basic command line of redis which we are commonly use for our development.
You can run redis command line on redis web site for experiment or you can run on our server after you already set it up. For this article, we are going to run from our local PC. I assume that you do have redis set up on your server. If you don't, you can set up follow its official web site or my previous article.
To get start we need log in to redis console via this command:
redis-cli
Now let's talk about those command line one by one:
-
SETSETis very useful command, we use this command to set value to key. And if key already store value, it will overwrite with new value.With
SETcommand we can pass some option such as:EXseconds -- Set the specified expire time, in seconds.PXmilliseconds -- Set the specified expire time, in milliseconds.NX-- Only set the key if it does not already exist.XX-- Only set the key if it already exist.
Command
SETwill returnOKif it is execute correctly.
-
GETWe use this command to get value of key which we apply via
SETor other methods.It will return
nilif the apply key have no value.
-
DELWe this command for delete the given keys. The return value is the total number of key which it delete.

-
EXPIREThis command is use to set timeout to exist key, and the key will delete when it reach the limit time.
EXPIREcommand will return 1 if the timeout was set, or 0 if key doesn't exist or timeout was not set.
-
TTLThis command is use to check the remaining time to live of a key that has a timeout.
This command will return the remain time,or -2 if the key does not exist, or -1 if the key exists but has no associated expire.

-
EXISTSThis command is use to check if key exist. It will return 1 if key exist or 0 if it is not exist.

-
KEYSIt use to find all keys which is match its parttern. It is returning the list of keys.

-
HELPWhen you get stack on some command line, you can use
HELPcommand which it is verry useful command, because it can guide you to explore other redis command.Ex: when we want to know how to use redis
SETcommand, we can type
HELP SET
and it will tell you the argument needed and the summary of the command.

Conclusion
In this article we do talk about a few command line of redis. However, there are ton of command line out there which waiting you to explore. So you if you are getting interested on redis, you can visit its official web site to gain more knowledge.
All rights reserved