经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
redis+php实现微博(二)发布与关注功能详解
来源:jb51  时间:2019/9/24 8:33:39  对本文有异议

本文实例讲述了redis+php实现微博发布与关注功能。分享给大家供大家参考,具体如下:

数据结构:

set post:postid:3:time timestamp
set post:postid:3:userid 5
set post:postid:3:content 测试发布哈哈哈哈

incr global:postid
set post:postid:$postidcho "用户名密码不能够为空!";

关注微博

following:3

被关注(粉丝)

followed:3

把发布的微博推给自己的粉丝

recivepost:10 postid

微博的发布代码:

  1. include("function.php");
  2. include("header.php");
  3. $content = I('content');
  4. if(!$content){
  5. error('内容不能够为空');
  6. }
  7. $user = isLogin();
  8. if($user==false){
  9. header("location:index.php");
  10. exit();
  11. }
  12. $r = redis_connect();
  13. $postid = $r->incr('global:postid');
  14. //$r->set("post:postid:".$postid.":time",time());
  15. //$r->set("post:postid:".$postid.":userid",$user['userid']);
  16. //$r->set("post:postid:".$postid.":content",$content);
  17. $r->hmset("post:postid:".$postid,array('userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content));
  18. //把微博推给自己的粉丝
  19. $fans = $r->smembers("followed:".$user['userid']);
  20. $fans[] = $user['userid'];
  21. foreach($fans as $fansid){
  22. $r->lpush('recivepost:'.$fansid,$postid);
  23. }
  24. //单独累计个人发布的信息
  25. $r->lpush('userpostid:'.$user['userid'],$postid);
  26. header("location:home.php");
  27. exit;
  28. include("bottom.php");
  29.  

微博的关注代码:

  1. include("function.php");
  2. include("header.php");
  3. if(isLogin()==false){
  4. header("location:index.php");
  5. exit;
  6. }
  7. $user = isLogin();
  8. $uid = trim($_GET['uid']);
  9. $f = trim($_GET['f']);
  10. $r = redis_connect();
  11. if($f==0){
  12. //将关注与被关注的数据结构存入redis
  13. $r->sadd("following:".$user['userid'],$uid);
  14. $r->sadd("followed:".$uid,$user['userid']);
  15. }else{
  16. //取消关注
  17. $r->srem("following:".$user['userid'],$uid);
  18. $r->srem("followed:".$uid,$user['userid']);
  19. }
  20. //根据传递过来的userid查找username
  21. $uname = $r->get("user:userid:".$uid.":username");
  22. header("location:profile.php?u=".$uname);
  23. include("bottom.php");
  24.  

更多关于PHP相关内容感兴趣的读者可查看jb51专题:《php+redis数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号