token clean with prefix
This commit is contained in:
parent
2115c82330
commit
93d4ea5e25
@ -41,6 +41,9 @@ public interface CacheStorager<T> {
|
||||
|
||||
/**
|
||||
* 清除所有缓存对象(<font color="red">请慎重</font>)
|
||||
*
|
||||
* @param prefix
|
||||
* 缓存key的前缀
|
||||
*/
|
||||
void clear();
|
||||
void clear(String prefix);
|
||||
}
|
||||
|
||||
@ -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()));
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ public class MemcacheTokenStorager extends TokenStorager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
public void clear(String prefix) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ public class MemoryTokenStorager extends TokenStorager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
public void clear(String prefix) {
|
||||
this.CONMAP.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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_";
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user