CentOS Stream 8
Sponsored Link

Redis 6 : Basic Operation for Database2021/06/28

This is the Basic Usage of Redis with [redis-cli] client program.
Following examples are basic one, you can see more commands on Official Site below.
⇒ https://redis.io/commands
[1] This is the basic Usage of Keys.
[root@www ~]#
redis-cli> auth password 

# set Value of a Key> set key01 value01 

# get Value of a Key> get key01 

# delete a Key> del key01 
(integer) 1

# determine if a Key exists or not (1 means true)> exists key01 
(integer) 0

# set Value of a Key only when the Key does not exist yet
# integer 0 means Value is not set because the Key already exists> setnx key01 value02 
(integer) 1

# set Value of a Key with expiration date (60 means Value will expire after 60 second)> setex key01 60 value01 

# set expiration date to existing Key> expire key01 30 
(integer) 1

# add Values to a Key> append key01 value02 
(integer) 15

# get substring of Value of a Key : [Key] [Start] [End]> substr key01 0 3 
"valu"> set key02 1 

# increment integer Value of a Key> incr key02 
(integer) 2

# increment integer Value of a Key by specified value> incrby key02 100 
(integer) 102

# decrement integer Value of a Key> decr key02 
(integer) 101

# decrement integer Value of a Key by specified value> decrby key02 51 
(integer) 50

# set values of some Keys> mset key01 value01 key02 value02 key03 value03 

# get some Values of Keys> mget key01 key02 key03 
1) "value01"
2) "value02"
3) "value03"

# rename existing Key> rename key03 key04 
OK> mget key01 key02 key03 key04 
1) "value01"
2) "value02"
3) (nil)
4) "value03"

# rename existing Key but if renamed Key already exists, command is not run> renamenx key01 key02 
(integer) 0> mget key01 key02 key03 key04 
1) "value01"
2) "value02"
3) (nil)
4) "value03"

# get number of Keys on current Database> dbsize 
(integer) 3

# move a key to another Database> move key04 1 
(integer) 1> select 1 
OK[1]> get key04 

# delete all Keys on current Database> flushdb 

# delete all Keys on all Database> flushall 
OK> quit 

# process with reading data from stdout
[root@www ~]# echo 'test_words' | redis-cli -a password --no-auth-warning -x set key01 
[root@www ~]# redis-cli -a password --no-auth-warning get key01 
[2] It's possible to use CAS operation (Check And Set) with watch command on Redis.
If another process changed value of the Key between multi - exec, change is not applied to the Key.
# watch a key> watch key01 
OK> get key01 
"test_words\n"> multi 
OK> set key01 value02 
QUEUED> exec 
1) OK
Matched Content