Redis implementations focus on relationships

Source: Internet
Author: User

Recently, the use of relational database to achieve the user's attention, so think of a different way of thinking, using Redis to Achieve the user's attention relationship.
After taking a look at some of the data structures of Redis, I think we can implement them with a set.

Suppose the ID of "I" is 1, and the ID of "others" is 2.

First, add attention

Add attention to two steps: 1. Add the ID to your watchlist and 2. Add your own ID to the other's fan list:

SADD 1:follow 2SADD 2:fans 1
Ii. cancellation of concerns

The cancellation of attention is also divided into two steps: 1. Remove the ID from your watchlist and 2. Remove your ID from the other's fan list:

SREM 1:follow 2SREM 2:fans 1
Iii. List of concerns

To view my watchlist:

SMEMBERS 1:follow

Check someone else's ID to replace it.

Iv. List of Fans

View a list of my fans:

SMEMBERS 2:fans

Check someone else's ID to replace it.

V. Character Relations 5.1 I pay attention to him.

I paid attention to him one way, to同时满足Two conditions: 1, my watchlistHe (or his fan listMe) 2. In my fan list没有He (or his list of concerns没有Me).

SISMEMBER 1:follow 2  #trueSISMEMBER 1:fans 2    #false
5.2 He pays attention to me one way

He was concerned about me one way, to同时满足Two conditions: 1, my watchlist没有He (or his fan list没有Me) 2. In my fan listHe (or his list of concernsMe).

SISMEMBER 1:follow 2  #falseSISMEMBER 1:fans 2    #true
5.3 If I have a meal with someone

I and someone are powder , to simultaneously satisfies two conditions: 1, my watchlist has him (or his fan list has me), 2, my fan list has him (or his attention list has me). At the same time set up for mutual powder.

sismember  1:follow  2  #true  sismember  1:fans  2  #true   

The relationship between the powders is mutual and can be reversed.

Six, my mutual powder

The people who inquire and I have the powder, are actually interested in me and my fans are asking for the intersection

SINTER 1:follow 1:fans
Vii. common concern

Query 1 and 2 of the common concern, is actually 1 of the attention and 2 of the attention to seek intersection

SINTER 1:follow 2:follow
Viii. number of related 8.1 My number of concerns
SCARD 1:follow
8.2 Number of my fans
SCARD 1:fans
Ix. problems

The problem is that my watchlist & my fan list can't be sorted by attention time, and the results are listed in positive order by ID.
The solution to consider is to add an ordered set of concerns while keeping an eye on the time stamp that is score.

ZADD 1:follow 1457871625 2ZADD 2:fans 1457871625 1

So my list of concerns is:

ZREVRANGE 1:follow-1

At the same time, the index of Zrevrange query can be used as a paging cursor, which basically solves the current problem.

Redis implementations focus on relationships

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.