Ubuntu 16.04
Sponsored Link

Redis : Basic Usage#2
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.
⇒ http://redis.io/commands
[1] This is the basic Usage of Keys.
redis-cli -a 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) 1

# 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) 0

# 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 key02 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 key01 key02 
OK> mget key01 key02 
1) (nil)
2) "value01"

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

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

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

# delete all Keys on current Database> flushdb 

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

# process with reading data from stdout
root@dlp:~# echo 'test_words' | redis-cli -a password -x set key01 
root@dlp:~# redis-cli -a password get key01 
[2] It's possible to use CAS operation 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 
"value01"> multi 
OK> set key01 value02 
QUEUED> exec 
1) OK