Storing Strings in Redis: Utility Commands

Let’s look at some utility commands related to storing Strings in Redis.

KEYS command

If we need to find out what keys are stored in the Redis, we can use the keys command, as shown below. This command will return all the keys saved in the database. The syntax of this command is:

keys *

INCR command

We can use this command if we need to increment the value of a given key by 1. In the example below, we are increasing the stock price of oracle by 1. The syntax of this command is:

INCR key

INCRBY command

We can use this command if we need to increment the value for a given key by a particular number. In the example below, we are increasing the stock price of Oracle by 5. The syntax of this command is:

INCRBY key count

If the value is not an integer, then either the ERR value is not an integer or out of range error is thrown. If we try to increment a key that does not exist, then a new key with value 1, is created. Similarly, if we try to decrement a key that does not exist then a new key, with value -1, is created.

INCRBYFLOAT command

This command is used if we need to increment the value of a key by a floating point number. In the example below, we are incrementing the stock price of Oracle by 3.5 The syntax of this command is:

INCRBYFLOAT key floatValue

Please try these commands in the terminal below:

  • SET Apple 300
  • GET Apple
  • INCR Apple
  • INCRBY Apple 5
  • INCRBYFLOAT Apple 3.4
  • keys *

Get hands-on with 1400+ tech skills courses.