|
| 1 | +package cn.binarywang.wx.miniapp.config.impl; |
| 2 | + |
| 3 | +import lombok.NonNull; |
| 4 | +import me.chanjar.weixin.common.bean.WxAccessToken; |
| 5 | +import me.chanjar.weixin.common.redis.RedissonWxRedisOps; |
| 6 | +import me.chanjar.weixin.common.redis.WxRedisOps; |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | +import org.redisson.api.RedissonClient; |
| 9 | + |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.locks.Lock; |
| 12 | + |
| 13 | +/** |
| 14 | + * 基于Redisson的实现 |
| 15 | + * |
| 16 | + * @author yuanqixun |
| 17 | + * @date 2020/5/3 |
| 18 | + */ |
| 19 | +public class WxMaRedissonConfigImpl extends WxMaDefaultConfigImpl { |
| 20 | + |
| 21 | + protected final static String LOCK_KEY = "wechat_ma_lock:"; |
| 22 | + protected final static String MA_ACCESS_TOKEN_KEY = "wechat_ma_access_token_key:"; |
| 23 | + protected final static String MA_JSAPI_TICKET_KEY = "wechat_ma_jsapi_ticket_key:"; |
| 24 | + protected final static String MA_CARD_API_TICKET_KEY = "wechat_ma_card_api_ticket_key:"; |
| 25 | + |
| 26 | + /** |
| 27 | + * redis 存储的 key 的前缀,可为空 |
| 28 | + */ |
| 29 | + /** |
| 30 | + * redis 存储的 key 的前缀,可为空 |
| 31 | + */ |
| 32 | + protected String keyPrefix; |
| 33 | + protected String accessTokenKey; |
| 34 | + protected String jsapiTicketKey; |
| 35 | + protected String cardApiTicketKey; |
| 36 | + protected String lockKey; |
| 37 | + |
| 38 | + private final WxRedisOps redisOps; |
| 39 | + |
| 40 | + public WxMaRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) { |
| 41 | + this(new RedissonWxRedisOps(redissonClient), keyPrefix); |
| 42 | + } |
| 43 | + |
| 44 | + public WxMaRedissonConfigImpl(@NonNull RedissonClient redissonClient) { |
| 45 | + this(redissonClient, null); |
| 46 | + } |
| 47 | + |
| 48 | + private WxMaRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) { |
| 49 | + this.redisOps = redisOps; |
| 50 | + this.keyPrefix = keyPrefix; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setAppid(String appid) { |
| 55 | + super.setAppid(appid); |
| 56 | + String prefix = StringUtils.isBlank(keyPrefix) ? "" : |
| 57 | + (StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":")); |
| 58 | + lockKey = prefix + LOCK_KEY.concat(appid); |
| 59 | + accessTokenKey = prefix + MA_ACCESS_TOKEN_KEY.concat(appid); |
| 60 | + jsapiTicketKey = prefix + MA_JSAPI_TICKET_KEY.concat(appid); |
| 61 | + cardApiTicketKey = prefix + MA_CARD_API_TICKET_KEY.concat(appid); |
| 62 | + } |
| 63 | + |
| 64 | + protected Lock getLockByKey(String key) { |
| 65 | + return redisOps.getLock(key); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Lock getAccessTokenLock() { |
| 70 | + return getLockByKey(this.lockKey.concat(":").concat("accessToken")); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Lock getCardApiTicketLock() { |
| 75 | + return getLockByKey(this.lockKey.concat(":").concat("cardApiTicket")); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Lock getJsapiTicketLock() { |
| 81 | + return getLockByKey(this.lockKey.concat(":").concat("jsapiTicket")); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String getAccessToken() { |
| 86 | + return redisOps.getValue(this.accessTokenKey); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean isAccessTokenExpired() { |
| 91 | + Long expire = redisOps.getExpire(this.accessTokenKey); |
| 92 | + return expire == null || expire < 2; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void updateAccessToken(WxAccessToken accessToken) { |
| 97 | + redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void updateAccessToken(String accessToken, int expiresInSeconds) { |
| 102 | + redisOps.setValue(this.accessTokenKey, accessToken, expiresInSeconds, TimeUnit.SECONDS); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getJsapiTicket() { |
| 107 | + return redisOps.getValue(this.jsapiTicketKey); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean isJsapiTicketExpired() { |
| 112 | + Long expire = redisOps.getExpire(this.jsapiTicketKey); |
| 113 | + return expire == null || expire < 2; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void expireJsapiTicket() { |
| 118 | + redisOps.expire(this.jsapiTicketKey, 0, TimeUnit.SECONDS); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { |
| 123 | + redisOps.setValue(this.jsapiTicketKey, jsapiTicket, expiresInSeconds, TimeUnit.SECONDS); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String getCardApiTicket() { |
| 129 | + return redisOps.getValue(cardApiTicketKey); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean isCardApiTicketExpired() { |
| 134 | + Long expire = redisOps.getExpire(this.cardApiTicketKey); |
| 135 | + return expire == null || expire < 2; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void expireCardApiTicket() { |
| 140 | + redisOps.expire(this.cardApiTicketKey, 0, TimeUnit.SECONDS); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) { |
| 145 | + redisOps.setValue(this.cardApiTicketKey, cardApiTicket, expiresInSeconds, TimeUnit.SECONDS); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void expireAccessToken() { |
| 150 | + redisOps.expire(this.accessTokenKey, 0, TimeUnit.SECONDS); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments