A brief
Redis key types are supported: String character types, map types hash, list the type of list, set collection types, sortedset indexed collections. Next to these key types in the use of summary and introduce Keys command, although the syntax is simple, but due to the excessive number, we still need a lot of practice.
Two, String character type
1, the assignment
Syntax: SET key value
127.0.0.1:6379> set test 123
OK
2, the value
Syntax: GET key
127.0.0.1:6379> get test
"123“
3, and the value of the assignment
Syntax: GETSET key value
127.0.0.1:6379> getset s2 222
"111"
127.0.0.1:6379> get s2
"222"
4, setting / getting a plurality of keys
grammar:
- MSET key value [key value …]
- MGET key [key …]
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> mget k1 k3
1) "v1"
2) "v3"
5, delete
Syntax: DEL key
127.0.0.1:6379> del test
(integer) 1
6, increase or decrease in value
a. incrementing number
When the string is stored integer, redis provide a practical command INCR, its role is to make the current key value is incremented, and returns the value after incrementing.
Syntax: INCR key
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
b. increase the specified integer
Syntax: INCRBY key increment
127.0.0.1:6379> incrby num 2
(integer) 5
127.0.0.1:6379> incrby num 2
(integer) 7
127.0.0.1:6379> incrby num 2
(integer) 9
c. decreasing numerical
Syntax: DECR key
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> decr num
(integer) 8
d. reducing the specified integer
Syntax: DECRBY key decrement
127.0.0.1:6379> decr num
(integer) 6
127.0.0.1:6379> decr num
(integer) 5
127.0.0.1:6379> decrby num 3
(integer) 2
127.0.0.1:6379> decrby num 3
(integer) -1
7, an additional value to the tail
Append key role is to append a value. The value of the key does not exist If key is value, which is equivalent to set key value. The return value is the total length of the string after appended.
Syntax: APPEND key value
127.0.0.1:6379> set str hello
OK
127.0.0.1:6379> append str " world!"
(integer) 12
127.0.0.1:6379> get str
"hello world!"
8, to get the string length
STRLEN command returns the length of the key, if the key does not exist is returned to zero.
Syntax: STRLEN key
127.0.0.1:6379> strlen str
(integer) 0
127.0.0.1:6379> set str hello
OK
127.0.0.1:6379> strlen str
(integer) 5
Results SETNX command returns 0 and 1 are commonly used to achieve Distributed Lock []
192.168.6.172:6379> setnx newname newData1
(integer) 0
192.168.6.172:6379> del newname
(integer) 1
192.168.6.172:6379> setnx newname newData1
(integer) 1
192.168.6.172:6379> setnx lock 1
(integer) 1
192.168.6.172:6379> del lock
(integer) 1
192.168.6.172:6379> setnx lock 1
(integer) 1
0 is returned in a set of key redis already exists, set up to fail
Return 1 proxy settings in redis key does not exist, the setup is successful
Three, map hash type
In the redis, map, also known as hash. Suppose User object stored in JSON serialized form to the Redis, there User object id, username, password, age, name and other attribute, stored as follows:
Save, update: User Object -> json (string) -> redis.
Note: The default does not support the existence of the object redis
If only update the age property in the business, other properties do not update how should I do? If you are still using the top of the transmission method, a waste of resources when dealing with, say below the hash can be a good solution to this problem.
redis hash Introduction
hash called hash type, which provides a mapping of fields and field values. Field values can only be a string type,
it does not support other types of hash types, collection types . as follows:
1, the assignment
HSET insert command does not distinguish between temporary and update operations performed when an insert command returns HSET, returns 0 when an update operation.
Setting a time value field
Syntax: HSET key field value
127.0.0.1:6379> hset user username zhangesan
(integer) 1
A plurality of field values
语法:HMSET key field value [field value...]
127.0.0.1:6379> hmset user age 20 username lisi
OK
When the field is not present assignment, HSET similar, except that if the field is present, the command does not perform any operation
Syntax: HSETNX key field value
127.0.0.1:6379> hsetnx user age 30
(integer) 0
If the user does not age field is set age value of 30, or does nothing.
2, the value
One gets a field value
Syntax: HGET key field
127.0.0.1:6379> hget user username
"zhangesan"
Fetching more than one field values
Syntax: HMGET key filed [field ...]
127.0.0.1:6379> hmget user age username
1) "20"
2) "lisi"
Get all field values
Syntax: HGETALL key
127.0.0.1:6379> hgetall user
1) "age"
2) "20"
3) "username"
4) "lisi"
3, delete the field
Can delete one or more fields, the return value is the number of fields deleted.
语法:HDEL key field [field...]
127.0.0.1:6379> hdel user age
(integer) 1
127.0.0.1:6379> hdel user age name
(integer) 0
127.0.0.1:6379> hdel user age username
(integer) 1
4, increasing numbers
语法:HINCRBY key field increment
127.0.0.1:6379> hincrby user age 2 将用户的年龄加2
(integer) 22
127.0.0.1:6379> hget user age 获取用户的年龄
"22“
5, it is determined whether there is a field
语法:HEXISTS key field
127.0.0.1:6379> hexists user age 查看user中是否有age字段
(integer) 1
127.0.0.1:6379> hexists user name 查看user中是否有name字段
(integer) 0
6, get only the field name or field value
grammar:
- HKEYS key
- whales key
127.0.0.1:6379> hmset user age 20 name lisi
OK
127.0.0.1:6379> hkeys user
1) "age"
2) "name"
127.0.0.1:6379> hvals user
1) "20"
2) "lisi"
7, to obtain the number of fields
Syntax: [number of fields inside the structure of the user returned HLEN key]
127.0.0.1:6379> hlen user
(integer) 2
Four, list the type of list
The difference Arraylist and linkedlist
- ArrayList is using arrays to store data, features: Query fast, slow additions and deletions
- LinkedList is a doubly linked list using the stored data, features: fast additions and deletions, slow queries, but the data query list ends soon.
redis the linked list to store the list, so the list of data types for redis operation, both ends of the data operation to the operation list.
1, adding elements to the ends of the list
Add elements to the left of the list
Syntax: LPUSH key value [value ...]
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
Add elements to the list on the right
127.0.0.1:6379> rpush list1 4 5 6
(integer) 3
2, view the list
LRANGE command is one of a list of the most commonly used type of command to get a list of segments, the return start, all the elements between the stop (containing the element ends), the index starts from 0. Indices may be negative, such as: "- 1" represents an element of the far right.
Syntax: LRANGE key start stop
127.0.0.1:6379> lrange list1 0 2
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
4) "4"
5) "5"
6) "6"
lrange start stop
start from the beginning of the first element index is 0
Finally, a stop element from right to left starting from -1 -1
ex: lrange list 3 -2 => [4,5], inclusive
3, the pop-up list of elements from both ends
LPOP command Pops an element from the list on the left, will be completed in two steps:
The first step: the list of elements removed from the list on the left
Step Two: Return value of the element is removed.
grammar:
- LPOP key
- RPOP key
127.0.0.1:6379> lpop list1
"1"
127.0.0.1:6379> rpop list1
"6"
4, the number of elements in the list acquired
Syntax: LLEN key
127.0.0.1:6379> llen list1
(integer)4
5, delete the value specified in the list
LREM command to delete the list of the first count value is value elements, returns the number of elements that are actually deleted. Depending on the count value of the implementation of the order will be different.
- When the count> 0 Shi, LREM starts deleting from the list on the left.
- When the count <0 Shi, LREM starts deleting from the list on the right.
- When the count = 0, LREM remove all elements of the value of value.
Syntax: LREM key count value
127.0.0.1:6379> lrem list1 1 4 从左边开始删除值为4的元素
(integer)1
6, Gets / sets the value of the specified index of the element
Obtain the specified index element values
Syntax: LINDEX key index
127.0.0.1:6379> lindex list1 2
"5"
Element value of the specified index
Syntax: LSET key index value
127.0.0.1:6379> lset list1 2 4
OK
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "3"
3) "4"
7, leaving only fragments of a specified list
Consistent with the specified range and LRANGE
Syntax: LTRIM key start stop
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> ltrim list1 0 1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "4"
8, the insertion element to the list
The command will first look left to right pivot element value in the list, and in accordance with the second parameter is determined to BEFORE AFTER or inserted before or after the value of the element.
语法:LINSERT key BEFORE|AFTER pivot value
127.0.0.1:6379> lrange list1 0 -1
1) "4"
127.0.0.1:6379> linsert list1 after 4 5
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "5"
9, the elements from one list to another list
Syntax: RPOPLPUSH source destination
127.0.0.1:6379> rpoplpush list1 list2
"5"
127.0.0.1:6379> lrange list2 0 -1
1)"5"
127.0.0.1:6379> lrange list1 0 -1
1) "4"
Five, set collection types
set (collection) and the list (a list of) the difference between:
- Collection type: disorder not repeat
- List type: orderly, repeatable
1, add / remove elements
Syntax: SADD key member [member ...]
127.0.0.1:6379> sadd set1 a b c
(integer)3
127.0.0.1:6379> sadd set1 a
(integer)0
Syntax: SREM key memeber [member ...]
127.0.0.1:6379> srem set1 c d d不在集合中,故只移除c
(integer)1
2, Gets a collection of all the elements
Syntax: SMEMBERS key
127.0.0.1:6379> smemebers set1
1)"b"
1)"a"
3, it is determined whether the elements in the collection
Syntax: SISMEMBER key member
127.0.0.1:6379> sismember set1 a
(integer)1
127.0.0.1:6379> sismember set1 h
(integer)0
4, operation command
calculating a difference between the current set of A -. B
It belongs to the set of elements A and B does not belong to the configuration.
Syntax: SDIFF key [key ...]
127.0.0.1:6379> sadd setA 1 2 3
(integer)3
127.0.0.1:6379> sadd setB 2 3 4
(integer)3
127.0.0.1:6379> sdiff setA setB
1)"1"
127.0.0.1:6379> sdiff setB setA
1)"4"
b. A set intersection operator ∩ B
A collection of elements belonging to the B and belongs to the configuration.
Syntax: SINTER key [key ...]
127.0.0.1:6379> sinter setA setB
1)"2"
2)"3"
c. union set operation A ∪ B
Belonging to the A or B belonging to the set of elements constituted.
Syntax: SUNION key [key ...]
127.0.0.1:6379> sunion setA setB
1) "1"
2) "2"
3) "3"
4) "4"
5, obtaining the number of elements in the set
Syntax: SCARD key
127.0.0.1:6379> smembers setA
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> scard setA
(integer)3
6, a pop-up element from the collection
Note: Since sets are unordered, so SPOP command randomly selected from a collection of pop elements.
Syntax: SPOP key
127.0.0.1:6379> spop setA
"1"
Six, sortedset indexed collections
Sortedset called zset, is an ordered set, sortable, but unique. Sortedset and set difference is, it will set the elements to add a score, and then sort by this score.
1, adding elements
And adding an element to the score of the ordered set of elements if the element already exists will replace the old scores with the new score. The return value is newly added to the number of elements in the set, it does not contain the previously existing elements.
语法:ZADD key score member [score member...]
127.0.0.1:6379> zadd scoreboard 80 zhangesan 89 lisi 94 wangwu
(integer)3
127.0.0.1:6379> zadd scoreboard 97 lisi
(integer)0
2, the elements get scores
Syntax: ZSCORE key member
127.0.0.1:6379> zscore scoreboard lisi
"97"
3, remove elements
Remove the key ordered set of one or more members, there is no member will be ignored.
When ordered set but not the type of key exists, it returns an error.
Syntax: ZREM key member [member ...]
127.0.0.1:6379> zrem scoreboard lisi
(integer)1
4, get ranked in a range of list elements
a. Element scores according to the ascending order of the index from start to return all of the elements between the stop (containing the element ends)
语法:ZRANGE key start stop [WITHSCORES]
127.0.0.1:6379> zrange scoreboard 0 2
1)"zhangesan"
2)"wangwu"
3)"lisi"
b. Element scores returned in descending order of the indexes from the start to all of the elements between the stop (containing the element ends)
语法:ZREVRANGE key start stop [WITHSCORES]
127.0.0.1:6379> zrevrange scoredboard 0 2
1)"lisi"
2)"wangwu"
3)"zhangesan"
If the score is needed to get the elements, you can add WITHSCORES parameter in the command tail
127.0.0.1:6379> zrange scoreboard 0 1 WITHSCORES
1) "zhangsan"
2) "80"
3) "wangwu"
4) "94"
5, get ranking elements
a. From small to large
Syntax: ZRANK key member
127.0.0.1:6379> zrank scoreboard lisi
(integer)0
b. descending
Syntax: ZREVRANK key member
127.0.0.1:6379> zrevrank scoreboard zhangsan
(integer)1
6, access to elements of the specified range of scores
语法:ZRANGEBYSCORE key min max [WITHSCORES][LIMIT offset count]
127.0.0.1:6379> zrangebyscore scoreboard 90 97 WITHSCORES
1) "wangwu"
2) "94"
3) "lisi"
4) "97"
127.0.0.1:6379> zrangebyscore scoreboard 70 100 limit 1 2
1) "wangwu"
2) "lisi"
7, increase the score of an element
The return value is a fraction of the changed
Syntax: ZINCRBY key increment member
127.0.0.1:6379> zincrby scoreboard 4 lisi
"101"
8, the number of elements in the set acquired
Syntax: ZCARD key
127.0.0.1:6379> zcard scoreboard
(integer)3
9, the number of elements is obtained within the specified range of scores
Syntax: ZCOUNT key min max
127.0.0.1:6379> zcount scoreboard 80 90
(integer) 1
10, removing elements in accordance with the position range
语法:ZREMRANGEBYRANK key start stop
127.0.0.1:6379> zremrangebyrank scoreboard 0 1
(integer)2
127.0.0.1:6379> zrange scoreboard 0 -1
1)"lisi"
11, removing elements in accordance with the range of scores
Syntax: ZREMRANGEBYSCORE key min max
127.0.0.1:6379> zadd scoreboard 84 zhangsan
(integer)1
127.0.0.1:6379> zremrangebyscore scoreboard 80 100
(integer)1
Seven, Keys command
1、keys
Return all key satisfies a given pattern of
127.0.0.1:6379> keys mylist*
1) "mylist"
2) "mylist5"
3) "mylist6"
4) "mylist7"
5) "mylist8"
2、exists
To confirm whether there is a key
example: From the results, the database HongWan this key does not exist, but the key is the existence of age
127.0.0.1:6379> exists HongWan
(integer)0
127.0.0.1:6379> exists age
(integer)1
3, of the
To delete a key
127.0.0.1:6379> del age
(integer)1
127.0.0.1:6379> exists age
(integer)0
4、rename
Rename key
Example: the age into age_new.
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> rename age age_new
OK
127.0.0.1:6379>keys *
1) "age_new"
5、type
Type of return value
Example: determination of the value of the type
127.0.0.1:6379> type addr
string
127.0.0.1:6379> type myzset2
set
127.0.0.1:6379> type mylist
list
6, set the key of survival time
redis in actual use is more used as a cache, but cache data usually need to set the time to live, that is: after the expiration of data destruction.
EXPIRE key seconds 设置key的生存时间(单位:秒)key在多少秒后会自动删除
TTL key 设置key剩余的生存时间
PERSIST key 清除生存时间
PEXPIRE key milliseconds 生存时间设置单位为:毫秒
Example:
127.0.0.1:6379> set test 1 设置test的值为1
OK
127.0.0.1:6379> get test 获取test的值
"1"
127.0.0.1:6379> EXPIRE test 5 设置test的生存时间为5秒
(integer)1
127.0.0.1:6379> TTL test 查看test的生于生成时间还有1秒删除
(integer)1
127.0.0.1:6379> TTL test
(integer) -2
127.0.0.1:6379> get test 获取test的值,已经删除
(nil)