情况1:(Windows)本地 webman + ThinkCache 组件使用 Redis 驱动,正常。
情况2:(Linux)服务器 webman + ThinkCache 组件使用 Redis 驱动,无反应,不报错。
情况3:(Linux)服务器 webman + 自己写连接,不用组件,无反应,不报错。
情况4:(Linux)服务器 普通PHP脚本 + 自己写连接,不用组件,正常。
问题会出在哪里?
框架内用法(无反应,不报错):
$lockKey = "test";
$lockValue = uniqid('', true);
$redis = Cache::store('redis')->handler();
$redis->set($lockKey, $lockValue, ['NX', 'EX' => 3])
框架内自己连接(无反应,不报错):
$lockKey = "test";
$lockValue = uniqid('', true);
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set($lockKey, $lockValue, ['NX', 'EX' => 30])
think-cache.php
<?php
return [
// 默认缓存驱动
'default' => 'file',
// 缓存连接方式配置
'stores' => [
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1',
// 缓存前缀
'prefix' => 'cache:',
// 默认缓存有效期 0表示永久缓存
'expire' => 0,
// Thinkphp官方没有这个参数,由于生成的tag键默认不过期,如果tag键数量很大,避免长时间占用内存,可以设置一个超过其他缓存的过期时间,0为不设置
'tag_expire' => 86400 * 30,
// 缓存标签前缀
'tag_prefix' => 'tag:',
// 连接池配置
'pool' => [
'max_connections' => 5, // 最大连接数
'min_connections' => 1, // 最小连接数
'wait_timeout' => 3, // 从连接池获取连接等待超时时间
'idle_timeout' => 60, // 连接最大空闲时间,超过该时间会被回收
'heartbeat_interval' => 50, // 心跳检测间隔,需要小于60秒
],
],
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => runtime_path() . '/file/',
],
],
];
自己写脚本(正常):
<?php
// 配置 Redis 服务器连接信息
$host = '127.0.0.1'; // Redis 服务器地址
$port = 6379; // Redis 服务器端口
$password = null; // Redis 服务器密码, 如果没有密码可以设置为 null
// 创建 Redis 对象
$redis = new Redis();
try {
// 连接到 Redis 服务器
if ($password) {
$redis->auth($password); // 如果需要密码,进行身份验证
}
$redis->connect($host, $port);
echo "Connected to Redis server at $host:$port\n";
// 测试设置和获取键的值
$testKey = 'test_key';
$testValue = 'Hello, Redis!';
// 设置键
$redis->set($testKey, $testValue, ['NX', 'EX' => 30]);
// 获取键
$retrievedValue = $redis->get($testKey);
if ($retrievedValue === $testValue) {
echo "Successfully set and retrieved the value: '$retrievedValue'\n";
} else {
echo "Failed to retrieve the correct value. Expected '$testValue', got '$retrievedValue'\n";
}
} catch (RedisException $e) {
echo "Could not connect to Redis: " . $e->getMessage() . "\n";
}
?>
老哥 啥也不贴出来就这么干问嘛
有没有可能本地和服务配置不一致,本地用的redis缓存,服务器用的file缓存
配置一样,代码一致。框架外普通脚本可以,框架内自己写连接或者用think-cache的配置都没有反应。
你配置里写的file驱动啊,没走redis
$redis = Cache::store('redis')->handler();
这里指定Redis了啊
你说的没反应是什么现象呢?你想让它有什么反应呢?
数据库没有操作成功,没有写入,后续逻辑也不执行了
$redis->set($lockKey, $lockValue, ['NX', 'EX' => 3])
把EX写大一点,比如 300
3太短了,过3秒你去查数据库就没有了。
我用你这个代码测试,去查redis也没有数据,但是我把 'EX' => 3 改为 'EX' => 300 就有数据了。
3太短了,来不及查就没了
也不行,本地就可以
估计是低级错误
服务器上运行 tcpdump -Ans 4000 -iany port 6379
然后访问webman页面,贴下tcpdump 结果我看下能不能找到问题
tcpdump -Ans 4000 -iany port 6379
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 4000 bytes
15:54:29.166219 IP 127.0.0.1.51184 > 127.0.0.1.6379: Flags [P.], seq 2375290192:2375290222, ack 1695099314, win 342, options [nop,nop,TS val 2378503168 ecr 2378453143], length 30: RESP "GET" "cache:PING"
E..R..@.@..................Pe )....V.F.....
......P.*2
$3
GET
$10
cache:PING
15:54:29.166309 IP 127.0.0.1.6379 > 127.0.0.1.51184: Flags [P.], seq 1:6, ack 30, win 350, options [nop,nop,TS val 2378503168 ecr 2378503168], length 5: RESP null
E..9.U@.@..g............e )....n...^.-.....
........$-1
15:54:29.166328 IP 127.0.0.1.51184 > 127.0.0.1.6379: Flags [.], ack 6, win 342, options [nop,nop,TS val 2378503168 ecr 2378503168], length 0
E..4..@.@..................ne )....V.(.....
........
15:55:12.874420 IP 127.0.0.1.53298 > 127.0.0.1.6379: Flags [S], seq 1951035457, win 43690, options [mss 65495,sackOK,TS val 2378546876 ecr 0,nop,wscale 7], length 0
E..<..@.@.m..........2..tJpA.........0.........
............
15:55:12.874433 IP 127.0.0.1.6379 > 127.0.0.1.53298: Flags [S.], seq 3823983597, ack 1951035458, win 43690, options [mss 65495,sackOK,TS val 2378546876 ecr 2378546876,nop,wscale 7], length 0
E..<..@.@.<............2..[.tJpB.....0.........
............
15:55:12.874442 IP 127.0.0.1.53298 > 127.0.0.1.6379: Flags [.], ack 1, win 342, options [nop,nop,TS val 2378546876 ecr 2378546876], length 0
E..4..@.@.m..........2..tJpB..[....V.(.....
........
15:55:12.874545 IP 127.0.0.1.53298 > 127.0.0.1.6379: Flags [P.], seq 1:102, ack 1, win 342, options [nop,nop,TS val 2378546876 ecr 2378546876], length 101: RESP "SET" "kp_lock:57903210418@chatroom" "695a1ce0d55028.66319091" "EX" "3" "NX"
E.....@.@.m].........2..tJpB..[....V.......
........*6
$3
SET
$28
kp_lock:57903210418@chatroom
$23
695a1ce0d55028.66319091
$2
EX
$1
"SET" "kp_lock:57903210418@chatroom" "695a1ce0d55028.66319091" "EX" "3" "NX"
从这里可以看出来,EX传的还是3,要么你没改代码,要么没restart重启webman
这样试的也不行:
use support\Redis;
Redis::set('aa', 'bb');
// 无法设置,没有报错
// 后续代码也不执行了
你先把 EX 为3的问题解决呗,不然白和你说这一堆了
我测试不用ex,不设置过期时间也不行呢
你写300呗
安装 webman/redis 测试:
config/redis.php