tp6+redis过期删除文件
1.创建自定义指令:https://www.kancloud.cn/manual/thinkphp6_0/1037651
php think make:command Rd rd
会生成app\command\Rd.php,编辑Rd.php
<?php declare (strict_types = 1); namespace app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class Rd extends Command { protected function configure() { // 指令配置 $this->setName('rd') ->setDescription('the rd command'); } protected function execute(Input $input, Output $output) { echo "已执行此execute方法!"; $redis = new \think\cache\driver\Redis(); //$redis = new \Redis(); $redis->connect("127.0.0.1", 6379); $redis->config('notify-keyspace-events','Ex'); //$redis->setOption(\think\cache\driver\Redis::OPT_READ_TIMEOUT, -1); //ini_set('default_socket_timeout',"-1");//设置多长时间没有反应就会终止-1代表永远 // $redis ->auth('');//password $redis->psubscribe(array('__keyevent@0__:expired'),function ($redis, $pattern, $channel, $message){ echo("redis----订阅"); echo "<br>"; //app()->getRootPath()项目根目录 $url = app()->getRootPath()."public/static/".$message; echo $url; //unlink($url); }); // 指令输出 $output->writeln('rd'); } }
2.配置config/console.php文件
<?php return [ 'commands' => [ 'rd' => 'app\command\Rd', ] ];
3.在宝塔面板服务器安装redis
4.安装所运行的php版本redis扩展
5. 重启redis和php版本
6.安装php版本的php-cli,可用命令执行
7.测试阶段:登录linux服务器:
[root@VM-4-10-centos ~]# cd /www/wwwroot/blqcs.xxxxxx.com
[root@VM-4-10-centos blqcs.xxxxxx.com]# php think rd
8.在app/controler/Index.php编辑,并在public/static创建a.txt
<?php namespace app\controller; use app\BaseController; class Index extends BaseController { public function test() { // echo $_SERVER['DOCUMENT_ROOT']; // echo "<br>"; // echo app()->getRootPath(); $redis = new \think\cache\driver\Redis(); $redis->connect("127.0.0.1", 6379); $redis->setex("a.txt",3,12);//s echo "写入redis成功"; } public function get(){ $redis = new \Redis(); $redis->connect("127.0.0.1", 6379); echo $redis->get("a.txt");//查询key // echo $redis->ttl("123");//过期时间 // var_dump($redis->keys("*"));//所有key // $redis->del("123");//删除key // $a = "[]"; // $c = "['https://gyycgyyxcx.xxxxx.com/uploads/220711_12/lhBn1ICXZrsHjeQpWoNVAbFt.jpeg']"; // echo strlen($a); // echo "<br />"; // echo strlen($c); } public function info(){ echo(phpinfo()); } }
9.执行info方法查看是否有redis字样,执行test方法=》查看7步骤是否有输出,并在public/static/a.txt是否删除
10.宝塔安装守护进程软件:Supervisor管理器 2.2
设置->添加守护进程->名称随意;启动命令:php think rd 执行目录:项目根目录
--完---
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术