Foreword
Redis offers five in the structure:
String (string), list (list), the set (set), hash (hash), ordered collection (zset).
A string
String can store three types of values: byte string, integer, floating point
command | behavior |
---|---|
GET | Gets the value according to the key |
SET | Setting a value corresponding to a key |
OF THE | Delete a key value |
Example:
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379>
nil means is empty, that is not the existence of this key.
Although the string is redis most simple structure, but also provides related redis numerical increment and decrement operation, and the processing command and bits substring.
Check value increment and decrement operation command.
command | Described by Examples and |
---|---|
INCR | INCR key ------- represents the value +1 |
DECR | DECR key ------- representative of the value of -1 |
INCRBY | INCRBY key amount -------- value plus the amount the |
DECRBY | DECRBY key amount -------- value minus the amount |
INCRBYFLOAT | INCRBYFLOAT key amount -------- value plus float amount |
Look operation command substring
command | Described by Examples and |
---|---|
APPEND | APPEND key value ----------------- value after the value is added to the value of a key |
GETRANGE | Obtaining an offset from start to end offset range character substring composition, including start and end |
SetRange | SETRANGE key offset value ------------ starting from the start offset to the specified value substring |
Second, the list
Popular speaking, is a list of a plurality of values corresponding to the key structure.
command | behavior |
---|---|
RPUSH | A value into the right end of the list, returns the length of the list after the entry into force command |
LPUSH | A value into the left end of the list, returns a list of commands after the entry into force of the length |
LPOP | Pop a value from the left end of the list |
RPOP | Pop a value from the right end of the list |
LINDEX | According to an index value to obtain a list of |
Examples
127.0.0.1:6379> RPUSH hello world
(integer) 1
127.0.0.1:6379> LPUSH hello world1
(integer) 2
127.0.0.1:6379> LPOP hello
"world1"
127.0.0.1:6379> RPOP hello
"world"
127.0.0.1:6379> RPUSH hello world
(integer) 1
127.0.0.1:6379> LINDEX hello 0
"world"
LPOP and RPOP provides the ability to remove the list of values, but you can only remove one, LTRIM letting Remove multiple elements become possible, of course, can also be achieved by redis characteristics of the transaction, it does not set forth here first .
LTrim | Trimming the list, leaving only the element offset from the start to the end offset range |
---|
For a list, in addition to the above, there is 阻塞式
a list of pop-up command
command | Described by Examples and |
---|---|
BLPOP | BLPOP key ... [key] timeout pop leftmost element from a first non-empty list, pop or blocked waiting timeout in seconds element |
BRPOP | Above correspondence, but it is the rightmost pop-up element |
RPOPLPUSH | RPOPLPUSH source-key dest-key from the literal meaning can be seen, this command is the source-key corresponding list of the rightmost pop elements, and push dest-key corresponding to the leftmost list |
BRPOPLPUSH | BPOPLPUSH source-key dest-key timeout is similar to the previous command, which is the corresponding list of the right most element source-key pop-up, and pushed into the left dest-key corresponding to the list, except that, if the source-key is empty, blocked within timeout seconds and wait for the pop-up elements can appear |
Third, the collection
And our common collection as the collection is by redis hash table to ensure that each string stored in itself is not the same for each.
command | behavior |
---|---|
SADD | The given element to the collection |
SMEMBERS | Returns all elements in the collection contained |
SISMEMBER | Check if an element exists in the collection |
SREM | Check whether there is an element, if there is removed |
Examples
127.0.0.1:6379> SADD hello world
(integer) 1
127.0.0.1:6379> SMEMBERS hello
1) "world"
127.0.0.1:6379> SISMEMBER hello world
(integer) 1
127.0.0.1:6379> SREM hello world
(integer) 1
Fourth, hash
In a way, the hash can be seen as a smaller version of redis, the hash can store multiple pairs. Popular, a hash key is a data structure corresponding to a plurality of key-value pairs.
command | behavior |
---|---|
HSET | Add the key to the hash and returns 1 or 0 is added to identify whether the key was already inside |
hget | In the hash, obtaining a value corresponding to the key according to a key |
HGETALL | The hash key, obtains the hash of all keys |
HDEL | If a given key exists in the hash, then delete the key |
Examples
127.0.0.1:6379> hset sanlie hello world
(integer) 1
127.0.0.1:6379> hset sanlie hello1 world
(integer) 1
127.0.0.1:6379> hset sanlie hello world
(integer) 0
127.0.0.1:6379> hget sanlie hello
"world"
127.0.0.1:6379> hgetall sanlie
1) "hello"
2) "world"
3) "hello1"
4) "world"
After the third line command, returns 0, represents the hash, the key already exists.
Hash hash operations are also used to add and remove key-value pairs
command | Described by Examples and |
---|---|
HMGET | Gets the value of a key corresponding to the hash |
HMSET | Set value corresponding to a key hash |
HDEL | Delete inside a hash or more key-value pairs |
HLEN | 返回散列中包含的键值对数量 |
五、有序集合
与散列一样,有序集合也是用来存储键值对的。
不同的是,有序集合中键值对的键被称为成员
,值被称为分值
,分值必须为浮点数。
命令 | 行为 |
---|---|
ZADD | 将一个带有给定分值的成员添加到有序集合中,返回添加元素的个数 |
ZRANGE | 根据元素在有序排列中的位置,从有序集合里面获取多个元素 |
ZRANGEBYSCORE | 根据一个分值段来获取在该分值段的所有元素 |
ZREM | ZREM key member-------如果给定成员存在于该有序集合,则删除该成员 |
ZCARD | ZCARD key--------返回有序集合包含的成员数量 |
ZCOUNT | ZCOUNT key min max----------返回分值介于min 和max之间的成员数量 |
ZSCORE | ZSOCRE key member --------返回成员的分值 |
ZINCRBY | ZINCRBY key increment member -----将member成员的分值加上increment |
示例
127.0.0.1:6379> ZADD ZZ 728 member1
(integer) 1
127.0.0.1:6379> ZADD ZZ 729 member2
(integer) 1
127.0.0.1:6379> ZADD ZZ 729 member2
(integer) 0
127.0.0.1:6379> ZRANGE ZZ 0 -1
1) "member1"
2) "member2"
127.0.0.1:6379> ZRANGEBYSCORE ZZ 728 728
1) "member1"
127.0.0.1:6379> ZREM ZZ member1
(integer) 1
第三行我们在键为ZZ的有序集合中添加了已经存在的键,所以返回0标识已存在,但是会覆盖原来的值。
第四行我们通过ZRANGE命令来获取下标范围的键值对,这里只返回显示了成员,如果需要返回分值,则在命令后加withscores
即可。