Using Redis to realize micro-blog's attention relation

Source: Internet
Author: User
Tags redis
2015-07-23/jockchou

Due to the needs of the product requirements, we do in the product to achieve similar micro-blog in the concern. In the past blog, "Using MySQL to realize the focus of the relationship between the program analysis" of the MySQL implementation and advantages and disadvantages, this blog introduces the implementation of Redis ideas. pay attention to the relationship between the four kinds of relationship States pay attention to the two-way attention of fans (mutual powder) No relationship demand analysis

In Weibo, each user will have a list of concerns, a list of fans. Users can view their own attention, a list of fans, or a list of other people's concerns and fans. Also, show the state of concern for everyone in the list and the current viewer. The possibility of a state is the four relationship States mentioned above.

The problem can be divided into two situations: look at your own attention, fan list to see other people's attention, fans list to see their attention, fans list:

This is a relatively simple situation. For example, look at their own list of concerns, lists of people's relationship with their own status can not be "no relationship" and "fans." It can only be "attention" and "two-way attention". Similarly, the list of fans has only two states. see other people's attention, fans list:

This is the most complicated situation, if you look at other people's attention list, the table and the person may have all of the above four relationship status. to analyze from the graph of a set

As shown in the figure above. The circle on the left represents the user's list of concerns, the circle on the right represents the list of fans, and the bottom circle represents the list to view (the collection). Use follow, fans, find to indicate these three sets.

When you view your list, it actually means that the Find collection is a subset of one of the above collections. For example, look at your fans, show that find is a subset of fans, see their own attention, to show a subset of follow.

When you view a list of others, the intersection of three sets is generated in this diagram. To query the users in the collection may be in your fans, focus on the collection, or maybe not. That is, it may be any kind of relationship state, and the problem is that we have to calculate the state of each user's relationship with the current user. In order to solve the four relationship states, we necessarily require the three small intersection of the lower part of the graph. The collection to query and my cross powder intersection to query the set with my attention intersection to query the sets that intersect with my fans

Users who are not in these three small intersection sets are users who are not in a relational state.

Suppose we adopt the following set of names:

Focus on the collection
Follow:userid

Fan Collection
Fans:userid

Cross Powder Collection (temporary)
Fofa:userid

The collection to query (temporary)
Find:userid

The collection to query is the intersection of my concerns (temporary)
Find_inter_follow:userid

The set that you want to query intersects with my fans (temp)
Find_inter_fans:userid

The collection to query is intersected with my cross (temporary)
Find_inter_fofa:userid

The others in find are not concerned with using sorted set storage relationships

Score is used to store the attention time, and each user stores two collections. Follow:userid stores users ' concerns, Fans:userid stores users ' fans. So we can design a function to find the set of these states.

function returns:

"Findset" => $findSet,//The collection "Fofaset" to be queried
=> $fofaSet,///The set of mutual powders
"Findinterfollowset" => $ Findinterfollowset,//To query the collection with my attention to the
"Findinterfansset" => $findInterFansSet//To query the set of with my fans pay

To find out the above four sets, you can conduct the relationship between the state of judgment, the first to determine whether the mutual powder, if not the mutual powder, and then judge whether it is my concern, if not, then judge whether it is my fans. If it's not, it's nothing. So that we can find the state.

* * UserID: Current User ID * Targetuserid: ID of the person being viewed * Findtype: Which list is viewed * Findstart: Page view of the list at the beginning of * Findend: Page view of the list at the end of the position/func tion Getchunksets ($redis, $userID, $targetUserID, $findType, $findStart, $findEnd) {$fansKey = "fans:".
        $userID; $followKey = "Follow:".
        $userID; $findKey = "Find:".

        $userID; $targetKey = $findType. ":" .
        $targetUserID; $fofaKey = "Find_inter_fofa:".

        $userID; $findInterFollowKey = "Find_inter_follow:".
        $userID; $findInterFansKey = "Find_inter_fans:".

        $userID;

        Find the Set element to query $findSet = $redis->zrevrange ($targetKey, $findStart, $findEnd, TRUE);

        To query the collection with my concern $findInterFollowSet = Array ();

        To query the set of with my fans $findInterFansSet = array ();

        Clear out the temporary set $redis->del ($findKey);
        $redis->del ($fofaKey);
        $redis->del ($findInterFollowKey);

        $redis->del ($findInterFansKey); Save up foreach ($findSet as $uID => $score) {$redis->zadd ($findKey, $score, $uid); ///!= Set if ($userID) {//See others $redis->zinter ($fofaKey, Array ($findKey, $

            Fanskey, $followKey));
            * * * If not to see their own list, also requires * 1: To query the collection with my attention to pay * 2: To query the set of with my fans to pay * *
            $redis->zinter ($findInterFollowKey, Array ($findKey, $followKey));

            $redis->zinter ($findInterFansKey, Array ($findKey, $fansKey)); $findInterFollowSet = $r
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.