tp6使用redis消息队列

尾部写入
for ($i=1;$i<=1000;$i++){
    Cache::store('redis')->rpush('list',date("Y-m-d H:i:s")."消息{$i}");
}

头部读取消息队列并删除 
$list = Cache::store('redis')->lpop('list');

1、新建个方法运行写入队列

public function hello(){
   for ($i=1;$i<=1000;$i++){
       Cache::store('redis')->rpush('list',date("Y-m-d H:i:s")."消息{$i}");
   }
}

2、tp6创建命令生成文件Dade

php think make:command Dade

加入命令config/console.php

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        //定时任务命令
        'Dade' => app\command\Dade::class
    ],
];

编写

Dade.php

<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\admin\model\Dade;
use think\facade\Cache;

class Dade extends Command
{
    protected function configure()
    {
        $this->setName('ExcelTask');
    }

    protected function execute(Input $input, Output $output)
    {
        //php think OrderTask
        $this->start();
        $output->writeln('操作成功');
    }

    /**
     * 启动
     */
    public function start()
    {
        while (1) {
            //执行读取,看效果
            sleep(1);
            $list = Cache::store('redis')->lpop('list');
            if(!empty($list)){
                $data = [];
                $data['name'] = "大得";
                $data['date'] = $list;
                Dade::create($data);
                echo $list."\n";
            }
        }
    }

}

3.执行

php think Dade

4、宝塔添加守护进程(以防死掉,守护进行重新启动)

Supervisor

5、tp6官方说数据库配置文件开启

// 是否需要断线重连
'break_reconnect'   => true,

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值