Skip to content

🎨 公众号模块配置类增加Redisson的实现 #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions weixin-java-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package me.chanjar.weixin.mp.config.impl;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
* @author wuxingye
* @date 2020/6/12
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class WxMpRedissonConfigImpl extends WxMpDefaultConfigImpl {

private static final long serialVersionUID = -5139855123878455556L;
private static final String ACCESS_TOKEN_KEY_TPL = "%s:access_token:%s";
private static final String TICKET_KEY_TPL = "%s:ticket:key:%s:%s";
private static final String LOCK_KEY_TPL = "%s:lock:%s:";
private final WxRedisOps redisOps;
private final String keyPrefix;
private String accessTokenKey;
private String lockKey;

public WxMpRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) {
this(new RedissonWxRedisOps(redissonClient), keyPrefix);
}

public WxMpRedissonConfigImpl(@NonNull RedissonClient redissonClient) {
this(redissonClient, null);
}

private WxMpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) {
this.redisOps = redisOps;
this.keyPrefix = keyPrefix;
}

/**
* 每个公众号生成独有的存储key.
*/
@Override
public void setAppId(String appId) {
super.setAppId(appId);
this.accessTokenKey = String.format(ACCESS_TOKEN_KEY_TPL, this.keyPrefix, appId);
this.lockKey = String.format(LOCK_KEY_TPL, this.keyPrefix, appId);
accessTokenLock = this.redisOps.getLock(lockKey.concat("accessTokenLock"));
jsapiTicketLock = this.redisOps.getLock(lockKey.concat("jsapiTicketLock"));
sdkTicketLock = this.redisOps.getLock(lockKey.concat("sdkTicketLock"));
cardApiTicketLock = this.redisOps.getLock(lockKey.concat("cardApiTicketLock"));
}

private String getTicketRedisKey(TicketType type) {
return String.format(TICKET_KEY_TPL, this.keyPrefix, appId, type.getCode());
}

@Override
public String getAccessToken() {
return redisOps.getValue(this.accessTokenKey);
}

@Override
public boolean isAccessTokenExpired() {
Long expire = redisOps.getExpire(this.accessTokenKey);
return expire == null || expire < 2;
}

@Override
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
redisOps.setValue(this.accessTokenKey, accessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public void expireAccessToken() {
redisOps.expire(this.accessTokenKey, 0, TimeUnit.SECONDS);
}

@Override
public String getTicket(TicketType type) {
return redisOps.getValue(this.getTicketRedisKey(type));
}

@Override
public boolean isTicketExpired(TicketType type) {
return redisOps.getExpire(this.getTicketRedisKey(type)) < 2;
}

@Override
public synchronized void updateTicket(TicketType type, String jsapiTicket, int expiresInSeconds) {
redisOps.setValue(this.getTicketRedisKey(type), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public void expireTicket(TicketType type) {
redisOps.expire(this.getTicketRedisKey(type), 0, TimeUnit.SECONDS);
}
}