Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Spring Data Redis 3.x 改变了 getExpire(K key) 的返回值语义:2.x 返回 null 表示 key 不存在,3.x 返回 -2。这导致使用 WxOpenInRedisTemplateConfigStorage 等 RedisTemplate 配置存储的用户在升级到 Spring Boot 3.x 后缓存失效。

修改内容

  • RedisTemplateWxRedisOps.getExpire(): 使用 getExpire(key, TimeUnit.SECONDS) 替代 getExpire(key),该重载方法在两个版本中行为一致
  • CommonWxRedisOpsTest: 新增 testGetExpireForNonExistentKey() 覆盖不存在 key 的场景
// Before
return redisTemplate.getExpire(key);

// After
return redisTemplate.getExpire(key, TimeUnit.SECONDS);

此修改向后兼容 Spring Data Redis 2.x,同时修复 3.x 的兼容性问题。

Original prompt

This section details on the original issue you should resolve

<issue_title>使用RedisTemplate 缓存 失效</issue_title>
<issue_description> WxOpenInRedisTemplateConfigStorage inRedisConfigStorage = new WxOpenInRedisTemplateConfigStorage(redisTemplate,"wxchat:");

com.github.binarywang weixin-java-open 4.6.0

springboot 版本 是3.2.5</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix RedisTemplate cache expiration issue 修复 Spring Boot 3 中 RedisTemplate.getExpire() 兼容性问题 Jan 9, 2026
Copilot AI requested a review from binarywang January 9, 2026 06:35
@binarywang binarywang marked this pull request as ready for review January 9, 2026 07:12
Copilot AI review requested due to automatic review settings January 9, 2026 07:12
@binarywang
Copy link
Owner

auggie review

@augmentcode
Copy link

augmentcode bot commented Jan 9, 2026

🤖 Augment PR Summary

Summary: 修复 Spring Boot 3(Spring Data Redis 3.x)下 RedisTemplate.getExpire 返回值语义变化导致的过期判断/缓存失效问题。

Changes:RedisTemplateWxRedisOps#getExpire 改为调用 getExpire(key, TimeUnit.SECONDS),并新增不存在 key 的场景单测覆盖。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a 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.

Copy link

Copilot AI left a 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 场景下的行为

Comment on lines +42 to +45
// 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");
Copy link

Copilot AI Jan 9, 2026

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 的检查。

Suggested change
// 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");

Copilot uses AI. Check for mistakes.
Copy link

@augmentcode augmentcode bot left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用RedisTemplate 缓存 失效

2 participants