UserFollwRedisService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class UserFollwRedisService extends BaseRedisService
  10. {
  11. var $user_follow_db; //set有序数据=user_id 用户关注的会员ID
  12. var $user_followed_by_db; // set有序数据=user_id 关注用户的会员ID
  13. // var $user_hash_db; //所有会员数据 user_id hash数据 存储在线数据
  14. //var $user_db; //:user_id hash数据,会员数据
  15. private $id;
  16. /**
  17. +----------------------------------------------------------
  18. * 架构函数
  19. +----------------------------------------------------------
  20. * @access public
  21. +----------------------------------------------------------
  22. */
  23. public function __construct($userID)
  24. {
  25. parent::__construct();
  26. $this->user_follow_db = $this->prefix.'user_following:';
  27. $this->user_followed_by_db = $this->prefix.'user_followed_by:';
  28. // $this->user_hash_db = $this->prefix.'user_hash_db';
  29. //$this->user_db = $this->prefix.'user:';
  30. $this->id = $userID;
  31. }
  32. /*
  33. * Makes this user follow the user with the given ID.
  34. * In order to stay efficient, we need to make a two-way
  35. * directed graph. This means when we follow a user, we also
  36. * say that that user is followed by this user, making a forward
  37. * and backword directed graph.
  38. */
  39. public function follow($user,$room_id=0) {
  40. $this->redis->sAdd($this->user_follow_db.$this->id, $user);
  41. $this->redis->sAdd($this->user_followed_by_db.$user, $this->id);
  42. if($room_id){
  43. //$this->incry($this->video_db.$room_id, 'fans_count', 1);
  44. $this->redis->hIncrBy($this->video_db.$room_id,'fans_count',1);
  45. }
  46. }
  47. /*
  48. * Makes this user unfollow the user with the given ID.
  49. * First we check to make sure that we are actually following
  50. * the user we want to unfollow, then we remove both the forward
  51. * and backward references.
  52. */
  53. public function unfollow($user,$room_id=0) {
  54. if ($this->is_following($user)) {
  55. $this->redis->srem($this->user_follow_db.$this->id, $user);
  56. $this->redis->srem($this->user_followed_by_db.$user, $this->id);
  57. if($room_id){
  58. //$this->incry($this->video_db.$room_id, 'fans_count', -1);
  59. $this->redis->hIncrBy($this->video_db.$room_id,'fans_count',-1);
  60. }
  61. }
  62. }
  63. /*
  64. * Returns an array of user ID's that this user follows.
  65. */
  66. public function following() {
  67. return $this->redis->sMembers($this->user_follow_db.$this->id);
  68. }
  69. public function get_follonging_user($user_id,$page=1,$page_size=20){
  70. $start = ($page-1)*$page_size;
  71. $list = $this->redis->sMembers($this->user_follow_db.$user_id);
  72. $keys = array_slice($list,$start,$page_size);
  73. $user_list = $this->redis->hMGet($this->user_hash_db,$keys);
  74. $user_array = array();
  75. if($user_id!=$this->id){
  76. $common_following = $this->common_following(array($user_id));
  77. }
  78. foreach($user_list as $k=>$v){
  79. $v= json_decode($v,true);
  80. $v['head_image'] = get_spec_image($v['head_image']);
  81. $v['user_id'] = $k;
  82. $v['nick_name'] = $v['nick_name']?$v['nick_name']:'';
  83. $v['sex'] = $v['sex']?$v['sex']:'0';
  84. $v['v_icon'] = $v['v_icon']?$v['v_icon']:'';
  85. $v['v_type'] = $v['v_type']?$v['v_type']:'';
  86. $v['user_level'] = $v['user_level']?$v['user_level']:'1';
  87. $v['fans_count'] = $v['fans_count']?$v['fans_count']:'0';
  88. if($user_id==$this->id){
  89. $v['follow_id'] = $k;
  90. }elseif(in_array($k,$common_following)){
  91. $v['follow_id'] = $k;
  92. }else{
  93. $v['follow_id'] = 0;
  94. }
  95. $user_array[] = $v;
  96. }
  97. return $user_array;
  98. }
  99. public function get_follonging_by_user($user_id,$page=1,$page_size=20){
  100. //return array();
  101. $start = ($page-1)*$page_size;
  102. $list = $this->redis->sMembers($this->user_followed_by_db.$user_id);
  103. $keys = array_slice($list,$start,$page_size);
  104. // if($user_id!=$this->id){
  105. $common_following = $this->common_followed_by(array($user_id));
  106. //}
  107. $user_array = array();
  108. if($keys){
  109. $user_list = $this->redis->hMGet($this->user_hash_db,$keys);
  110. foreach($user_list as $k=>$v){
  111. $v= json_decode($v,true);
  112. $v['head_image'] = get_spec_image($v['head_image']);
  113. $v['user_id'] = $k;
  114. $v['nick_name'] = $v['nick_name']?$v['nick_name']:'';
  115. $v['sex'] = $v['sex']?$v['sex']:'0';
  116. $v['v_icon'] = $v['v_icon']?$v['v_icon']:'';
  117. $v['v_type'] = $v['v_type']?$v['v_type']:'';
  118. $v['user_level'] = $v['user_level']?$v['user_level']:'1';
  119. $v['fans_count'] = $v['fans_count']?$v['fans_count']:'0';
  120. $user_array[] = $v;
  121. }
  122. }
  123. return $user_array;
  124. }
  125. public function get_private_user($page=1,$page_size=20){
  126. $start = ($page-1)*$page_size;
  127. $keys = $this->common_private_follower();
  128. $countlist=$this->redis->hMGet($this->user_hash_db,$keys);
  129. $rs_count=count($countlist);
  130. $root['countlist']=$rs_count;
  131. $keys = array_slice($keys,$start,$page_size);
  132. $user_array = array();
  133. if($keys){
  134. $user_list = $this->redis->hMGet($this->user_hash_db,$keys);
  135. foreach($user_list as $k=>$v){
  136. if($v){
  137. $v= json_decode($v,true);
  138. $v['head_image'] = get_spec_image($v['head_image']);
  139. $v['user_id'] = $k;
  140. $v['nick_name'] = $v['nick_name']?$v['nick_name']:'';
  141. $v['nick_name'] = htmlspecialchars_decode($v['nick_name']);
  142. $v['signature'] = $v['signature']?$v['signature']:'';
  143. $v['signature'] = htmlspecialchars_decode($v['signature']);
  144. $v['sex'] = $v['sex']?$v['sex']:'0';
  145. $v['v_icon'] = $v['v_icon']?$v['v_icon']:'';
  146. $v['v_type'] = $v['v_type']?$v['v_type']:'';
  147. $v['user_level'] = $v['user_level']?$v['user_level']:'1';
  148. $user_array[] = $v;
  149. }
  150. }
  151. }
  152. $list = $user_array;
  153. $root['list'] = $list;
  154. if($page==0){
  155. $root['has_next'] = 0;
  156. }else{
  157. if (count($list) == $page_size)
  158. $root['has_next'] = 1;
  159. else
  160. $root['has_next'] = 0;
  161. }
  162. $root['page'] = $page;//
  163. $root['status'] = 1;
  164. return $root;
  165. }
  166. /*
  167. * Returns an array of user ID's that this user is followed by.
  168. */
  169. public function followed_by($user_id=0) {
  170. if ($user_id == 0) $user_id = $this->id;
  171. return $this->redis->sMembers($this->user_followed_by_db.$user_id);
  172. }
  173. /*
  174. * Test to see if this user is following the given user or not.
  175. * Returns a boolean.
  176. */
  177. public function is_following($user) {
  178. return $this->redis->sismember($this->user_follow_db.$this->id, $user);
  179. }
  180. /*
  181. * Test to see if this user is followed by the given user.
  182. * Returns a boolean.
  183. */
  184. public function is_followed_by($user) {
  185. return $this->redis->sismember($this->user_followed_by_db.$this->id, $user);
  186. }
  187. /*
  188. * Tests to see if the relationship between this user and the given user is mutual.
  189. */
  190. public function is_mutual($user) {
  191. return ($this->is_following($user) && $this->is_followed_by($user));
  192. }
  193. /*
  194. * Returns the number of users that this user is following.
  195. */
  196. public function follow_count() {
  197. return intval($this->redis->sCard($this->user_follow_db.$this->id));
  198. }
  199. /*
  200. * Retuns the number of users that follow this user.
  201. */
  202. public function follower_count() {
  203. return intval($this->redis->sCard($this->user_followed_by_db.$this->id));
  204. }
  205. /*
  206. * Finds all users that the given users follow in common.
  207. * Returns an array of user IDs
  208. */
  209. public function common_following($users) {
  210. $redis = $this->redis;
  211. $users[] = $this->id;
  212. $keys = array();
  213. foreach ($users as $user) {
  214. $keys[] = $this->user_follow_db.$user;
  215. }
  216. return $redis->sinter($keys);
  217. }
  218. /*
  219. * Finds all users that all of the given users are followed by in common.
  220. * Returns an array of user IDs
  221. */
  222. public function common_followed_by($users) {
  223. $redis = $this->redis;
  224. $keys = array();
  225. foreach ($users as $user) {
  226. $keys[] = $this->user_followed_by_db.$user;
  227. }
  228. $keys[] = $this->user_follow_db.$this->id;
  229. return $redis->sinter($keys);
  230. }
  231. public function common_private_follower(){
  232. $redis = $this->redis;
  233. $keys = array();
  234. if($GLOBALS['distribution_cfg']['IS_REDIS_JQ']){
  235. $array_followed = $this->followed_by($this->id);
  236. $array_follow = $this->following($this->id);
  237. return array_intersect($array_followed,$array_follow);
  238. }else{
  239. $keys[] = $this->user_followed_by_db.$this->id;
  240. $keys[] = $this->user_follow_db.$this->id;
  241. return $redis->sinter($keys);
  242. }
  243. }
  244. //建立set数组
  245. public function test_new(){
  246. $key = $this->user_follow_db.'77';
  247. $this->redis ->zAdd($key,22,2);
  248. $this->redis ->zAdd($key,33,3);
  249. }
  250. public function zfollowing() {
  251. return $this->redis->zRange($this->user_follow_db.$this->id,0,-1);
  252. }
  253. }//类定义结束
  254. ?>