Skip to content

Commit 61a514d

Browse files
Copilotbinarywang
andcommitted
修复 Spring Boot 3 中 RedisTemplate 缓存失效问题
Co-authored-by: binarywang <[email protected]>
1 parent 12b5bf5 commit 61a514d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ 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+
// For non-existent keys, getExpire should return a negative value
43+
// In Spring Data Redis 2.x: may return null (but our implementation should handle this)
44+
// In Spring Data Redis 3.x: returns -2
45+
Assert.assertTrue(expire == null || expire < 0, "Non-existent key should have null or negative expiration");
46+
}
47+
3848
@Test
3949
public void testExpire() {
4050
String key = "access_token", value = String.valueOf(System.currentTimeMillis());

0 commit comments

Comments
 (0)