- 尾部写入
- 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
-
- 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、宝塔添加守护进程(以防死掉,守护进行重新启动)
5、tp6官方说数据库配置文件开启
// 是否需要断线重连 'break_reconnect' => true,