12/08/2018, 13:33

Redis Common Command

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 ...

82.png

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:

  • SET

    SET is 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 SET command we can pass some option such as:

    • EX seconds -- Set the specified expire time, in seconds.
    • PX milliseconds -- 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 SET will return OK if it is execute correctly.

    Screen Shot 2016-06-25 at 1.54.32 PM.png

  • GET

    We use this command to get value of key which we apply via SET or other methods.

    It will return nil if the apply key have no value.

    Screen Shot 2016-06-25 at 1.59.08 PM.png

  • DEL

    We this command for delete the given keys. The return value is the total number of key which it delete.

    Screen Shot 2016-06-25 at 2.04.18 PM.png

  • EXPIRE

    This command is use to set timeout to exist key, and the key will delete when it reach the limit time.

    EXPIRE command will return 1 if the timeout was set, or 0 if key doesn't exist or timeout was not set.

    Screen Shot 2016-06-25 at 2.11.35 PM.png

  • TTL

    This 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.

    Screen Shot 2016-06-25 at 2.18.01 PM.png

  • EXISTS

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

    Screen Shot 2016-06-25 at 3.15.00 PM.png

  • KEYS

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

    Screen Shot 2016-06-25 at 3.50.45 PM.png

  • HELP

    When you get stack on some command line, you can use HELP command 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 SET command, we can type

        HELP SET
and it will tell you the argument needed and the summary of the command.

![Screen Shot 2016-06-25 at 1.42.26 PM.png](/uploads/b2daedc1-bd1b-4e3d-baaa-b95795577faa.png)

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.

0