Use Redis to store the follow relationship
Redis provides a wide range of data types. Compared with relational databases or simple Key-Value storage (such as Memcached), Redis data models are more similar to actual data models. For example, in the storage of friend relationships mentioned below, the original author uses the Redis Sets data structure.
The storage method is as follows: for each user, the attention relation is stored in two lists, one is the UID list of the person who follows this user, and the other is the UID list of the user fans, both lists use Sets ). For example, for a user whose user ID is 123, graph: user: 123: following stores the list of followers, graph: user: 1: followed_by saves the list of people who follow him.
The following is a PHP code interest relation class, including regular interest relation operation queries and other methods. For details, see the notes:
<?
/*
* This example wowould probably work best if you're using
* An MVC framework, but it can be used standalone as well.
*
* This example also assumes you are using Predis, the excellent
* PHP Redis library available here:
* Https://github.com/nrk/predis
*/
Class UserNode {
// The user's ID, probably loaded from MySQL
Private $ id;
// The redis server configuration
Private $ redis_config = array (
Array ('host' = & gt; 'localhost', 'Port' = & gt; 6379)
);
// Stores the redis connection resource so that
// We only need to connect to Redis once
Private $ redis;
Public function _ construct ($ userID ){
$ This-> id = $ userID;
}
Private function redis (){
If (! $ This-> redis ){
$ This-> redis = new Predis \ Client ($ redis_config );
}
Return $ this-> redis;
}
/*
* Makes this user follow the user with the given ID.
* In order to stay efficient, we need to make a two-way
* Directed graph. This means when we follow a user, we also
* Say that user is followed by this user, making a forward
* And backword directed graph.
*/
Public function follow ($ user ){
$ This-> redis ()-> sadd ("graph: user :{$ this-> id}: following", $ user );
$ This-> redis ()-> sadd ("graph: user: $ user: followed_by", $ this-> id );
}
/*
* Makes this user unfollow the user with the given ID.
* First we check to make sure that we are actually following
* The user we want to unfollow, then we remove both the forward
* And backward references.
*/
Public function unfollow ($ user ){
If ($ this-> is_following ()){
$ This-> redis ()-> srem ("graph: user: {$ this-> id}: following", $ user );
$ This-> redis ()-> srem ("graph: user: $ user: followed_by", $ this-> id );
}
}
/*
* Returns an array of user ID's that this user follows.
*/
Public function following (){
Return $ this-> redis ()-> smembers ("graph: user :{$ this-> id}: following ");
}
/*
* Returns an array of user ID's that this user is followed.
*/
Public function followed_by (){
Return $ this-> redis ()-> smembers ("graph: user :{$ this-> id}: followed_by ");
}
/*
* Test to see if this user is following the given user or not.
* Returns a boolean.
*/
Public function is_following ($ user ){
Return $ this-> redis ()-> sismember ("graph: user :{$ this-> id}: following", $ user );
}
/*
* Test to see if this user is followed by the given user.
* Returns a boolean.
*/
Public function is_followed_by ($ user ){
Return $ this-> redis ()-> sismember ("graph: user :{$ this-> id}: followed_by", $ user );
}
/*
* Tests to see if the relationship between this user and the given user is mutual.
*/
Public function is_mutual ($ user ){
Return ($ this-> is_following ($ user) & $ this-> is_followed_by ($ user ));
}
/*
* Returns the number of users that this user is following.
*/
Public function follow_count (){
Return $ this-> redis ()-> scard ("graph: user :{$ this-> id}: following ");
}
/*
* Retuns the number of users that follow this user.
*/
Public function follower_count (){
Return $ this-> redis ()-> scard ("graph: user :{$ this-> id}: followed_by ");
}
/*
* Finds all users that the given users follow in common.
* Returns an array of user IDs
*/
Public function common_following ($ users ){
$ Redis = $ this-> redis ();
$ Users [] = $ this-> id;
$ Keys = array ();
Foreach ($ users as $ user ){
$ Keys [] = "graph: user: {$ user}: following ";
}
Return call_user_func_array (array ($ redis, "sinter"), $ keys );
}
/*
* Finds all users that all of the given users are followed by in common.
* Returns an array of user IDs
*/
Public function common_followed_by ($ users ){
$ Redis = $ this-> redis ();
$ Users [] = $ this-> id;
$ Keys = array ();
Foreach ($ users as $ user ){
$ Keys [] = "graph: user: {$ user}: followed_by ";
}
Return call_user_func_array (array ($ redis, "sinter"), $ keys );
}
}
The following is an example of using this class to operate the relationship of interest:
<?
// Create two user nodes, assume for this example
// They're users with no social graph entries.
$ User1 = UserNode (1 );
$ User2 = UserNode (2 );
$ User1-> follows (); // array ()
// Add some followers
$ User1-> follow (2 );
$ User1-> follow (3 );
// Now check the follow list
$ User1-> follows (); // array (2, 3)
// Now we can also do:
$ User2-> followed_by (); // array (1)
// If we do this...
$ User2-> follow (3 );
// Then we can do this to see which people users #1 and #2 follow in common
$ User1-> common_following (2); // array (3)
You may also like the following articles about Redis. For details, refer:
Install and test Redis in Ubuntu 14.04
Basic configuration of Redis master-slave Replication
Redis cluster details
Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis
Redis series-installation, deployment, and maintenance
Install Redis in CentOS 6.3
Learning notes on Redis installation and deployment
Redis. conf
Redis details: click here
Redis: click here
This article permanently updates the link address: