Skip to content

Commit 3b37f52

Browse files
wuxingyexingye.wu
and
xingye.wu
authored
#1620 🎨 公众号模块配置类增加Redisson的实现
Co-authored-by: xingye.wu <[email protected]>
1 parent 29b4dbd commit 3b37f52

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: weixin-java-mp/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
<groupId>org.projectlombok</groupId>
8181
<artifactId>lombok</artifactId>
8282
</dependency>
83+
<dependency>
84+
<groupId>org.redisson</groupId>
85+
<artifactId>redisson</artifactId>
86+
</dependency>
8387
</dependencies>
8488

8589
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package me.chanjar.weixin.mp.config.impl;
2+
3+
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.NonNull;
6+
import me.chanjar.weixin.common.enums.TicketType;
7+
import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
8+
import me.chanjar.weixin.common.redis.WxRedisOps;
9+
import org.redisson.api.RedissonClient;
10+
11+
import java.util.concurrent.TimeUnit;
12+
13+
/**
14+
* @author wuxingye
15+
* @date 2020/6/12
16+
*/
17+
@EqualsAndHashCode(callSuper = true)
18+
@Data
19+
public class WxMpRedissonConfigImpl extends WxMpDefaultConfigImpl {
20+
21+
private static final long serialVersionUID = -5139855123878455556L;
22+
private static final String ACCESS_TOKEN_KEY_TPL = "%s:access_token:%s";
23+
private static final String TICKET_KEY_TPL = "%s:ticket:key:%s:%s";
24+
private static final String LOCK_KEY_TPL = "%s:lock:%s:";
25+
private final WxRedisOps redisOps;
26+
private final String keyPrefix;
27+
private String accessTokenKey;
28+
private String lockKey;
29+
30+
public WxMpRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) {
31+
this(new RedissonWxRedisOps(redissonClient), keyPrefix);
32+
}
33+
34+
public WxMpRedissonConfigImpl(@NonNull RedissonClient redissonClient) {
35+
this(redissonClient, null);
36+
}
37+
38+
private WxMpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) {
39+
this.redisOps = redisOps;
40+
this.keyPrefix = keyPrefix;
41+
}
42+
43+
/**
44+
* 每个公众号生成独有的存储key.
45+
*/
46+
@Override
47+
public void setAppId(String appId) {
48+
super.setAppId(appId);
49+
this.accessTokenKey = String.format(ACCESS_TOKEN_KEY_TPL, this.keyPrefix, appId);
50+
this.lockKey = String.format(LOCK_KEY_TPL, this.keyPrefix, appId);
51+
accessTokenLock = this.redisOps.getLock(lockKey.concat("accessTokenLock"));
52+
jsapiTicketLock = this.redisOps.getLock(lockKey.concat("jsapiTicketLock"));
53+
sdkTicketLock = this.redisOps.getLock(lockKey.concat("sdkTicketLock"));
54+
cardApiTicketLock = this.redisOps.getLock(lockKey.concat("cardApiTicketLock"));
55+
}
56+
57+
private String getTicketRedisKey(TicketType type) {
58+
return String.format(TICKET_KEY_TPL, this.keyPrefix, appId, type.getCode());
59+
}
60+
61+
@Override
62+
public String getAccessToken() {
63+
return redisOps.getValue(this.accessTokenKey);
64+
}
65+
66+
@Override
67+
public boolean isAccessTokenExpired() {
68+
Long expire = redisOps.getExpire(this.accessTokenKey);
69+
return expire == null || expire < 2;
70+
}
71+
72+
@Override
73+
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
74+
redisOps.setValue(this.accessTokenKey, accessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
75+
}
76+
77+
@Override
78+
public void expireAccessToken() {
79+
redisOps.expire(this.accessTokenKey, 0, TimeUnit.SECONDS);
80+
}
81+
82+
@Override
83+
public String getTicket(TicketType type) {
84+
return redisOps.getValue(this.getTicketRedisKey(type));
85+
}
86+
87+
@Override
88+
public boolean isTicketExpired(TicketType type) {
89+
return redisOps.getExpire(this.getTicketRedisKey(type)) < 2;
90+
}
91+
92+
@Override
93+
public synchronized void updateTicket(TicketType type, String jsapiTicket, int expiresInSeconds) {
94+
redisOps.setValue(this.getTicketRedisKey(type), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
95+
}
96+
97+
@Override
98+
public void expireTicket(TicketType type) {
99+
redisOps.expire(this.getTicketRedisKey(type), 0, TimeUnit.SECONDS);
100+
}
101+
}

0 commit comments

Comments
 (0)