Token优化

This commit is contained in:
jinyu
2016-04-21 08:57:25 +08:00
parent 8902d8aef7
commit fa6b60af82
14 changed files with 435 additions and 433 deletions
@@ -0,0 +1,36 @@
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
/**
*
* @className: AbstractTokenCreator
* @author jinyu
* @date 2016年4月21日
* @since JDK 1.6
* @see
*/
public abstract class AbstractTokenCreator implements TokenCreator {
protected final WeixinRequestExecutor weixinExecutor;
public AbstractTokenCreator() {
this.weixinExecutor = new WeixinRequestExecutor();
}
/**
* 缓存key:附加weixin4j前缀
*
* @return
*/
public String getCacheKey() {
return String.format("weixin4j_%s", getCacheKey0());
}
/**
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
*
* @return
*/
public abstract String getCacheKey0();
}
@@ -29,17 +29,14 @@ public class FileTokenStorager implements TokenStorager {
@Override
public Token lookup(String cacheKey) throws WeixinException {
File token_file = new File(String.format("%s/%s.xml", cachePath,
cacheKey));
File token_file = new File(String.format("%s/%s.xml", cachePath, cacheKey));
try {
if (token_file.exists()) {
Token token = XmlStream.fromXML(
new FileInputStream(token_file), Token.class);
Token token = XmlStream.fromXML(new FileInputStream(token_file), Token.class);
if (token.getCreateTime() < 0) {
return token;
}
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
.currentTimeMillis()) {
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System.currentTimeMillis()) {
return token;
}
}
@@ -52,10 +49,7 @@ public class FileTokenStorager implements TokenStorager {
@Override
public void caching(String cacheKey, Token token) throws WeixinException {
try {
XmlStream.toXML(
token,
new FileOutputStream(new File(String.format("%s/%s.xml",
cachePath, cacheKey))));
XmlStream.toXML(token, new FileOutputStream(new File(String.format("%s/%s.xml", cachePath, cacheKey))));
} catch (IOException e) {
throw new WeixinException(e);
}
@@ -64,12 +58,10 @@ public class FileTokenStorager implements TokenStorager {
@Override
public Token evict(String cacheKey) throws WeixinException {
Token token = null;
File token_file = new File(String.format("%s/%s.xml", cachePath,
cacheKey));
File token_file = new File(String.format("%s/%s.xml", cachePath, cacheKey));
try {
if (token_file.exists()) {
token = XmlStream.fromXML(new FileInputStream(token_file),
Token.class);
token = XmlStream.fromXML(new FileInputStream(token_file), Token.class);
token_file.delete();
}
} catch (IOException e) {
@@ -83,9 +75,8 @@ public class FileTokenStorager implements TokenStorager {
File[] files = new File(cachePath).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile()
&& "xml".equals(FileUtil.getFileExtension(file
.getName()));
return file.isFile() && file.getName().startsWith(PREFIX)
&& "xml".equals(FileUtil.getFileExtension(file.getName()));
}
});
for (File token : files) {
@@ -45,8 +45,7 @@ public class RedisTokenStorager implements TokenStorager {
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
}
public RedisTokenStorager(String host, int port,
JedisPoolConfig jedisPoolConfig) {
public RedisTokenStorager(String host, int port, JedisPoolConfig jedisPoolConfig) {
this(new JedisPool(jedisPoolConfig, host, port));
}
@@ -78,8 +77,7 @@ public class RedisTokenStorager implements TokenStorager {
jedis = jedisPool.getResource();
jedis.hmset(cacheKey, token2map(token));
if (token.getExpiresIn() > 0) {
jedis.expire(cacheKey, token.getExpiresIn()
- (int) (CUTMS / 1000l));
jedis.expire(cacheKey, token.getExpiresIn() - (int) (CUTMS / 1000l));
}
} finally {
if (jedis != null) {
@@ -130,7 +128,7 @@ public class RedisTokenStorager implements TokenStorager {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Set<String> cacheKeys = jedis.keys("weixin4j_*");
Set<String> cacheKeys = jedis.keys(String.format("%s*", PREFIX));
if (!cacheKeys.isEmpty()) {
Pipeline pipeline = jedis.pipelined();
for (String cacheKey : cacheKeys) {
@@ -1,30 +1,30 @@
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
/**
* TOKEN创建者
*
* @className TokenCreator
* @author jy
* @date 2015年1月10日
* @since JDK 1.6
* @see
*/
public interface TokenCreator {
/**
* 返回缓存KEY的名称
*
* @return
*/
public String getCacheKey();
/**
* 创建token
*
* @return
* @throws WeixinException
*/
public Token createToken() throws WeixinException;
}
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
/**
* TOKEN创建者
*
* @className TokenCreator
* @author jy
* @date 2015年1月10日
* @since JDK 1.6
* @see
*/
public interface TokenCreator {
/**
* 返回缓存KEY的名称
*
* @return
*/
public String getCacheKey();
/**
* 创建token
*
* @return
* @throws WeixinException
*/
public Token createToken() throws WeixinException;
}
@@ -19,4 +19,8 @@ public interface TokenStorager extends CacheStorager<Token> {
* 考虑到程序的临界值,实际有效时间应该减去下面这个数
*/
final long CUTMS = 1 * 60 * 1000l;
/**
* 缓存key的前缀
*/
final String PREFIX = "weixin4j_";
}