Redis Five data type commands introduction (4)

Source: Internet
Author: User
Tags bulk insert delete key

1. String Type command

Set value: Set ID 001 Gets the value: Get ID Delete key value: Del ID verify key exists: Exists ID

Show All Key:keys *

INCR, Incrby specify how much to increase, DECR, Decrby specify how much to reduce

127.0.0.1:6379> Set counter 1

Ok

127.0.0.1:6379> incr counter

(integer) 2

127.0.0.1:6379> incr counter

(integer) 3

127.0.0.1:6379> Incrby Counter 2

(integer) 5

127.0.0.1:6379> Incrby Counter 3

(integer) 8

127.0.0.1:6379> DECR counter

(integer) 7

127.0.0.1:6379> Decrby Counter 2

(integer) 5

Getset first gets the getset name in the setting Lisi

Mget, Mset Batch setup, msetnx with transactional assignment, if key exists, transaction regression, no assignment processing

127.0.0.1:6379> mset name Zhangsan age 11

Ok

127.0.0.1:6379> Mget name Age

1) "Zhangsan"

2) "11"

Append append string append name "Teacher"

strlen string length, substr string intercept substr name 0 6

Setex Setting the Expiration Time command unit is seconds Setex sex male

Setnx set the value, if key has a value, does not succeed, returns 0, otherwise 1,setnx name Tom

SetRange to replace a value from a specified position

127.0.0.1:6379> set myemail [email protected]

Ok

127.0.0.1:6379> Get Myemail

"[Email protected]"

127.0.0.1:6379> SetRange Myemail 4 163.com

(integer) 12

127.0.0.1:6379> Get Myemail

"[Email protected]"

GetRange string Intercept getrange name 0 6 replace substr command

2. List Type command

List is a linked list structure, the main function is push, pop, get all the values of a range and so on.

We can add delete elements from the head or tail of the list by push, pop operations, so that the list can be both a stack and a queue.

1. Data insertion command for list lists in Redis: Lpush,rpush,linsert,lrange

127.0.0.1:6379>rpush mylist 1---Result: (integer) 1

127.0.0.1:6379>rpush mylist 2---Result: (integer) 2

127.0.0.1:6379>rpush mylist 3---rpush command: Insert 3 data from the right to the MyList list, and return the value to the current list's capacity. The result is: (integer) 3

Rpush BULK Insert Rpush mylist 1 2 3

127.0.0.1:6379>lrange mylist 0-1---lrange command: View the data in the MyList list, 0 start position, 1 end position, and end position at-1, which represents the last position of the list, that is, view all. The result is:1> "1" 2> "2" 3> "3"

127.0.0.1:6379>lpush mylist 0---lpush command: Insert a data 0 data from the left to the MyList list

127.0.0.1:6379>lrange mylist 0-1---result is:1> "0" 2> "1" 3> "2" 4> "3"

127.0.0.1:6379>linsert mylist after 3 4---The Linsert command, the expression is Linsert key Before|after pivot value; This command means a list of keys for MyList To find data with a value of 3, after which a value of 4 is inserted.

127.0.0.1:6379>lrange MyList 0-1---results for:1> "0" 2> "1" 3> "2" 4> "3" 5> "4"

127.0.0.1:6379>linsert mylist before 0-1---means: look for data with a value of 0 in the list of key mylist, and insert a value of 1 before it.

127.0.0.1:6379>lrange mylist 0-1---result for:1> "-1" 2> "0" 3> "1" 4> "2" 5> "3" 6> "4"

127.0.0.1:6379>lisert mylist after 5 8---The result is:-1, because the MyList list does not have data with a value of 5, so nothing is done and the status value-1 is returned. If key does not exist, an error message is returned.

127.0.0.1:6379>lrange mylist 0-1---result for:1> "-1" 2> "0" 3> "1" 4> "2" 5> "3" 6> "4"

2. Data deletion command for list in Redis: Lpop,rpop,ltrim

127.0.0.1:6379>lpop mylist---lpop command: Remove a piece of data from the left side of the list while outputting the deleted data, where the result is 1

127.0.0.1:6379>lrange MyList 0-1---results for:1> "0" 2> "1" 3> "2" 4> "3" 5> "4"

127.0.0.1:6379>rpop mylist---rpop command: Remove a piece of data from the right side of the list and output the deleted data, where the result is 4

127.0.0.1:6379>lrange mylist 0-1---result is:1> "0" 2> "1" 3> "2" 4> "3"

