RedisApsaradb for redis provides a wide range of data types. Compared with relational databases or simple key-value storage (such as memcached), apsaradb for redis data models are more similar to actual data models. For exampleFriend relationshipThe original author used the redis sets data structure.
The storage method is as follows:FollowTwo Lists of link storage, one for the UID list of the person you are interested in, and the other for 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 followers.
Below is a PHPCodeFollowed relationship class, including the regular follow relationship operation query 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 mysqlprivate $ ID; // The redis server configurationprivate $ redis_config = array (Array ('host' => 'Localhost', 'Port' => 6379); // stores the redis connection resource so that/we only need to connect to redis onceprivate $ 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)
Source: blog.meltingice.net