Brother Lian Learning Python class notes----Redis type

Source: Internet
Author: User

Redis is commonly referred to as a data structure server because values (value) can be types such as strings (string), hashes (hash), lists (list), collections (sets), and ordered collections (sorted sets).

String (substring type)

Set command: Sets a key and value, the key exists only overwrite, return OK
> Set key values for example: >set name Zhangsan
Get command: Gets the value of a key that returns a value
> Get keys for example: >get name
SETNX command: Sets a nonexistent key and value (prevents overwriting),
> Setnx Key value returns 0 if the key already exists indicates a failure
Setex command: Sets a key and value (in seconds) for a specified validity period
> Setex key [Effective time] value for example: >setex color red
Non-write valid time indicates permanent validity, equivalent to set
SETRANGE command: Replace substring (replacement length determined by substring length)
> SetRange Key Position Sub-string
> SetRange name 4 AA replaces the 4th position of the name key corresponding value
Mset command: Bulk set key and value, return OK if successful
> Mset key 1 value 1 key 2 value 2 Key 3 value 3 ....

MSETNX command: Bulk set non-existent keys and values, Success returns OK

> Msetnx key 1 value 1 key 2 value 2 Key 3 value 3 ....
Getset command: Get the original value and set the new value
GetRange command: Gets the value of the specified range
>getrange key 0 4//Gets the value at the specified 0 to 4 position
Mget command: Bulk fetch values
>mget key 1 Key 2 Key 3 ....
INCR command: Specifies the value of the key to do the Gaga operation, returning the result after the addition.
>INCR keys for example: >INCR kid
Incrby command: Set a key plus the specified value
> Incrby key m//where m can be a positive integer or a negative integer
DECR command: Specifies the value of the key to do the subtraction operation, returning the result after the subtraction.
> DECR keys for example: >DECR kid
Decrby command: Set a key minus the specified value
> Decrby key m//where m can be a positive integer or a negative integer
Append command: Append value to the string of the specified key, returning the length of the new string value
>append Append string
Strlen the length >strlen key name//returns the corresponding value.

Hash type
Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.
Hset command: Set the key and value of a hash table
>hset Hash Name Key value
Example: >hset user:001 name Zhangsan
Hget command: Gets the corresponding value of the key in the execution hash name
HSETNX command: Set keys and values that do not exist in a hash table
>HSETNX Hash Name key value//successfully returned 1, failed to return 0
Example: >hsetnx user:001 name Zhangsan
Hmset command: Hmset user:001 username Zhangsan age sex 1 Batch setup
Hmget user:001 username age sex: getting values in bulk
>hexists user:001 Name//Whether there is a return 1 if present
>hlen user:001//Gets the number of keys in a hash user001 name
>hdel user:001 name//delete hash user:001 in the name key
>hkeys user:002//Returns all keys in the hash named user:002.
>hvals user:002//Returns all values in the hash named user:002.
>hgetall user:002//Returns all keys and values in the hash named user:002.

List lists (doubly linked list structure)
The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head of the list (to the left) or to the tail (to the right)
The list can be used as a "stack" or as a "queue".
Operation:
>lpush list1 "World"//pressing a string into the head of the List1
>lpush list1 "Hello"//pressing a string in List1 head
>lrange list1 0-1//get content in List1
0: Start-1 means end.
>rpush list2 "World"//pressing a string at the rear of the List2
>rpush list2 "Hello"//pressing a string at the rear of the List2
>lrange list2 0-1//get content in List2
0: Start-1 means end.
>linsert List2 before Hello there
Add a string before or after the key corresponds to a specific position in the list
>lset List2 1 "Four" modifies the value at the specified index location
>lrem list2 2 "Hello"//delete top two Hello values
>lrem list2-2 "Hello"//delete after two Hello values
>lrem list2 0 "Hello"//delete all Hello values
>ltrim MYLIST8 1 3//Remove the value outside this range
>lpop list2//delete element from List2 's head and return delete element
>rpop list2//delete element from the tail of list2 and return delete element
>rpoplpush list1 list2//Remove an element from the tail of the List1 to the List2 head. and returns
>lindex List2 1//Returns the element at index position in List2
>llen List2//return List2 on length

Redis Collection (SET)
Redis's set is an unordered collection of type string. A collection member is unique, which means that duplicate data cannot appear in the collection.
>sadd myset "Hello"//Add an element to MySet
Successful return 1, fail (repeat) return 0
>smembers MySet//Gets all the elements in the MySet (the result is unordered)
>srem MySet "One"//Remove a single from MySet
Successfully returned 1, failed (not present) returned 0
>spop MySet//randomly returns and deletes an element in the MySet
>srandmember MySet//randomly gets an element in the MySet, but does not delete
> Smove myset1 myset2 zhangsan: Myset1 in Zhangsan moved to Myset2
> SCard Myset1 Returns the number of Myset1
> Sismember myset zhangsan: Determine if Zhang San is in MySet
>sdiff Myset1 Myset2//Returns the difference set of two sets
Take Myset1 as the standard, get the non-existent in the Myset2.
>sdiffstore dstset myset1 Myset2 ...//Returns the difference set of all collections and saves them in Dstset
>sinter myset1 Myset2 Myset3 ...//return intersection in N collections
>sinterstore dstset myset1 Myset2 ...//returns the intersection of n sets and stores them in Dstset
> Sunion myset1 Myset2 ...//returns the set of all sets
> Sunionstore dstset myset1 myset2//Returns the set of all collections and stores them in Dstset

Redis ordered set Sset (sorted set)
A Redis ordered collection is a collection of elements of type string, as well as a collection, and does not allow duplicate members.
The difference is that each element is associated with a double-type fraction. Redis is a small-to-large ordering of the members in a collection by fractions.
The members of an ordered collection are unique, but fractions (score) can be duplicated.
> Zadd zset 1 One add one to Zset, sort by 1
> Zrem zset One: delete one in Zset
> Zincrby zset 2 One: If one exists, the order is incremented by 2, and if one does not exist, then 2
> Zrank Zset One: Return one ranking in Zset (from small to large)
> Zrevrank Zset One: Return one ranking in Zset (sort from largest to small)
> Zrange zset 0-1 withscores: Sort by score (sort from score to large)
> Zrevrange zset 0-1 withscores: Sort by score (sort by score from large to small)
> Zrangebyscore zset 2 3 withscores: Returns the element in the collection score at a given interval (contains 2 and 5)
> Zcount zset 2 3: Returns the number of a given interval in the collection
> Zcard zset: Returns the number of elements in the collection
> Zscore Zset One: Returns the score of one element
> Zremrangebyrank zset 3 3: Delete elements in the collection that rank in a given interval
> Zremrangebyscore zset 1 2: Delete zset from small to large score in 1-2


Brother Lian Learning Python class notes
Python learning Exchange, resource Sharing Group: 563626388 QQ

Brother Lian Learning Python class notes----Redis type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.