Redis: CLI Usage
In this article I will introduce some usage of redis CLI.
As you may know, Redis has 5 data types – string, bitmaps, list, hash, set, sorted set
I will introduce some basic API of each data type.
General
$ redis-cli
To start a cli instance of redis
There are 16 different partition in redis. default on 0 when starting the CLI.
> set k hello
> get k
“hello”
> select 1
> get k
(nil)
> select 0
> get k
“hello”
string
String is the most common data type we use in Redis.
All commands can be found using help
> help @string
> help @generic
- NX/XX
> set k hello [NX XX]
NX: only set the value when it doesn’t exist
XX: can only update the value, cannot set the initial value of key
- MSET
Set multiple keys in a single command
> MSET k1 hello k2 world k3 !
> MGET k1 k2 k3
1) “hello” 2) “world” 3) “!”
- MSETNX
Atomic version of MSET, one fail will causing revert all command
- APPEND
Append new value to existing value
> set k1 hello
> append k1 “ redis”
> get k1
“hello redis”
- GETRANGE
This command can return the substring of data
> set k1 “hello world”
>getrange k1 6 10
“world”
> getrange k1 -5 -1
“world”
- SETRANGE
This command can modify partial string
> set k1 “hello world”
> get k1
“hello world”
> SETRANGE k1 6 “redi”
“hello redid”
- STRLEN
return the length of value
- TYPE
This command is the trickiest one,
> help type
Determines the type of value stored at a key.
> set k1 99
> set k2 “hello”
> type k1
string
> type k2
string
> object encoding k1
int
> object encoding k2
embstr
- Numerical Calculation
> set k1 99
> incr k1
> get k1
100
> incrby k1 11 # 111
> decr k1 # 110
> decrby k1 10 #100
> incrbyfloat k1 0.5 # 100.5
- How Redis interpret the encoding?
Redis will not try to encode the string, instead Redis only store them in bytes
> set k1 “中国”
> get k1
“\xe4\xb8\xad\xe5\x9b\xbd”
> exit
$redis-cli –raw # using the encoding
> get k1
“中国”
bitmap
Sometimes bitmap can play a big role.
Let take a look some example commands.
In redis, the default length of value is 8 bit
> setbit k1 1 1 // set the bit on index 1 to be 1: 01000000
> get k1
@
> strlen k1
1
> setbit k1 7 1 // 01000001
> get k1
“A”
> setbit k1 9 1 // 01000001 01000000
> get k1
“A@”
> strlen k1
2
- bitcount : count the how many bits are set to 1 in value
> bitcount k1 0 -1
3
- Redis also support bit operations
> bitop AND destkey k1 k2
List
- help command
> help @list
- Add element into a list
> lpush k1 1 2 3 4 5 // means insert 5 elements at the left end of list
> lrange k1 0 -1 // list all the elements in the list
1) “5” 2) “4” 3) “3” 4) “2” 5) “1”
> lindex k1 2
3
> lset k1 3 hello
> lindex k1 3
“hello”
- remove the elements from list
lrem [key] [count ] [element]
this command means: from the value of “key”, remove “count” of “elements”.
if the “count” is positive, then remove the element from the head / left of list
if the “count” is negative, then remove the element from the tail / right of list
> lpush k1 1 2 3 1 2 3 1 2 3
> lrange k1 0 -1
1) “3” 2) “2” 3) “1” 4) “3” 5) “2” 6) “1” 7) “3” 8) “2” 9) “1”
> lrem k1 2 1 // means, from the left of k1, remove two 1s
> lrange k1 0 -1
1) “3” 2) “2” 3) “3” 4) “2” 5) “3” 6) “2” 7) “1”
> lrem k1 -2 2 // means, from the right of k1, remove two 2s
> lrange k1 0 -1
1) “3” 2) “2” 3) “3” 4) “3” 5) “1”