127.0.0.1:6379>ltrim mylist 1 3----ltrim command: Retains the value of the set of two subscript intervals, deleting all values that are not in its range. 1 is the subscript value to start preserving, and 3 is the subscript value that ends the reservation.

127.0.0.1:6379>lrange mylist 0-1---result is:1> "1" 2> "2" 3> "3"

3. Data View command for the list in Redis: Llen,lindex

127.0.0.1:6379>llen mylist---llen command: Return the length of the list, here MyList only 4 data left, so the output is 4

127.0.0.1:6379>lindex mylist 3---lindex command: Gets the data for the given position, where the data at coordinates 3 is "2", so the result is 2.

4. The list data modification command in Redis: LSet

127.0.0.1:6379>lset mylist 2 zlh---lset command: Set the value of subscript 2 to ZLH and return an error if the subscript value goes out of range or an empty list is LSet

127.0.0.1:6379>lrange mylist 0-1---Result: 1> "1" 2> "2" 3> "ZLH"

5. List in Redis, two List A, B, add the tail element of a list to the head element of list B, command: Rpoplpush

#这里我有连个列表A数据为 {4,5,6}, b list data = {*.}

127.0.0.1:6379>rpoplpush A B

127.0.0.1:6379>lrange A---result for:1> "1 ' 2>" 2 "

127.0.0.1:6379>lrange B---result is:1> "3 ' 2>" 4 "3>" 5 "4>" 6 "

6. Several advanced commands with blocking in Redis: Blpop,brpop,brpoplpush

127.0.0.1:6379>blpop a---Means: a list has values, remove a data from the left, if there is no value, then wait for a to insert the data, wait time is 30 seconds, if the time is set to 0 means that the blocking time unlimited extension

127.0.0.1:6379>brpoplpush a b---means: Add the tail element of a list to the head element of list B, if there is a value in the A list is inserted, if there is no value, wait for a to insert data, wait time is 30 seconds, If the time is set to 0, the blocking time is extended indefinitely

3. Set type

Both set and list are string sequences, very similar, except that set is a hash table that preserves the uniqueness of strings, with no precedence.

The new command for the set list in Redis Sadd,key value value can not be duplicated, return the number of inserted data, key can be followed by multiple value values

127.0.0.1:6379>sadd MySet 1---Add the Set data value of key MySet to 1,

127.0.0.1:6379>sadd MySet 2 4 5---Add the Set data value of key MySet to 2 4 5,

commands for viewing set data collections in Redis: Smembers, you can get all of the elements, and when the set members are a bit more concerned about their performance.

127.0.0.1:6379>smembers MySet---Gets a set of set data values that key is MySet

A command in Redis that determines whether a value exists in the value of a key: Sismember, if there is a return of 1, there is no return 0

127.0.0.1:6379>sismember MySet 3---The return value is 0 because only 1 of the set members of MySet, 2 No 3

127.0.0.1:6379>sismember MySet 2---The return value is 1 because 2 exists in the set member of MySet.

The delete data command for set in Redis, Srem, returns the number of deletes, followed by multiple value values for key

127.0.0.1:6379>srem MySet 1---Delete the data item with a key of Myset,value 1.

127.0.0.1:6379>srem MySet 2 4---Delete 2 data items with a key of Myset,value of 2 and 4.

Command SCard to view the number of set data in Redis if no return 0 is present

127.0.0.1:6379>scard MySet---See how many data items exist in the MySet

Set random view Element command Srandmember in Redis

127.0.0.1:6379>srandmember myset---Output key is a random value in the data item of MySet,

Redis set randomly deletes the command spop of an element and returns the output of the deleted data

127.0.0.1:6379>spop myset----Randomly deletes a piece of data from a key MySet data collection and returns the output-deleted data

The set in Redis moves the elements in one set to a command in another set Smove

127.0.0.1:6379>sadd MySet 1 2 3----Add 3 data to MySet 1 2 3

27.0.0.1:6379>sadd Youset 3 4 5----Add 3 data to Youset 3 4 5

127.0.0.1:6379>smove Youset MySet 4---to remove data 4 from Youset and add data 4 to MySet

127.0.0.1:6379>smembers MySet---Result: 1 2 3 4

127.0.0.1:6379>smembers Youset---result is 3 5

Set intersection command sinter in Redis

127.0.0.1:6379>sinter MySet youset---Output is the intersection of MySet and Youset, the output is: 3

Command sunion for set-seek-set in Redis

