Skip to content

Commit

Permalink
🐛 #1473 修复多个小程序获取redis里的access_token冲突问题
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Mar 26, 2020
1 parent faf07b3 commit 9bad0ff
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class WxMaRedisConfigImpl extends AbstractWxMaRedisConfig {

private JedisPool jedisPool;

private static final String ACCESS_TOKEN_KEY = "wa:access_token:";

private String accessTokenKey;

/**
* JedisPool 在此配置类是必须项,使用 WxMaRedisConfigImpl(JedisPool) 构造方法来构造实例
*/
Expand All @@ -37,4 +41,41 @@ public void setJedisPool(JedisPool jedisPool) {
protected Jedis getJedis() {
return jedisPool.getResource();
}

/**
* 每个公众号生成独有的存储key.
*/
@Override
public void setAppid(String appId) {
super.setAppid(appId);
this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId);
}

@Override
public String getAccessToken() {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.get(this.accessTokenKey);
}
}

@Override
public boolean isAccessTokenExpired() {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.ttl(accessTokenKey) < 2;
}
}

@Override
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.setex(this.accessTokenKey, expiresInSeconds - 200, accessToken);
}
}

@Override
public void expireAccessToken() {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.expire(this.accessTokenKey, 0);
}
}
}

0 comments on commit 9bad0ff

Please sign in to comment.