();
- hash.put(HASH_VALUE_FIELD, value);
- hash.put(HASH_EXPIRE_FIELD, String.valueOf(expiresTime));
- jedis.hmset(getRedisKey(key), hash);
- }
- }
-
- private long getExpireFromRedis(String key) {
- try (Jedis jedis = jedisPool.getResource()) {
- String expire = jedis.hget(getRedisKey(key), HASH_EXPIRE_FIELD);
- return expire == null ? 0 : Long.parseLong(expire);
- }
- }
-
- private void setExpire(String key, long expiresTime) {
- try (Jedis jedis = jedisPool.getResource()) {
- jedis.hset(getRedisKey(key), HASH_EXPIRE_FIELD, String.valueOf(expiresTime));
- }
+ public WxMaRedisConfigImpl(JedisPool jedisPool) {
+ this.jedisPool = jedisPool;
}
+ /**
+ * 使用 WxMaRedisConfigImpl(JedisPool) 构造方法来设置 JedisPool
+ */
+ @Deprecated
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
- public void setMaId(String maId) {
- this.maId = maId;
- }
-
- @Override
- public String getAccessToken() {
- return getValueFromRedis(ACCESS_TOKEN);
- }
-
- @Override
- public Lock getAccessTokenLock() {
- if (accessTokenLock == null) {
- synchronized (this) {
- if (accessTokenLock == null) {
- accessTokenLock = new DistributedLock(getRedisKey("accessTokenLock"));
- }
- }
- }
- return accessTokenLock;
- }
-
- @Override
- public boolean isAccessTokenExpired() {
- return System.currentTimeMillis() > getExpireFromRedis(ACCESS_TOKEN);
- }
-
- @Override
- public synchronized void updateAccessToken(WxAccessToken accessToken) {
- updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
- }
-
- @Override
- public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
- setValueToRedis(ACCESS_TOKEN, System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L, accessToken);
- }
-
- @Override
- public String getJsapiTicket() {
- return getValueFromRedis(JSAPI_TICKET);
- }
-
- @Override
- public Lock getJsapiTicketLock() {
- if (jsapiTicketLock == null) {
- synchronized (this) {
- if (jsapiTicketLock == null) {
- jsapiTicketLock = new DistributedLock(getRedisKey("jsapiTicketLock"));
- }
- }
- }
- return jsapiTicketLock;
- }
-
- @Override
- public boolean isJsapiTicketExpired() {
- return System.currentTimeMillis() > getExpireFromRedis(JSAPI_TICKET);
- }
-
- @Override
- public void expireJsapiTicket() {
- setExpire(JSAPI_TICKET, 0);
- }
-
- @Override
- public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
- // 预留200秒的时间
- setValueToRedis(JSAPI_TICKET, System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L, jsapiTicket);
- }
-
-
- @Override
- public String getCardApiTicket() {
- return getValueFromRedis(CARD_API_TICKET);
- }
-
- @Override
- public Lock getCardApiTicketLock() {
- if (cardApiTicketLock == null) {
- synchronized (this) {
- if (cardApiTicketLock == null) {
- cardApiTicketLock = new DistributedLock(getRedisKey("cardApiTicketLock"));
- }
- }
- }
- return cardApiTicketLock;
- }
-
- @Override
- public boolean isCardApiTicketExpired() {
- return System.currentTimeMillis() > getExpireFromRedis(CARD_API_TICKET);
- }
-
- @Override
- public void expireCardApiTicket() {
- setExpire(CARD_API_TICKET, 0);
- }
-
- @Override
- public void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) {
- setValueToRedis(CARD_API_TICKET, System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L, cardApiTicket);
- }
-
- @Override
- public void expireAccessToken() {
- setExpire(ACCESS_TOKEN, 0);
- }
-
- @Override
- public String getSecret() {
- return this.secret;
- }
-
- public void setSecret(String secret) {
- this.secret = secret;
- }
-
- @Override
- public String getToken() {
- return this.token;
- }
-
- public void setToken(String token) {
- this.token = token;
- }
-
- @Override
- public long getExpiresTime() {
- return getExpireFromRedis(ACCESS_TOKEN);
- }
-
- @Override
- public String getAesKey() {
- return this.aesKey;
- }
-
- public void setAesKey(String aesKey) {
- this.aesKey = aesKey;
- }
-
- @Override
- public String getMsgDataFormat() {
- return this.msgDataFormat;
- }
-
- public void setMsgDataFormat(String msgDataFormat) {
- this.msgDataFormat = msgDataFormat;
- }
-
- @Override
- public String getHttpProxyHost() {
- return this.httpProxyHost;
- }
-
- public void setHttpProxyHost(String httpProxyHost) {
- this.httpProxyHost = httpProxyHost;
- }
-
- @Override
- public int getHttpProxyPort() {
- return this.httpProxyPort;
- }
-
- public void setHttpProxyPort(int httpProxyPort) {
- this.httpProxyPort = httpProxyPort;
- }
-
- @Override
- public String getHttpProxyUsername() {
- return this.httpProxyUsername;
- }
-
- public void setHttpProxyUsername(String httpProxyUsername) {
- this.httpProxyUsername = httpProxyUsername;
- }
-
- @Override
- public String getHttpProxyPassword() {
- return this.httpProxyPassword;
- }
-
- public void setHttpProxyPassword(String httpProxyPassword) {
- this.httpProxyPassword = httpProxyPassword;
- }
-
@Override
- public String toString() {
- return WxMaGsonBuilder.create().toJson(this);
- }
-
- @Override
- public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
- return this.apacheHttpClientBuilder;
- }
-
- public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
- this.apacheHttpClientBuilder = apacheHttpClientBuilder;
- }
-
- @Override
- public boolean autoRefreshToken() {
- return true;
- }
-
- @Override
- public String getAppid() {
- return appid;
- }
-
- public void setAppid(String appid) {
- this.appid = appid;
- }
-
- /**
- * 基于redis的简单分布式锁.
- */
- private class DistributedLock implements Lock {
-
- private JedisLock lock;
-
- private DistributedLock(String key) {
- this.lock = new JedisLock(getRedisKey(key));
- }
-
- @Override
- public void lock() {
- try (Jedis jedis = jedisPool.getResource()) {
- if (!lock.acquire(jedis)) {
- throw new RuntimeException("acquire timeouted");
- }
- } catch (InterruptedException e) {
- throw new RuntimeException("lock failed", e);
- }
- }
-
- @Override
- public void lockInterruptibly() throws InterruptedException {
- try (Jedis jedis = jedisPool.getResource()) {
- if (!lock.acquire(jedis)) {
- throw new RuntimeException("acquire timeouted");
- }
- }
- }
-
- @Override
- public boolean tryLock() {
- try (Jedis jedis = jedisPool.getResource()) {
- return lock.acquire(jedis);
- } catch (InterruptedException e) {
- throw new RuntimeException("lock failed", e);
- }
- }
-
- @Override
- public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
- try (Jedis jedis = jedisPool.getResource()) {
- return lock.acquire(jedis);
- }
- }
-
- @Override
- public void unlock() {
- try (Jedis jedis = jedisPool.getResource()) {
- lock.release(jedis);
- }
- }
-
- @Override
- public Condition newCondition() {
- throw new RuntimeException("unsupported method");
- }
-
+ protected Jedis getJedis() {
+ return jedisPool.getResource();
}
}
diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaRedisConnectionConfigImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaRedisConnectionConfigImpl.java
new file mode 100644
index 00000000..e48cda20
--- /dev/null
+++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaRedisConnectionConfigImpl.java
@@ -0,0 +1,24 @@
+package cn.binarywang.wx.miniapp.config.impl;
+
+import redis.clients.jedis.Jedis;
+
+/**
+ * 基于Redis的微信配置provider. 使用自己管理的 Jedis 实例进行 Redis 操作。
+ *
+ *
+ * 需要引入依赖jedis-lock,才能使用该类。
+ *
+ */
+public class WxMaRedisConnectionConfigImpl extends AbstractWxMaRedisConfig {
+
+ public WxMaRedisConnectionConfigImpl(Jedis jedis) {
+ this.jedis = jedis;
+ }
+
+ private Jedis jedis;
+
+ @Override
+ protected Jedis getJedis() {
+ return jedis;
+ }
+}