EDITORIAL
Content is derived under the dark horse in the redis Python tutorial, I practice a bit in the virtual machine, in addition they added some content. If you have questions, welcome criticism.
redis View command in the File ⽹ official documentation that http://redis.cn/commands.html
redis is key-value data structure, each data key-value pairs are ⼀
Type is a string key, the key can not be repeated
There are five types of values:
- String string
- Hash hash
- List list
- Collection set
- Ordered set zset
Take a look at what it can do these types of data? Figure source cattle off network advanced algorithms class.
string type
Redis string type is the most basic type of data storage, it is binary safe in Redis, this would mean that the type can accept data in any format, such as JPEG image data or Json object description information. Data type Value length of the string can accommodate up in Redis is 512M.
Storage
If you add a key set was not present, if the key set already exists to modify
Setting key
set key value
Example 1: set key data for the name is ben
set name ben
Set the key and the expiration time, in seconds
setex key seconds value
Example 2: Set a value of the key to aa aa expiration time of 3 seconds of data
setex aa 3 aa
A plurality of keys
mset key1 value1 key2 value2 ...
Example 3: Key Set to 'a1' value 'python', the key is 'a2' value 'java', the key is 'a3' value 'c'
mset a1 python a2 java a3 c
Added value
append key value
Example 4: To bond is added in the hello a1
append a1 hello
Obtain
Get: get the value according to the key if this key does not exist return nil
get key
Example 5: Get value of the key 'name' of
get 'name'
Obtaining a plurality of values according to the plurality of keys
mget key1 key2 ...
Example 6: acquiring the key a1, the value of a2, a3 'of
mget a1 a2 a3
delete
⻅ next section details the operation key, the delete key will delete the value
Increment decrement
incr key
decr key
Key command
Find key parameters ⽀ hold regular expression
keys pattern
Example 1: View all keys
keys *
Example 2: View of the key comprising a name
keys a*
Determining whether there is a key, if there is a return, there is no return 0
exists key1
Example 3: determining whether there is a key python
exists python
View the key corresponding to the value of the type
type key
Four cases: View java type key value of five types of support redis ⽀ species in ⼀
type java
Delete keys and corresponding values
the key1 key2 ...
Example 5: Delete key a2, a3
del a2 a3
Set the expiration time, in seconds
If you do not specify an expiration time is ⼀ straight exist until Use DEL removed
expire key seconds
Example 6: java expiration time set key 11 seconds
expire java 11
View the effective time, in seconds
ttl key
Example 7: Check the java key valid time
ttl java
hash type
- Using the hash stored in the object, the structure of the object attribute, the value of
- Value is of type string
Add, modify
Providing a single property
hset key field value
Example 1: The user setting key attribute name is alex
hset user name alex
Set multiple properties
hmset key field1 value1 field2 value2 ...
Example 2: Set key attribute name user2 is java, attribute age 10
hmset user2 name java age 10
Obtain
Get all the attributes specified key
hkeys key
Example 3: Get all the key attributes of user2
hkeys user2
Gets ⼀ property values
hget key field
Example 4: Get the name attribute value of the key data
hget data name
Values of a plurality of attributes
hmget key field1 field2 ...
Example 5: acquiring key data attribute 'name', 'age values of
hmget data name age
Get all the value of the property
whales key
Example 6: an overview of all values of key data attribute
whales data
hgetAll key: get attributes and values
delete
Delete the entire hash keys and values Using the del command
Delete properties corresponding attribute value will be deleted from the ⼀
hdel key field1 field2 ...
Example 7: Properties of the Delete key data age
hdel u2 age
Type list
- Element type list of string
- Inserting sorted order
increase
Insert the data on the left
lpush key value1 value2 ...
Example 1: From the list on the left bond is a question of filling with the data a, b, c
lpush question a b c
Insert data in the right
rpush key value1 value2 ...
Example 2: From the list on the right bond is a question of filling with data 123
rpush question 1 2 3
Before or after the specified element Insert a new element
linsert key before or after the existing elements of the new element
Example 3: In the key elements in the list quesiton 2 before filling with d
linsert question before 2 d
Obtain
Returns the list of elements within the specified range ⾥
- start, stop elements for the index index
- Index from the left side, to the first frame element 0
- Indices may be negative, indicating the start count from the end, such as the last frame -1 represents elements
lrange key start stop
Example 4: Get the key is 'a1' a list of all the elements
lrange a1 0 -1
Setting element values specified index
Index from the left side, to the first frame element 0
Indices may be negative, showing the tail counted as -1 for last frame elements
lset key index value
Example 5: Modification key question in the list is the subscript for the element 2 java
lset question 2 java
delete
Delete the specified element
- The value of the element count value of the secondary list that appears before removing the
- count> 0: to remove the tail from the head
- count <0: is removed from the head to the tail
- count = 0: remove all
lrem key count value
Example 6.1: abababcab list1 added to the list of elements START
lpush list1 a b a b a b c a b
Example 6.2: Start Remove four 'b' from the right list list1
lrem list1 4 b
set type
- ⽆ ordered set
- Elements of type string
- ⼀ element has a unique, non-repeat
- Definitions: For the set operation is not modified
increase
Adding elements
sadd key member1 member2 ...
Example 1: add to the collection of key elements s1 'zhangsan', 'lisi', 'wangwu'
sadd s1 zhangsan sili wangwu
Obtain
Return all the elements
smembers key
Example 2: obtaining a collection of all the elements key s1
smembers s1
delete
Delete the specified element
srem key
Example 3: Delete key elements in the set of s1 'wangwu'
srem S1 wangwu
zset type
- sorted set, ordered set
- Elements of type string
- ⼀ element has a unique, non-repeat
- Are associated with each element of type double ⼀ Score, it indicates a weight by weight of the elements from below approximately sorting ⼩
- Description: no modification operations
increase
Add to
zadd key score1 member1 score2 member2 ...
Example 1: ben add elements to the collection of keys zset1, alex, eric, liu, a weight of 4,3,5,1, respectively,
zadd zset1 4 ben 3 alex 5 eric 1 liu
Obtain
Returns the specified range of elements
start, stop elements for the index index
Index from the left side, to the first frame element 0
Indices may be negative, indicating the start count from the end, such as the last frame -1 represents elements
zrange key start stop
Example 2: Get Key 'zset1' the set of all the elements
zrange zset1 0 -1
Return members score value between the min and max
zrangebyscore key min max
Example 3: acquiring a set of key members of set1 authority value between 1 and 4
zrangebyscore a4 1 4
Returns the value of the member member of score
zscore key member
Example 4: acquiring a set of weights zset1 key element ben weight
zscore in zset1
delete
Delete the specified element
zrem key member1 member2 ...
Example 5: Delete the collection of elements zset1 admin
zrem zset1 admin
Delete weights of the elements in the specified range
zremrangebyscore key min max
Example 6: Remove set 'zset1' permission elements between 3,5
zremrangebyscore zset1 3 5