安装redis
Windows下载地址 https://github.com/tporadowski/redis/releases
根据系统情况下载安装
cmd 窗口 redis-server.exe redis.windows.conf

Linux源码安装
下载地址:http://redis.io/download,下载最新稳定版本。
# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
# tar xzf redis-6.0.8.tar.gz
# cd redis-6.0.8
# make
# cd src
# ./redis-server
配置TP6
config/cache.php
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1'
],
启动redis 更改配置
redis-cli.exe
输入 config get *
查看
113) "notify-keyspace-events"
114) ""
如果是空 执行
config set notify-keyspace-events Ex
config get *
查看
或者 编辑修改 redis.windows.conf
修改 notify-keyspace-events Ex
通过配置启动 redis-server redis.windows.conf
配置 think 命令
自定义命令
php think make:command Hello hello
会生成一个app\command\Hello
配置config/console.php文件
<?php
return [
'commands' => [
'hello' => 'app\command\Hello',
]
];
运行 php think
有没有hello 命令
hello.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;
// use app\common\controller\MyRedis;
use think\facade\Cache;
class Hello extends Command
{
protected function configure()
{
// 指令配置
$this->setName('hello')
->setDescription('the hello command');
}
protected function execute(Input $input, Output $output)
{
$this->getkey();
$output->writeln("ok");
}
public function getkey(){
// ini_set('default_socket_timeout', -1);
//redis 订阅 第二个 是个 回调函数
Cache::store('redis')->handler()->psubscribe(array('__keyevent@0__:expired'), 'app\command\Hello::keyCallback');//回调必须写绝对路径 要不然 会报错
}
public static function keyCallback($redis, $pattern, $channel, $message) {
file_put_contents('order.log',$message."\r\n",FILE_APPEND);
//随便写个 log 记录 不要复制 要不然会在项目的 根目录出现 记得写路径
//在这里写逻辑啊 写数据库操作
echo '已删除订单编号:'.$message;
}
}
编写你的测试代码
app/index/index
use think\facade\Cache;
Cache::store('redis')->setex("wuya1","10","123");
测试
在终端执行 php think hello
页面运行你的测试代码 ***\index\index
终端会打印出
已删除订单编号:**
