Skip to content

Commit 16d5cf9

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

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/redis/RedisTemplateWxRedisOps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
2929

3030
@Override
3131
public Long getExpire(String key) {
32-
return redisTemplate.getExpire(key);
32+
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
3333
}
3434

3535
@Override

weixin-java-common/src/test/java/me/chanjar/weixin/common/redis/CommonWxRedisOpsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ public void testGetExpire() {
3535
Assert.assertTrue(expireSeconds <= 4 && expireSeconds >= 0);
3636
}
3737

38+
@Test
39+
public void testGetExpireForNonExistentKey() {
40+
String nonExistentKey = "non_existent_key_" + System.currentTimeMillis();
41+
Long expire = wxRedisOps.getExpire(nonExistentKey);
42+
// 对于不存在的 key,底层使用 getExpire(key, TimeUnit.SECONDS) 时应返回负值
43+
// Spring Data Redis 2.x 和 3.x 约定:-2 表示 key 不存在,-1 表示 key 没有过期时间
44+
// 因此这里不应返回 null,而应返回一个小于 0 的值
45+
Assert.assertNotNull(expire, "Non-existent key should not have null expiration");
46+
Assert.assertTrue(expire < 0, "Non-existent key should have negative expiration");
47+
}
48+
3849
@Test
3950
public void testExpire() {
4051
String key = "access_token", value = String.valueOf(System.currentTimeMillis());

0 commit comments

Comments
 (0)