redis+php实现微博(二)发布与关注功能详解

2025-05-29 0 99

本文实例讲述了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

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
include("function.php");

include("header.php");

$content = I('content');

if(!$content){

error('内容不能够为空');

}

$user = isLogin();

if($user==false){

header("location:index.php");

exit();

}

$r = redis_connect();

$postid = $r->incr('global:postid');

//$r->set("post:postid:".$postid.":time",time());

//$r->set("post:postid:".$postid.":userid",$user['userid']);

//$r->set("post:postid:".$postid.":content",$content);

$r->hmset("post:postid:".$postid,array('userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content));

//把微博推给自己的粉丝

$fans = $r->smembers("followed:".$user['userid']);

$fans[] = $user['userid'];

foreach($fans as $fansid){

$r->lpush('recivepost:'.$fansid,$postid);

}

//单独累计个人发布的信息

$r->lpush('userpostid:'.$user['userid'],$postid);

header("location:home.php");

exit;

include("bottom.php");

微博的关注代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
include("function.php");

include("header.php");

if(isLogin()==false){

header("location:index.php");

exit;

}

$user = isLogin();

$uid = trim($_GET['uid']);

$f = trim($_GET['f']);

$r = redis_connect();

if($f==0){

//将关注与被关注的数据结构存入redis

$r->sadd("following:".$user['userid'],$uid);

$r->sadd("followed:".$uid,$user['userid']);

}else{

//取消关注

$r->srem("following:".$user['userid'],$uid);

$r->srem("followed:".$uid,$user['userid']);

}

//根据传递过来的userid查找username

$uname = $r->get("user:userid:".$uid.":username");

header("location:profile.php?u=".$uname);

include("bottom.php");

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

原文链接:https://www.cnblogs.com/lisqiong/p/5580927.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 redis+php实现微博(二)发布与关注功能详解 https://www.kuaiidc.com/92403.html

相关文章

发表评论
暂无评论