127.0.0.1:6379>sunion MySet youset---Output is a set of MySet and Youset, the output is: 1 2 3 4 5

Command Sdiff for set differential set in Redis

127.0.0.1:6379>sdiff MySet youset---Output is youset non-existent data in MySet, the output is: 1 2 4

127.0.0.1:6379>sdiff Youset MySet---The output is myset non-existent data in youset, the result is: 5

4. SortedSet Type command

Sorted set is an upgraded version of Set, which adds a sequential attribute to the set, which can be specified when adding a modified element, and Zset automatically re-adjusts the order of the new values after each assignment. It can be understood as a data table with two columns of fields, a column of value, and a list of sequential numbers. Key in operation is understood as the name of Zset.

Operation of an ordered collection Zset:

Zadd: Adds an element member,score for sorting to a zset named key. If the element exists, its order is updated. (Usage: Zadd ordinal set ordinal number element value)

127.0.0.1:6379> Zadd Zset1 1
(integer) 1
127.0.0.1:6379> Zadd Zset1 2 One
(integer) 1
127.0.0.1:6379> Zadd Zset1 3 Seven
(integer) 1

127.0.0.1:6379> Zrange Zset1 0-1
1) "Both"
2) "One"
3) "Seven"
127.0.0.1:6379> zrange Zset1 0-1 withscores
1) "Both"
2) "1"
3) "One"
4) "2"
5) "Seven"
6) "2"

Zrem: Removes the element in the Zset named key. (Usage: Zrem an ordered set of element values to delete)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "One"
4) "2"
5) "Seven"
6) "2"
127.0.0.1:6379> Zrem Zset1 One
(integer) 1
127.0.0.1:6379> zrange Zset1 0-1 withscores
1) "Both"
2) "1"
3) "Seven"
4) "2"

Zincrby: If the element member is already present in Zset with the name key, the score of the element increases increment, otherwise the element is added to the collection, and its score value is increment. The order number of the element is incremented or decreased. (Usage: Zincrby ordered set increment the specified element value)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Seven"
4) "2"
127.0.0.1:6379> Zincrby Zset1 5 Seven
"7"
127.0.0.1:6379> zrange Zset1 0-1 withscores
1) "Both"
2) "1"
3) "Seven"
4) "7"

Zrank: Returns the rank of the member element with the name key (sorted by score from small to large) that is subscript. (Usage: Zrank The specified element value for an ordered collection), and the return value is the index of the specified element value.

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Seven"
4) "7"
127.0.0.1:6379> Zrank Zset1 Seven
(integer) 1

Zrevrank: Returns the rank of the member element with the name key (sorted by score from large to small) that is subscript. (Usage: Zrank The specified element value for an ordered collection)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Seven"
4) "7"
127.0.0.1:6379> Zrevrank Zset1 Seven
(integer) 0

Zrange: Displays the element values for the specified subscript in the collection (sorted by score from small to large). If you need to display the sequential numbering of the elements, take the withscores. (Usage: Zrange ordinal set subscript index 1 Subscript index 2 withscores)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"
7) "Seven"
8) "7"

Zrevrange: Displays the value of the element in the collection specified by the subscript (sorted by score from large to small). If you need to display the sequential numbering of the elements, take the withscores. (Usage: Zrevrange ordinal set subscript index 1 Subscript index 2 withscores)

127.0.0.1:6379> Zrevrange Zset1 0-1withscores
1) "Seven"
2) "7"
3) "One"
4) "3"
5) "Five"
6) "2"
7) "Both"
8) "1"

Zrangebyscore: Displays the elements of the specified range order number (sorted by score from small to large). (Usage: Zrangebyscore ordered set order Number 1 sequence number 2 withscores)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"
7) "Seven"
8) "7"
127.0.0.1:6379> Zrangebyscore Zset1 Withscores
1) "Five"
2) "2"
3) "One"
4) "3"
5) "Seven"
6) "7"

Zcount: Returns the number of score in the collection for a given interval. (Usage: Zcount ordered set order Number 1 sequential number 2)

127.0.0.1:6379> Zcount Zset1 2 7
(integer) 3

Zcard: Returns the number of elements in the collection. (Usage: Zcard ordered set)

127.0.0.1:6379> Zrange Zset1 0-1
1) "Both"
2) "Five"
3) "One"
4) "Seven"
127.0.0.1:6379> Zcard Zset1
(integer) 4

