2.23.1.4. Redis-cli utility

Warning!

For the utility to work, you must connect Redis in your hosting account.

The Redis-cli utility is a complete Redis client and is used to manipulate data stored in the Redis database.

View quick documentation:

redis-cli -h

A Unix socket is used to connect to Redis. The command to connect looks like this:

redis-cli -s ~/.system/redis.sock

Key -s must be specified, without it you cannot connect to a Unix socket.

After connecting to a Redis socket, the following popular commands are available for use:

Data management

  • Print all recorded keys:
    keys '*'
  • Display all recorded keys that have in the name key_:
    keys key_*

    You can use symbols in the pattern:

    • * — any number of any characters.
    • ? — any one character.
  • Write data example_data with key key_example:
    set key_example "example_data"
  • Retrieve data recorded earlier with a key key_example:
    get key_example
  • Get the type of data stored in this key:
    type key_example
  • Rename key from key_example on key_another_example:
    rename key_example key_another_example
  • Check for the existence of a key:
    exists key_example

    If the key exists, it will output 1, if not - 0.

  • Delete data and key key_example:
    del key_example
  • Get key lifetime:
    ttl key_example

    By default, the output is -1, which means there is no lifetime limitation.

  • Install TTL and the data written in it, after which the key will be deleted:
    expire key_example

Command execution queue

  • Activate the mode of writing commands to the queue:
    multi

    This mode is useful for executing multiple commands one at a time.

  • Execute all commands written to the queue after multi:
    exec

    If an error occurs while specifying commands, then all entries in the queue will be discarded.

Operations on numbers

  • Increment, increasing the value of the data by one, written in the key key_example:
    incr key_example
  • Increment, increasing the data value by the number specified instead of increment:
    incrby key_example increment
  • Decrement, decrement of the data value by one, written in the key key_example:
    decr key_example
  • Decrement, increasing the data value by the number specified instead of decrement:
    decrby key_example decrement

String operations

  • Append to the end of the key data string key_example text test_text:
    append key_example "test_text"
  • Length of the written data string in the key:
    strlen key_example
  • Get a specific character range of a string:
    getrange key_example start end

    The start and end of the range are specified instead of start and end... For example, for the line 1234567890 command getrange key_example 2 6 willreturn 4567... The characters are counted from 0.

  • Replace the data written in the key, starting with the character under the number start:
    setrange key_example start "text"

    For example, the line 1234567890 command setrange key_example 2 "123" will change to 1212367890.

Working with lists

  • Add a new element with text to the end of the list test:
    rpush key_example "test"
  • List the elements of the list, starting with the element numbered start and ending with the number stop:
    lrange key_example start stop
  • Get the number of items in a list:
    llen key_example
  • Remove the first element of the list and get the data for the following:
    lpop key_example

The commands are described in more detail in official documentation.

Content