-
-
Notifications
You must be signed in to change notification settings - Fork 9k
修复 Spring Boot 3 中 RedisTemplate.getExpire() 兼容性问题 #3843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Co-authored-by: binarywang <[email protected]>
|
auggie review |
🤖 Augment PR SummarySummary: 修复 Spring Boot 3(Spring Data Redis 3.x)下 🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review completed. No suggestions at this time.
Comment augment review to trigger a new review at any time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
这个 PR 修复了 Spring Boot 3 中使用 RedisTemplate 进行配置存储时的缓存失效问题。主要原因是 Spring Data Redis 3.x 改变了 getExpire(K key) 方法的返回值语义:2.x 版本中 key 不存在时返回 null,而 3.x 版本返回 -2。
关键变更
- 将
RedisTemplateWxRedisOps.getExpire()方法中的redisTemplate.getExpire(key)改为redisTemplate.getExpire(key, TimeUnit.SECONDS),确保在两个版本中行为一致 - 新增测试用例
testGetExpireForNonExistentKey()覆盖不存在的 key 场景
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| weixin-java-common/src/main/java/me/chanjar/weixin/common/redis/RedisTemplateWxRedisOps.java | 修改 getExpire 方法实现,使用带 TimeUnit 参数的重载方法确保跨版本兼容性 |
| weixin-java-common/src/test/java/me/chanjar/weixin/common/redis/CommonWxRedisOpsTest.java | 新增测试用例验证不存在的 key 场景下的行为 |
| // For non-existent keys, getExpire should return a negative value | ||
| // In Spring Data Redis 2.x: may return null (but our implementation should handle this) | ||
| // In Spring Data Redis 3.x: returns -2 | ||
| Assert.assertTrue(expire == null || expire < 0, "Non-existent key should have null or negative expiration"); |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释表明 Spring Data Redis 2.x 可能返回 null,但使用 getExpire(key, TimeUnit.SECONDS) 方法后,该方法在 Spring Data Redis 2.x 和 3.x 中都不应该返回 null,而是返回 -2 表示 key 不存在,-1 表示 key 没有过期时间。建议更新注释以反映实际行为,或者移除对 null 的检查。
| // For non-existent keys, getExpire should return a negative value | |
| // In Spring Data Redis 2.x: may return null (but our implementation should handle this) | |
| // In Spring Data Redis 3.x: returns -2 | |
| Assert.assertTrue(expire == null || expire < 0, "Non-existent key should have null or negative expiration"); | |
| // 对于不存在的 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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review completed. No suggestions at this time.
Comment augment review to trigger a new review at any time.
Spring Data Redis 3.x 改变了
getExpire(K key)的返回值语义:2.x 返回 null 表示 key 不存在,3.x 返回 -2。这导致使用WxOpenInRedisTemplateConfigStorage等 RedisTemplate 配置存储的用户在升级到 Spring Boot 3.x 后缓存失效。修改内容
getExpire(key, TimeUnit.SECONDS)替代getExpire(key),该重载方法在两个版本中行为一致testGetExpireForNonExistentKey()覆盖不存在 key 的场景此修改向后兼容 Spring Data Redis 2.x,同时修复 3.x 的兼容性问题。
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.