Zremrangebyrank: Deletes the element in the collection that is ranked in the given interval. (Delete by index subscript) (usage: Zremrangebyrank ordinal set index number 1 index number 2)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"
7) "Seven"
8) "7"
127.0.0.1:6379> Zremrangebyrank Zset1 3 3
(integer) 1
127.0.0.1:6379> zrange Zset1 0-1 withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"

Zremrangebyscore: Deletes the elements in the collection score the given interval (score values in order). (Usage: Zremrangebyscore ordered set order Number 1 sequential number 2)

127.0.0.1:6379> Zrange Zset1 0-1withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"
7) "Seven"
8) "7"
127.0.0.1:6379> Zremrangebyscore Zset1 57
(integer) 1
127.0.0.1:6379> zrange Zset1 0-1 withscores
1) "Both"
2) "1"
3) "Five"
4) "2"
5) "One"
6) "3"

5. Hash type

a command demonstration of a hash in Redis for a single key/value operation

Add command for hash in Redis Hset, if key does not exist, create key, exist, overwrite original value

Redis 127.0.0.1:6379>hsetmyhash name Jim----keys to MySet key value set key to name value for Jim

View command for hash in Redis Hget

Redis 127.0.0.1:6379>hgetmyhash name----output: "Jim", get key MySet, key name value

Redis 127.0.0.1:6379>hsetmyhash name ZLH----Overwrite original value, change value to ZLH replace Jim

Redis 127.0.0.1:6379>hgetmyhash Name---output: "ZLH"

The hash in Redis gets the key containing the number of field commands Hlen

Redis 127.0.0.1:6379>hsetmyhash Age---Set key to MySet key to age value=31

Redis 127.0.0.1:6379>hlenmyhash---Output of 2,key as MySet field number is 2

A command hexists in Redis that specifies whether field is present in the specified key, there is a return of 1, there is no return 0

Redis127.0.0.1:6379>hexists Myhash Name---Returns 1, indicating the existence

Redis127.0.0.1:6379>hexists Myhash name1---Return 0, indicating that there is no

Redis Hash Delete command Hdel, delete one or more of the specified fields

Redis 127.0.0.1:6379>hsetmyhash Sex nan----add data

Redis 127.0.0.1:6379>hsetmyhash issingle Yes----add data

Redis 127.0.0.1:6379>hsetmyhash Hobby Sports----Add data

Redis 127.0.0.1:6379>hdelmyhash Hobby----Delete individual data, filed to hobby data

Redis 127.0.0.1:6379>hdelmyhash issingle Sex---Delete multiple data, filed two data for Issingle and sex

Redis Hash if key or field does not exist insert valid, otherwise do not take action command hsetnx

redis127.0.0.1:6379>hsetnx myhash Sex Nan---Set the value of Myhash,field to sex as Nan, successfully returning 1 because there is no sex in this field

REDIS127.0.0.1:6379>HSETNX Myhash Sex NV---Set the value of Myhash,field to sex as NV and did not successfully return 0 because there was a field for sex and a value

Redis 127.0.0.1:6379>hgetmyhash Sex-Output "Nan"

A command that increases or decreases when value in a hash in Redis is a numeric value Hincrby

Redis 127.0.0.1:6379>delmyhash---Delete this key

Redis 127.0.0.1:6379>hsetmyhash Age---Set key to Myhash with an age value of 31

Redis127.0.0.1:6379>hincrby Myhash Age---give key to Myhash, the value of age is added 10, the output is 41

Redis127.0.0.1:6379>hincrby Myhash age-10---Give key to Myhash, the value of the key is age minus 10, the output is 31

command demonstration of the hash batch operation Key/value in Redis

Add Key/value commands in bulk Hmset

Redis 127.0.0.1:6379>delmyhash--delete this key

Redis 127.0.0.1:6379>hmsetmyhash name ZLH age issingle No----add multiple key values Name=zlh,age=31,issingle=no to the hash of the key for Myhash

Command Hmget to get key/value in bulk

Redis 127.0.0.1:6379>hmgetmyhash name Age issingle----Output: ZLH No

Commands for all fields and value are obtained according to the Myhash key Hgetall

Redis127.0.0.1:6379>hgetall myhash----Output is: Name Age Issingle ZLH

Get all the field commands Hkeys

The Redis 127.0.0.1:6379>hkeysmyhash---output is: Name Age Issingle

command to get the values of all fields hvals

The Redis 127.0.0.1:6379>hvalsmyhash----output is: ZLH No

Redis Five data type commands introduction (4)

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.