Redis Commands

Organizing frequently used Redis commands

1. GET key

  • A command to retrieve the value of a key

  • If the key does not exist, it returns nil

2. SET key value

  • A command to SET a key with a string value

  • If the key already has a value set, it overwrites it

Options

SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX] [GET]
  • EX seconds

    • Can set the expire time (in seconds)

  • PX milliseconds

    • Set the expire time (in milliseconds)

  • NX

    • Setting to SET only when the key does not exist

  • XX

    • Setting to SET only when the key exists

  • KEEPTTL

    • Setting to keep the TTL that the key has

  • GET

    • If the key already exists,

      • Returns the set value

    • If the key does not exist,

      • Returns nil

Return value

  • Returns OK if the key is successfully SET

Examples

3. GETSET key value

  • SETs a value for the key and returns the previously stored value

  • Returns Error if the key exists but has no stored string value

Examples

4. EXPIRE key seconds

  • Sets a timeout on the key

    • When the timeout expires, the key is automatically deleted

  • The set timeout is cleared when the key's content is deleted or overwritten

    • The following commands clear the timeout:

      • DEL

      • SET

      • GETSET

      • All *STORE commands

    • That is, all other commands that alter values do not change the timeout

      • ex) The INCR command that increments a value does not affect the timeout

  • The timeout can be cleared by converting the key to a persistent key through the PERSIST command

  • Note that calling the EXPIRE command with a non-positive timeout deletes the key rather than expiring it!

    • The key event is del, not expired

Refreshing expires

  • You can call the EXPIRE command on a key that already has an expire set to configure a new timeout

    • In this case, the TTL (Time to Live) is updated to the newly set value

Return value

  • Returns 1 if the timeout is set

  • Returns 0 if the key does not exist

Examples

5. TTL key

  • Returns the TTL (Time To Live) of the timeout set on the key

    • Returns -2 if the key does not exist

    • Returns -1 if the key exists but no expire is set

6. INCRBY key increment

  • Increments the number stored at the key

  • If the key does not exist, sets it to 0 before executing the increment

Return value

  • Returns the incremented value

  • Returns Error if the key has a value of the wrong type that cannot be converted to an integer

Examples

7. DECRBY key decrement

  • Decrements the number stored at the key

  • If the key does not exist, sets it to 0 before executing the decrement

Return value

  • Returns the decremented value

  • Returns Error if the key has a value of the wrong type that cannot be converted to an integer

Examples

More to be added

Last updated