Skip to content
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

修改 Redis 配置实现类以实现选择数据库及密码认证功能 #1314

Merged
merged 1 commit into from
Dec 8, 2019

Conversation

xJoeWoo
Copy link

@xJoeWoo xJoeWoo commented Dec 5, 2019

主要是将 cn.binarywang.wx.miniapp.config.impl.WxMaRedisConfigImpl 重命名为 AbstractWxMaRedisConfig,在保留原来的 Redis 存储逻辑下,抽象了一个 getJedis() 方法出来,用于从子类获得 Jedis 实例:
https://github.com/xJoeWoo/WxJava/blob/e29680928023a5f92491fd5a2a0beebf42b216d8/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/AbstractWxMaRedisConfig.java#L53

并且在 AbstractWxMaRedisConfig 类中添加了一个回调接口 JedisConfig,用于配置每次操作的 Jedis 实例:

https://github.com/xJoeWoo/WxJava/blob/e29680928023a5f92491fd5a2a0beebf42b216d8/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/AbstractWxMaRedisConfig.java#L18-L20

在调用 setJedisConfig 设置回调后,可以在回调接口实现中,进行 select() 选择数据库,或 auth() 进行密码认证等配置操作:

 setJedisConfig(new JedisConfig() {
      @Override
      public Jedis config(Jedis jedis) {
        jedis.select(3);
        jedis.auth("whatever");
        return jedis;
      }
    });

其它改进:

  1. 为了减少重复代码(主要是读写存储在内存中的字段),将 AbstractWxMaRedisConfig 修改成继承 WxMaDefaultConfigImpl
  2. 添加 WxMaDefaultConfigImpl.expiresAheadInMillis 方法,用于统一修改提前过期时间
    https://github.com/xJoeWoo/WxJava/blob/e29680928023a5f92491fd5a2a0beebf42b216d8/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaDefaultConfigImpl.java#L51-L56
  3. 添加 WxMaDefaultConfigImpl.isExpired 方法,用于统一判断传入时间是否过期
    https://github.com/xJoeWoo/WxJava/blob/e29680928023a5f92491fd5a2a0beebf42b216d8/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaDefaultConfigImpl.java#L58-L63
  4. 添加 AbstractWxMaRedisConfig.redisKeyPrefix 字段,用于修改存放配置参数的 Redis Key
    https://github.com/xJoeWoo/WxJava/blob/e29680928023a5f92491fd5a2a0beebf42b216d8/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/AbstractWxMaRedisConfig.java#L29-L32
  5. WxMaRedisConfigImpl 添加了传入 JedisPool 的构造方法,同时将无参构造函数和 setJedisPool 置为过时。因为此配置类必须依赖 JedisPool,适合从构造方法传入,否则容易忽略调用 setJedisPool 设置 JedisPool。

若此方案可行,这里可以一并修改其它项目的 Redis 配置类。

@xJoeWoo
Copy link
Author

xJoeWoo commented Dec 5, 2019

另外修改的时候发现了 #1313 ,不知是依赖不对还是分支没开发完成

@binarywang binarywang merged commit 232ea7e into binarywang:develop Dec 8, 2019
yuanqixun pushed a commit to yuanqixun/weixin-java-tools that referenced this pull request Dec 9, 2019
…-tools into develop

* 'develop' of https://github.com/Wechat-Group/weixin-java-tools:
  🎨 优化重构微信小程序订阅消息相关类
  🎨 binarywang#1314 优化增强微信小程序Redis配置实现类功能
  ✨	binarywang#1309 增加发送和查询企业微信红包的接口
  🔖 发布3.6.1.B测试版本
  ✨ binarywang#1033 微信支付增加发送小程序红包的接口
  Update README.md
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.

2 participants