删除AbstractTokenCreator,引入CacheCreator<T>类
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
|
||||
/**
|
||||
* Cache的创建
|
||||
*
|
||||
* @className CacheCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年5月24日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public interface CacheCreator<T> {
|
||||
/**
|
||||
* CacheKey
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String key();
|
||||
|
||||
/**
|
||||
* 创建Cache
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public T create() throws WeixinException;
|
||||
}
|
||||
@@ -3,8 +3,8 @@ package com.foxinmy.weixin4j.token;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
|
||||
/**
|
||||
* cache存储
|
||||
*
|
||||
* Cache的存储
|
||||
*
|
||||
* @className CacheStorager
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年6月22日
|
||||
@@ -14,34 +14,34 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
public interface CacheStorager<T> {
|
||||
/**
|
||||
* 查找缓存中的对象
|
||||
*
|
||||
* @param cacheKey
|
||||
*
|
||||
* @param key
|
||||
* 缓存key
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
T lookup(String cacheKey) throws WeixinException;
|
||||
T lookup(String key) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 缓存新的对象
|
||||
*
|
||||
* @param cacheKey
|
||||
*
|
||||
* @param key
|
||||
* 缓存key
|
||||
*
|
||||
* @param t
|
||||
*
|
||||
* @param cache
|
||||
* 将要缓存的对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
void caching(String cacheKey, T t) throws WeixinException;
|
||||
void caching(String key, T cache) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 移除缓存对象
|
||||
*
|
||||
* @param cacheKey
|
||||
*
|
||||
* @param key
|
||||
* 缓存key
|
||||
* @return 移除的对象
|
||||
*/
|
||||
T evict(String cacheKey) throws WeixinException;
|
||||
T evict(String key) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 清除所有缓存对象(<font color="red">请慎重</font>)
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* TOKEN创建者
|
||||
*
|
||||
* Token的创建
|
||||
*
|
||||
* @className TokenCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public interface TokenCreator {
|
||||
/**
|
||||
* 返回缓存KEY的名称
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCacheKey();
|
||||
public abstract class TokenCreator implements CacheCreator<Token> {
|
||||
protected final WeixinRequestExecutor weixinExecutor;
|
||||
|
||||
public TokenCreator() {
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建token
|
||||
*
|
||||
* 缓存key:附加weixin4j_前缀
|
||||
*
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token createToken() throws WeixinException;
|
||||
@Override
|
||||
public String key() {
|
||||
return String.format("weixin4j_%s", key0());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String key0();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* 对token的缓存获取
|
||||
*
|
||||
*
|
||||
* @className TokenHolder
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年6月12日
|
||||
@@ -25,7 +25,7 @@ public class TokenHolder {
|
||||
private final TokenStorager tokenStorager;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param tokenCreator
|
||||
* token创建器
|
||||
* @param tokenStorager
|
||||
@@ -38,15 +38,15 @@ public class TokenHolder {
|
||||
|
||||
/**
|
||||
* 获取token对象
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token getToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
String cacheKey = tokenCreator.key();
|
||||
Token token = tokenStorager.lookup(cacheKey);
|
||||
if (token == null) {
|
||||
token = tokenCreator.createToken();
|
||||
token = tokenCreator.create();
|
||||
tokenStorager.caching(cacheKey, token);
|
||||
}
|
||||
return token;
|
||||
@@ -54,7 +54,7 @@ public class TokenHolder {
|
||||
|
||||
/**
|
||||
* 获取token字符串
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
@@ -64,25 +64,25 @@ public class TokenHolder {
|
||||
|
||||
/**
|
||||
* 手动刷新token
|
||||
*
|
||||
*
|
||||
* @return 刷新后的token
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token refreshToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
Token token = tokenCreator.createToken();
|
||||
String cacheKey = tokenCreator.key();
|
||||
Token token = tokenCreator.create();
|
||||
tokenStorager.caching(cacheKey, token);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动移除token
|
||||
*
|
||||
*
|
||||
* @return 被移除的token
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token evictToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
String cacheKey = tokenCreator.key();
|
||||
return tokenStorager.evict(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package com.foxinmy.weixin4j.token;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* token的存储
|
||||
*
|
||||
* Token的存储
|
||||
*
|
||||
* @className TokenStorager
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年9月27日
|
||||
|
||||
Reference in New Issue
Block a user