From 65c1cdc07c89bb70aab81a94c3559b68eff3f1fd Mon Sep 17 00:00:00 2001 From: taneg <30307000+taneg@users.noreply.github.com> Date: Fri, 14 May 2021 10:52:04 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20open=E6=A8=A1=E5=9D=97=E7=A7=BB?= =?UTF-8?q?=E9=99=A4Jedis=E5=BC=BA=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...disTemplateConfigStorageConfiguration.java | 10 +- weixin-java-open/pom.xml | 4 + .../WxOpenInRedisTemplateConfigStorage.java | 145 ++++++++++++++++++ 3 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisTemplateConfigStorage.java diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedisTemplateConfigStorageConfiguration.java b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedisTemplateConfigStorageConfiguration.java index 54daf8a52..79521c921 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedisTemplateConfigStorageConfiguration.java +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedisTemplateConfigStorageConfiguration.java @@ -2,11 +2,10 @@ package com.binarywang.spring.starter.wxjava.open.config.storage; import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; import lombok.RequiredArgsConstructor; -import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; -import me.chanjar.weixin.common.redis.WxRedisOps; import me.chanjar.weixin.open.api.WxOpenConfigStorage; import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; -import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage; +import me.chanjar.weixin.open.api.impl.WxOpenInRedisTemplateConfigStorage; + import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -35,9 +34,8 @@ public class WxOpenInRedisTemplateConfigStorageConfiguration extends AbstractWxO return this.config(config, properties); } - private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() { + private WxOpenInRedisTemplateConfigStorage getWxOpenInRedisTemplateConfigStorage() { StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); - WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); - return new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); + return new WxOpenInRedisTemplateConfigStorage(redisTemplate, properties.getConfigStorage().getKeyPrefix()); } } diff --git a/weixin-java-open/pom.xml b/weixin-java-open/pom.xml index a9dacb70b..507856448 100644 --- a/weixin-java-open/pom.xml +++ b/weixin-java-open/pom.xml @@ -91,6 +91,10 @@ org.redisson redisson + + org.springframework.data + spring-data-redis + diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisTemplateConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisTemplateConfigStorage.java new file mode 100644 index 000000000..da04b176b --- /dev/null +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInRedisTemplateConfigStorage.java @@ -0,0 +1,145 @@ +package me.chanjar.weixin.open.api.impl; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; + +import lombok.NonNull; +import me.chanjar.weixin.common.redis.JedisWxRedisOps; +import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; +import me.chanjar.weixin.common.redis.WxRedisOps; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.util.Pool; + +/** + *
+ *     RedisTemplateConfigStorage
+ * 
+ * + * @author taneg + * @date 2021/05/13 11:12:35 + */ +public class WxOpenInRedisTemplateConfigStorage extends AbstractWxOpenInRedisConfigStorage { + + private final WxRedisOps redisOps; + + public WxOpenInRedisTemplateConfigStorage(@NonNull StringRedisTemplate stringRedisTemplate, String keyPrefix) { + this(new RedisTemplateWxRedisOps(stringRedisTemplate), keyPrefix); + } + + public WxOpenInRedisTemplateConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) { + this.redisOps = redisOps; + this.keyPrefix = keyPrefix; + } + + @Override + public String getComponentVerifyTicket() { + return redisOps.getValue(this.componentVerifyTicketKey); + } + + @Override + public void setComponentVerifyTicket(String componentVerifyTicket) { + redisOps.setValue(this.componentVerifyTicketKey, componentVerifyTicket, Integer.MAX_VALUE, TimeUnit.SECONDS); + } + + @Override + public String getComponentAccessToken() { + return redisOps.getValue(this.componentAccessTokenKey); + } + + @Override + public boolean isComponentAccessTokenExpired() { + Long expire = redisOps.getExpire(this.componentAccessTokenKey); + return expire == null || expire < 2; + } + + @Override + public void expireComponentAccessToken() { + redisOps.expire(this.componentAccessTokenKey, 0, TimeUnit.SECONDS); + } + + @Override + public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) { + redisOps.setValue(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); + } + + @Override + public String getAuthorizerRefreshToken(String appId) { + return redisOps.getValue(this.getKey(this.authorizerRefreshTokenKey, appId)); + } + + @Override + public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { + redisOps.setValue(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0, TimeUnit.SECONDS); + } + + @Override + public String getAuthorizerAccessToken(String appId) { + return redisOps.getValue(this.getKey(this.authorizerAccessTokenKey, appId)); + } + + @Override + public boolean isAuthorizerAccessTokenExpired(String appId) { + Long expire = redisOps.getExpire(this.getKey(this.authorizerAccessTokenKey, appId)); + return expire == null || expire < 2; + } + + @Override + public void expireAuthorizerAccessToken(String appId) { + redisOps.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0, TimeUnit.SECONDS); + } + + @Override + public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) { + redisOps.setValue(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); + } + + @Override + public String getJsapiTicket(String appId) { + return redisOps.getValue(this.getKey(this.jsapiTicketKey, appId)); + } + + @Override + public boolean isJsapiTicketExpired(String appId) { + Long expire = redisOps.getExpire(this.getKey(this.jsapiTicketKey, appId)); + return expire == null || expire < 2; + } + + @Override + public void expireJsapiTicket(String appId) { + redisOps.expire(this.getKey(this.jsapiTicketKey, appId), 0, TimeUnit.SECONDS); + } + + @Override + public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) { + redisOps.setValue(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); + } + + @Override + public String getCardApiTicket(String appId) { + return redisOps.getValue(this.getKey(this.cardApiTicket, appId)); + } + + @Override + public boolean isCardApiTicketExpired(String appId) { + Long expire = redisOps.getExpire(this.getKey(this.cardApiTicket, appId)); + return expire == null || expire < 2; + } + + @Override + public void expireCardApiTicket(String appId) { + redisOps.expire(this.getKey(this.cardApiTicket, appId), 0, TimeUnit.SECONDS); + } + + @Override + public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) { + redisOps.setValue(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); + } + + @Override + public Lock getLockByKey(String key) { + return redisOps.getLock(key); + } +}