From 3481cca8ec71167974905da3de2af3a19df56f03 Mon Sep 17 00:00:00 2001 From: Charming Date: Thu, 26 Apr 2018 12:10:55 +0800 Subject: [PATCH] =?UTF-8?q?#559=20=E5=BE=AE=E4=BF=A1=E5=BC=80=E6=94=BE?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=EF=BC=9A1.=20WxOpenInRedisConfigStorage=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20JedisPool/JedisSentinelPool=20=E7=AD=89=20?= =?UTF-8?q?Pool=20=E7=9A=84=E5=AD=90=E7=B1=BB=EF=BC=9B2.=20WxOpenIn?= =?UTF-8?q?RedisConfigStorage=20=E5=A2=9E=E5=8A=A0=20keyPrefix=20=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=AF=E9=85=8D=E7=BD=AE=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E7=BC=80=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/impl/WxOpenInRedisConfigStorage.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisConfigStorage.java index eb2d5e54..3745a150 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisConfigStorage.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisConfigStorage.java @@ -1,7 +1,9 @@ package me.chanjar.weixin.open.api.impl; +import org.apache.commons.lang3.StringUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; +import redis.clients.util.Pool; /** * @author 007 @@ -15,7 +17,11 @@ public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage { private final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket:"; private final static String CARD_API_TICKET_KEY = "wechat_card_api_ticket:"; - protected final JedisPool jedisPool; + protected final Pool jedisPool; + /** + * redis 存储的 key 的前缀,可为空 + */ + private String keyPrefix; private String componentVerifyTicketKey; private String componentAccessTokenKey; private String authorizerRefreshTokenKey; @@ -23,6 +29,15 @@ public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage { private String jsapiTicketKey; private String cardApiTicket; + public WxOpenInRedisConfigStorage(Pool jedisPool) { + this.jedisPool = jedisPool; + } + + public WxOpenInRedisConfigStorage(Pool jedisPool, String keyPrefix) { + this.jedisPool = jedisPool; + this.keyPrefix = keyPrefix; + } + public WxOpenInRedisConfigStorage(JedisPool jedisPool) { this.jedisPool = jedisPool; } @@ -30,10 +45,12 @@ public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage { @Override public void setComponentAppId(String componentAppId) { super.setComponentAppId(componentAppId); - this.componentVerifyTicketKey = COMPONENT_VERIFY_TICKET_KEY.concat(componentAppId); - this.componentAccessTokenKey = COMPONENT_ACCESS_TOKEN_KEY.concat(componentAppId); - this.authorizerRefreshTokenKey = AUTHORIZER_REFRESH_TOKEN_KEY.concat(componentAppId); - this.authorizerAccessTokenKey = AUTHORIZER_ACCESS_TOKEN_KEY.concat(componentAppId); + String prefix = StringUtils.isBlank(keyPrefix) ? "" : + (StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":")); + componentVerifyTicketKey = prefix + COMPONENT_VERIFY_TICKET_KEY.concat(componentAppId); + componentAccessTokenKey = prefix + COMPONENT_ACCESS_TOKEN_KEY.concat(componentAppId); + authorizerRefreshTokenKey = prefix + AUTHORIZER_REFRESH_TOKEN_KEY.concat(componentAppId); + authorizerAccessTokenKey = prefix + AUTHORIZER_ACCESS_TOKEN_KEY.concat(componentAppId); this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(componentAppId); this.cardApiTicket = CARD_API_TICKET_KEY.concat(componentAppId); }