public
Last active

Social graph functions implemented using Redis.

  • Download Gist
usernode.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
<?
 
/*
* This example would 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:
*
*/
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 )
);
// 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 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 by.
*/
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);
}
}

hi, line55 should be a typo:

if ($this->is_following($user)) {

Good catch, thanks!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.