Use Redis (sort set sorting set type operations)

Source: Internet
Author: User

Sort set Sorted collection type

Interpretation:

Sort set is a collection of string types

Each element of the sort set will be associated with a right

The elements in the collection can be obtained in an orderly manner by weight values

Application:

Get popular posts (reply volume) Information:

SELECT * from Message ORDER BY backnum DESC LIMIT 5;

Using the sort set to achieve the most popular Top 5 post information

Post ID reply amount (million)

11 102
12 141
13 12
14 651
15 131
16 256
17 341
18 79
19 461
20 101

The amount of each element in the sorted collection is a combination of value weights

Sort set Sorted collection

Value | Right 11|121 22|484 33|1089

Sort set Type operation

Attention:

Right score

Value Member

Zadd Key Score Member

Adds an element to the collection element that exists in the collection to update the corresponding score

Zrem Key Member

Deleting the specified element 1 indicates success returns 0 if the element does not exist

Zincrby Key INCR Member

Increase the score value of the corresponding member according to the INCR amplitude and return the score value

Zrank Key Member

Returns the rank (subscript) of the specified element in the collection, in which the elements are sorted by score from small to large

Zrevrank Key Member

Returns the rank (subscript) of the specified element in the collection, in which the elements are sorted by score from large to small

Zrange Key Start end

Removes an element of a specified interval from the collection returns an ordered result by score

Zrevrange Key Start end

Removes an element of a specified interval from the collection returns the score in reverse order.

Zcard Key

Returns the number of elements in the collection

Zscore key Element

Returns the score corresponding to the given element

Zremrangebyrank Key min Max

Delete the elements in the collection that rank in a given interval (weights are small to large)

eg

The sort set sorting set contains only 5 element information,

The 5 elements are the highest amount of replies per post being replied to,

There's a chance to get inside that set.

But only the top 5 posts with the highest number of replies will be present in the collection

When the response volume is low, it is deleted.

Create a sort set sorted collection

Hostmessage is key, with 5 elements inside

Zadd Hostmasssge 102//(integer) 1

Zadd Hostmasssge 122//(integer) 1

Zadd Hostmasssge 142//(integer) 1

Zadd Hostmasssge 162//(integer) 1

Zadd HOSTMASSSGE 182//(integer) 1

Delete the element that has the lowest value (reply value) for each new element added to the set.

Deletes an old element for each new element added

/*************************************/

Sixth element generation

Zadd hostmasssge//(integer) 1

By right-score order of reverse

Zrevrange hostmassage 0 100

The results are as follows (display data: value)

1) "15"

2) "14"

3) "13"

4) "12"

5) "16"

6) "11"

Delete the elements in the collection that rank in a given interval (weights are small to large)

Delete Right less data remaining 5 article

Zremrangebyrank hostmassage 0 0

Zrevrange hostmassage 0 100

The results are as follows (display data: value)

1) "15"

2) "14"

3) "13"

4) "12"

5) "16"

/*********************************/

Returns the rank (subscript) of the specified element in the collection,

Zrank hostmassage//(integer) 4

Zrank hostmassage//(integer) 0

Returns the number of elements in the collection

Zcard Hostmessage

(integer) 5

Right to return by value (reply amount)

Zscore Hostmessage 15

Returns "182"

Adds some weight information to the specified element value

Zincarby Hostmessage 200 14

Returns "362"

Zrevrange hostmessage 0 100

The results are as follows (display data: value)

1) "14"

2) "15"

3) "13"

4) "12"

5) "16"

Use Redis (sort set sorting set type operations)

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.