互相关注 php如何实现,redis+php实现微博(二)发布与关注功能详解

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

微博的发布代码:

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");

微博的关注代码:

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程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是ThinkPHP6实现Redis连接池以及Redis队列的详细代码实现。 首先,在ThinkPHP6中使用Redis需要安装`topthink/think-redis`扩展,可以通过以下命令进行安装: ``` composer require topthink/think-redis ``` 接下来,我们需要在项目的配置文件中配置Redis连接信息,可以在`config/database.php`文件中添加以下代码: ```php 'redis' => [ 'type' => 'redis', 'hostname' => '127.0.0.1', 'password' => '', 'port' => 6379, 'select' => 0, 'timeout' => 0, 'prefix' => '', 'persistent' => true, 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, 'wait_timeout' => 3, 'max_idle_time' => 60, ], ], ``` 配置项说明: - `type`:数据库类型,这里填写`redis`。 - `hostname`:Redis主机地址。 - `password`:Redis密码,如果没有设置密码可以不填写。 - `port`:Redis端口号,默认为6379。 - `select`:选择的数据库,默认为0。 - `timeout`:连接Redis的超时时间,默认为0表示不限制。 - `prefix`:设置的键名前缀,默认为空。 - `persistent`:是否使用持久化连接,默认为true。 - `pool`:配置连接池信息,包括最小连接数、最大连接数、等待超时时间和最大空闲时间。 接下来,我们可以通过以下代码获取Redis连接并进行操作: ```php use think\facade\Cache; // 获取Redis连接 $redis = Cache::store('redis')->handler(); // 设置键值对 $redis->set('name', 'Tom'); // 获取键值对 $name = $redis->get('name'); echo $name; ``` 以上代码中,我们使用了ThinkPHP6的缓存门面`think\facade\Cache`来获取Redis连接,通过`store`方法指定使用`redis`缓存驱动,再通过`handler`方法获取Redis连接。 接下来,我们来实现Redis队列功能,具体的代码如下: ```php use think\queue\Job; use think\facade\Cache; // 定义任务处理类 class TestJob { public function fire(Job $job, $data) { // 获取Redis连接 $redis = Cache::store('redis')->handler(); // 从队列中取出任务数据 $name = $data['name']; // 进行任务处理 // ... // 任务处理完成后删除任务 $job->delete(); } } // 将任务加入队列 $jobHandlerClassName = 'TestJob'; // 任务处理类名 $jobData = ['name' => 'Tom']; // 任务数据 $queueName = 'test_queue'; // 队列名称 $delay = 0; // 延迟时间,默认为0 \think\Queue::later($delay, $jobHandlerClassName, $jobData, $queueName); ``` 以上代码中,我们首先定义了一个任务处理类`TestJob`,它实现了`fire`方法来处理任务。在`fire`方法中,我们首先获取Redis连接,然后从队列中取出任务数据,进行任务处理,并最终删除任务。 接下来,我们将任务加入队列。在代码中,我们使用了`think\Queue`门面的`later`方法来将任务加入队列,指定了任务处理类名、任务数据、队列名称和延迟时间(默认为0表示不延迟)。 以上就是ThinkPHP6实现Redis连接池和Redis队列的详细代码实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值