🎨 #3843 【基础架构】修复 Spring Boot 3(Spring Data Redis 3.x)下 RedisTemplate.getExpire 返回值语义变化导致的过期判断/缓存失效问题

This commit is contained in:
Copilot
2026-01-14 17:05:39 +08:00
committed by GitHub
parent 3b5ead664c
commit 16d5cf9afd
2 changed files with 12 additions and 1 deletions

View File

@@ -29,7 +29,7 @@ public class RedisTemplateWxRedisOps implements WxRedisOps {
@Override
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
@Override

View File

@@ -35,6 +35,17 @@ public class CommonWxRedisOpsTest {
Assert.assertTrue(expireSeconds <= 4 && expireSeconds >= 0);
}
@Test
public void testGetExpireForNonExistentKey() {
String nonExistentKey = "non_existent_key_" + System.currentTimeMillis();
Long expire = wxRedisOps.getExpire(nonExistentKey);
// 对于不存在的 key底层使用 getExpire(key, TimeUnit.SECONDS) 时应返回负值
// Spring Data Redis 2.x 和 3.x 约定:-2 表示 key 不存在,-1 表示 key 没有过期时间
// 因此这里不应返回 null而应返回一个小于 0 的值
Assert.assertNotNull(expire, "Non-existent key should not have null expiration");
Assert.assertTrue(expire < 0, "Non-existent key should have negative expiration");
}
@Test
public void testExpire() {
String key = "access_token", value = String.valueOf(System.currentTimeMillis());