token clean with prefix

This commit is contained in:
jinyu 2016-05-25 15:37:47 +08:00
parent 2115c82330
commit 93d4ea5e25
8 changed files with 32 additions and 21 deletions

View File

@ -41,6 +41,9 @@ public interface CacheStorager<T> {
/**
* 清除所有缓存对象(<font color="red">请慎重</font>)
*
* @param prefix
* 缓存key的前缀
*/
void clear();
void clear(String prefix);
}

View File

@ -78,12 +78,12 @@ public class FileTokenStorager extends TokenStorager {
}
@Override
public void clear() {
public void clear(final String prefix) {
File[] files = new File(cachePath).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile()
&& file.getName().startsWith(prefix())
&& file.getName().startsWith(prefix)
&& "xml".equals(FileUtil.getFileExtension(file
.getName()));
}

View File

@ -50,7 +50,7 @@ public class MemcacheTokenStorager extends TokenStorager {
}
@Override
public void clear() {
public void clear(String prefix) {
throw new UnsupportedOperationException();
}

View File

@ -45,7 +45,7 @@ public class MemoryTokenStorager extends TokenStorager {
}
@Override
public void clear() {
public void clear(String prefix) {
this.CONMAP.clear();
}
}

View File

@ -126,11 +126,11 @@ public class RedisTokenStorager extends TokenStorager {
}
@Override
public void clear() {
public void clear(String prefix) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Set<String> cacheKeys = jedis.keys(String.format("%s*", prefix()));
Set<String> cacheKeys = jedis.keys(String.format("%s*", prefix));
if (!cacheKeys.isEmpty()) {
Pipeline pipeline = jedis.pipelined();
for (String cacheKey : cacheKeys) {

View File

@ -13,6 +13,7 @@ import com.foxinmy.weixin4j.model.Token;
* @see
*/
public abstract class TokenCreator implements CacheCreator<Token> {
protected final WeixinRequestExecutor weixinExecutor;
public TokenCreator() {
@ -20,13 +21,22 @@ public abstract class TokenCreator implements CacheCreator<Token> {
}
/**
* 缓存key:附加weixin4j_前缀
* 缓存key的前缀
*
* @return 默认为weixin4j_
*/
public String prefix() {
return "weixin4j_";
}
/**
* 缓存key:附加key前缀
*
* @return
*/
@Override
public String key() {
return String.format("weixin4j_%s", key0());
return String.format("%s%s", prefix(), key0());
}
/**

View File

@ -76,13 +76,20 @@ public class TokenHolder {
}
/**
* 手动移除token
* 移除token
*
* @return 被移除的token
* @throws WeixinException
*/
public Token evictToken() throws WeixinException {
public Token evictToken() {
String cacheKey = tokenCreator.key();
return tokenStorager.evict(cacheKey);
}
/**
* 清除所有的token(<font color="red">请慎重</font>)
*/
public void clearToken() {
String prefix = tokenCreator.prefix();
tokenStorager.clear(prefix);
}
}

View File

@ -24,13 +24,4 @@ public abstract class TokenStorager implements CacheStorager<Token> {
public long ms() {
return 60 * 1000l;
}
/**
* 缓存key的前缀
*
* @return 默认为weixin4j_
*/
public String prefix() {
return "weixin4j_";
}
}