Redis----友人の関心関係を計算する

1680 ワード

class Friend{
			public $flag = ""; 		#           
			public $redis = "";		#redis    

			function __construct($name){
				$this->flag = $name;
			}

			function getRd(){		#   redis  
				//   redis     redis  
				$this->redis  = new Redis();
				$this->redis -> connect('192.168.9.38',6379);
				$this->redis -> select(9);
				return $this->redis;
			}

			function follow($user){	#      
				//redis  ->sadd(  ,$user);
				//$this->getRd()->sadd(  key,$user);
				$this->getRd()->sadd("user:{$this->flag}:follow", $user);
			}

			function following(){	#           
				//                 
				//redis  ->smember(  )
				return $this->getRd()->smembers("user:{$this->flag}:follow");
			}

			function isfollow($user){	#         
			return $this->getRd()->sismember("user:{$this->flag}:follow",$user);
			}

			function follow_common($user){	#          
			//                 
			return $this->getRd()->sinter("user:{$this->flag}:follow","user:{$user}:follow");	
			}
		}

			$user1 = new Friend(1);
			$user1 -> follow(3);
			$user1 -> follow(4);
			$user1 -> follow(5);
			$user1 -> follow(7);
			$user1 -> follow(9);

			$user2 = new Friend(2);
			$user2 -> follow(3);
			$user2 -> follow(4);
			$user2 -> follow(5);
			$user2 -> follow(11);
			$user2 -> follow(100);
			$user2 -> follow(130);
			
			echo "user1:following:";
			print_r($user1->following());
			print_r($user1->isfollow(3));
			var_dump($user1->isfollow(13));
			print_r($user1->follow_common(2));