Redis case-set to store the relationship of interest-redis

Source: Internet
Author: User
Tags php redis redis server

<?php

/*
* This example would probably the best if you ' re using
* An MVC framework, but it can be used standalone as well.
*
* This example also assumes is using Predis, the excellent
* PHP Redis Library available here:
* Https://github.com/nrk/predis
*/

Redis provides a rich data type, compared to relational databases or simple key-value storage (such as memcached),
The Redis data model is more similar to the data model that is actually applied. For example, the following is the storage of the friend relationship, the original author uses the Redis sets (collection) data structure.
The storage method is as follows: For each user, their concern is stored in two lists, a list of the UID for this user's attention,
Another list of UID for this user's fan, both of which use sets (collection). For example, for users with a user ID of 123,
Graph:user:123:following keeps a list of its followers, and Graph:user:1:followed_by keeps a list of people who follow him.
The following is a code of interest in the PHP class, including the general focus on the operation of the relational query methods, such as to see the comments:
Class Usernode {

The user ' s ID, probably loaded from MySQL
Private $id;
The Redis server configuration
Private $redis _config = Array (
Array (' host ' = ' localhost ', ' port ' = 6379)
//    );

Private $redis;

Public function __construct ($userID) {
$this->id = $userID;
}

Public Function Redis () {
if (! $this->redis) {
$this->redis = new Predis\client ($redis _config);
$this->redis = new Redis ();
$this->redis->connect ("127.0.0.1"); Connect (IP for Redis server)
$this->redis->auth (' Guangzhou '); Enter Password verification
}

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 the 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 this we are actually following
* The user we want to unfollow and 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, the This user follows.
*/

Public function following () {
return $this->redis ()->smembers ("graph:user:{$this->id}:following");
}

/*
* Returns an array of user ID ' s, the This user is followed by.
*/

Public Function followed_by () {
return $this->redis ()->smembers ("graph:user:{$this->id}:followed_by");
}

/*
* Test to see if this user was 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 was 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, this user is following.
*/

Public Function Follow_count () {
return $this->redis ()->scard ("graph:user:{$this->id}:following");
}

/*
* Retuns the number of users follow this user.
*/

Public Function Follower_count () {
return $this->redis ()->scard ("graph:user:{$this->id}:followed_by");
}

/*
* Finds All users, 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, and all of the given users is followed by in common.
* Returns an array of user IDs
*/

Public Function common_followed_by ($users) {
$redis = $this->redis ();
$keys = Array ();
foreach ($users as $user) {
$keys [] = "graph:user:{$user}:followed_by";
}

return Call_user_func_array (Array ($redis, "sinter"), $keys);
}

}

$user 1 = new Usernode (1);
$user 2 = new Usernode (2);


$user 1->follow (2);
$user 1->follow (3);

Var_dump ($user 1->common_following (Array (2,3)));
?>

Redis case-set to store the relationship of interest-redis

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.