Preface
In Redis, set and list are string sequences, very similar, except that set is to use a hash table to maintain the uniqueness of the string, no order, not like a list, can be deleted and removed data. But set also has its own corresponding processing command to complete its own operation. Let's take a look at how the set command is used.
command to manipulate a single set
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 the key to MySet is 2,
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>sadd MySet 1 2 3 4 5
127.0.0.1:6379>scrad MySet---See how many data items exist in the MySet and return a result of 5
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
127.0.0.1:6379>scard MySet---The number of data in a data collection of MySet key is 4
commands to manipulate multiple set
Set in addition to the elements in a set of the deletion and check, but also can operate on more than one set, such as a set of elements moved to another set, the calculation of multiple sets of the set, intersection, difference set and other operations.
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
Summary
If you have any questions after reading this article, please join the top left corner of the blog to Exchange learning.
My Redis Series Blog: Double-click an address
REDIS data Structure detailed set (iii)