Get the App
SLTechnology News&Howtos  ›  Database  › 

Sets type and operation of redis

Shulou Source: shulou.com Published: 2022-06-01 10:32:29 09月13日 Update

Sets type and operation

Set is a set, which is similar to the concept of set in our mathematics. There are operations such as adding and deleting elements to the set, intersecting and subtracting multiple sets, and so on. Key is understood as the name of the set.

Set is implemented through hash table (that is, set is not ordered), so the complexity of adding, deleting, and searching is O (1). Hash table automatically resizes as it is added or removed. It is important to note that synchronizing (acquiring write locks) when adjusting hash table will block other read and write operations, and may soon switch to skip list, which is already used in sorted set. In addition to the basic add and delete operations on set collection types, other useful operations include union, intersection, and difference of collections. Through these operations, you can easily implement the friend recommendation in sns and the tag function of blog.

Sadd

Adds the specified element to the collection set.

Such as: sadd myset "hello"

Srem

Delete the specified element in the set

Such as: srem myset "hello"

Spop

Randomly delete elements in set and return this data

Sdiff

Returns elements that set1 does not have in set2.

Such as: sdiff set1 set2

Sdiffstore

Returns elements that set1 does not have in set2 and stores them in another set.

Such as: sdiff set1 set2 set3

Smembers

View Set elements

Such as: smembers set1

Sinter

Returns the intersection of the specified set (multiple)

Such as: sinter set1 set2 set3

Sinterstore

Returns the intersection of the specified set and saves it as another set.

Sunion

Returns the union of all given set

Such as: sunion set1 set2 set3

Sunionstore

Returns the union of all given set and stores it as another set

Smove

Remove the member from the first set and add it to the second corresponding set

Such as: smove myset2 myset7 member

Scard

Returns the number of elements of set

Sismember

Test whether member is an element of set

Srandmember

Randomly returns an element of set, but does not delete the element

Tags: Element intersection multiple type size storage adjustment complexity similarity number function name complexity friend difference math data time usefulness concept Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS vpn Microsoft NVidia