redis support string, list, set, zset and hash five data types, but their operation is inconsistent .. The following syntax rough over again.
String type
Brief Description
- The most basic data types, and as Memcached
- Single-valued single value, the analogy of Java Map <String, String>.
- Binary safe, redis of stringkey contain any data, such as jpg image content and sequence of the target can be.
- In theory, string the value redis can be large, up to 512M.
Common Operations
set key value
Definition / covering a string keyget key
Gets the string value of the keydel key
Delete keyappen key value
Value appendstrlen key
Length valueexists key
Determine whether there is a keymove key n
The key is to migrate the current library n number databaseexpire key n
Key expiration time is set to n seconds
after expiredget key
andkeys *
will not see, equivalent deletedttl kyc
See how long expired target key
-1 means never expires (the default), - 2 representatives have expiredtype key
Check the type of bond, such as stringincr key
The values are +1 only valid for the numeric valuedecr key
For values -1, only valid numeric valueincrby key n
Value of + n, only valid numeric valuedecrby key n
Value of -n, only valid numeric valuegetrange key 0 3
Gets the value of the specified range (the first four characters), substr i.e.,getrange key 0 -1
retrieves allsetrange key ..
Value specified intervalsetex key n value
Defines a key value and simultaneously set the expiration time (n seconds value)setnx key value
If the key does not exist only to create that set if not exist, avoid covering- mset / mget / msetnx also define / obtain a plurality of keys
mset k1 v1 k2 v2 k3 v3 mget k1 k2 k3 msetnx k3 v3 k4 v4 #注:k3已存在,本次失败,k4也不会创建
- ...
List type
Brief Description
- Single-value multi-value, the analogy of Java Map <string, List>
- Is a string list, both sides can operate
- If the key does not exist, create a new list, then there is an additional element
- If the values of all the plurality of value is removed, the corresponding key is automatically removed
Common Operations
- lpush / rpush / lrange define a list and access key
lpush l1 v4 v3 v2 v1 #从左push rpush l1 v5 v6 v7 v8 #从右push lrange l1 0 -1 #获取l1值 lrange l1 0 4 #范围获取l1值
- lpop / rpop key popped from the left / right side (pull out and return)
llen key
The number of list elementslindex key value
String indexOf function of analogy- ...
Set Types
Brief Description
- List and is not very different, single-value multi-value, the analogy of Java Map <String, Set>
- No repeat disorder, the bottom is Hashtable
Common Operations
- sadd key values .. Set key definitions
- smembers key traversal key set
- sismember key value determines whether there is a certain value
madd s1 1 2 3 2 3 smembers s1 sismember s1 1 sismember s1 x
scard key
set number of sets of elementssrem key value
Remove a valuespop key
A random popsrandmember key n
A stack of n random- ...
Zset (ordered set) type
Brief Description
- That Sorted Set, and Set as disorderly no duplication, analogy Java's Map <String, ScoreSet>
- Each element is associated will give a double type of score Score
- zset member elements can not be repeated, and this score can be repeated
- That sort by score value of each member from small to large row
- Some scenes are used for statistics
Common Operations
- zadd/zrange
zadd z01 60 C 70 B 80 A 90 S
zrange z01 0 -1
zrange z01 0 -1 withscores
- ...
Hash type
Brief Description
- Single-value multi-value, analog in Java
Map<String,<String, Object>
- It can be defined as an object in the redis
Common Operations
- hset / hget / hmset / hmget / hgetall / hdel definition, get, delete
hset user0 id 1 nane hwc1
hget user0 id
hgetall user0
hdel user0 id
hlen key
The number of "object attribute"hexists key0 key1
The existence of a "property"hkeys/hvals key0
Traversing the "object" attribute / value- ...