Redis redis-cli

Installing redis-cli

The following shows how to install it with Homebrew in a macOS environment.

brew tap aoki/redis-cli
brew update && brew doctor
brew install redis-cli

github.com/aoki/homebrew-redis-cli

Server control

Connecting with redis-cli

If the host name and port number are omitted, it connects to localhost on port 6379.

$ redis-cli

Remote access can be done as follows.

$ redis-cli -h #{host name} -p #{port number}

Other main options are as follows.

  • -n: database number
  • -a: password
  • -s: socket
  • -u: server URL and other connection options can be set

Querying server information

redis-cli info
> info
# Server
redis_version:6.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:ffd199d8341c2d8f
redis_mode:standalone
... omitted ....

Querying connected clients

> client list

Disconnecting a connected client

> client kill 10.0.0.8:33333

Dumping all requests received after running the command

redis-cli monitor
> monitor
OK

Key operations

Setting a key and value

set {key name} {value}
> set key01 value01
OK

Querying a value by key

get {key name}
> get key01
value01

Querying key list

> keys *
key01

Deleting a key

> del key01
1

Checking whether a key exists, where 1 means true

exists {key name} {value}
> exists key01
0

Setting data only if the key does not already exist

setnx {key name} {value}
> setnx key01 value02
1
> setnx key01 value02
0

If the result is 0, it means the key exists and the data was not updated.

Renaming an existing key

rename {old key name} {new key name}
> rename key01 key02 
OK
> rename key03 key04
ERR no such key

Returning the number of keys in the current database

> dbsize
2

Moving a specific key to another database

> move key01 1
1

Selecting a DB number

> select 1
OK

Deleting all keys in the current database

> flushdb
OK

Deleting all keys in all databases

> flushall
OK

Regular expressions

The regular expressions available in Redis are only the following.

  • []: one of the characters inside the brackets
  • *: any string
  • ?: a single character

Examples

# Query all Key lists.
$ keys *

# Query keys that start with a number.
$ keys [0-9]*

Multiple Key

Setting multiple key values together

> mset key01 value01 key02 value02 key03 value03 
OK

Querying multiple key values at once

> mget key01 key02 key03
value01
value02
value03

expire

Setting a key with an expiration period

The following example sets it to 60 seconds.

> setex key01 60 value01 
OK

Setting expiration on an existing key

> expire key02 30

Math

Adding 1 to a key value

The following example sets key02 to 1 and increments it by 1.

> set key02 1 
OK
> incr key02 
2

Adding a specified number to a key value

> incrby key02 100 
102

Subtracting 1 from a key value

> decr key02 
101

Subtracting a specified number from a key value

> decrby key02 51 
50

List

Adding values to the head of a List

Multiple values can be specified separated by spaces.

> lpush list01 value01
1

Adding values to the end of a List

Multiple values can be specified separated by spaces.

> rpush list01 value02
2

Returning the number of List elements

> llen list01
2

Returning the element at a specified index in a List

> lindex list01 0
value01

Returning elements from a specified start index to end index in a List

> lrange list01 0 1
value01
value02

Changing the element at a specified index in a List to a specified value

> lset list01 1 value03
OK
> lindex list01 1
value03

Returning and deleting the first element of a List

> lpop list01
value01

Returning and deleting the last element of a List

> rpop list01
value03

Trimming a List to elements from a specified start index to end index

> ltrim list01 1 3
OK

Hash

This is a data format managed by a parent Key, child Key (field), and value. For example, if you use the parent Key as a product ID and child keys to store product attribute information, you can use it with a way of thinking close to RDB data management.

Adding a value to a specified Hash field

> hset hash01 field01 value01
(integer) 1

Returning the value of a specified Hash field

> hget hash01 field01
"value01"

Adding multiple fields to a Hash

> hmset hash01 field02 value02 field03 value03
OK

Returning the values of multiple Hash fields

> hmget hash01 field01 field02 field03
value01
value02
value03

Returning all Hash fields

> hkeys hash01
field01
field02
field03

Returning all values of Hash fields

> hvals hash01
value01
value02
value03

Adding a specified number to the value of a specified Hash field

> hincrby hash01 field04 100
101

Checking whether a specified Hash field exists

> hexists hash01 field01
1

Returning the number of Hash fields

> hlen hash01
4

Removing a specified Hash field

> hdel hash01 field04
1

Other operations

How to search for keys when keys cannot be used

scan 0 match devkuma-* count 1000

Key names can be searched with wildcards, and devkuma-* searches for keys that start with devkuma-.