CacheStorager新增移除、清除token两个接口方法

This commit is contained in:
jinyu 2016-01-24 13:19:38 +08:00
parent 8c4d5a2798
commit bca5816617
4 changed files with 87 additions and 3 deletions

View File

@ -16,7 +16,7 @@ public interface CacheStorager<T> {
* 查找缓存中的对象
*
* @param cacheKey
* 缓存名称
* 缓存key
* @return
* @throws WeixinException
*/
@ -26,11 +26,25 @@ public interface CacheStorager<T> {
* 缓存新的对象
*
* @param cacheKey
* 缓存名称
* 缓存key
*
* @param t
* 将要缓存的对象
* @throws WeixinException
*/
void caching(String cacheKey, T t) throws WeixinException;
/**
* 移除缓存对象
*
* @param cacheKey
* 缓存key
* @return 移除的对象
*/
T evict(String cacheKey);
/**
* 清除所有缓存对象(<font color="red">请慎重</a>)
*/
void clear();
}

View File

@ -1,12 +1,14 @@
package com.foxinmy.weixin4j.token;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.util.FileUtil;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
@ -58,4 +60,36 @@ public class FileTokenStorager implements TokenStorager {
throw new WeixinException(e);
}
}
@Override
public Token evict(String cacheKey) {
Token token = null;
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_file.delete();
}
} catch (IOException e) {
; // ingore
}
return token;
}
@Override
public void clear() {
File[] files = new File(cachePath).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile()
&& "xml".equals(FileUtil.getFileExtension(file
.getName()));
}
});
for (File token : files) {
token.delete();
}
}
}

View File

@ -39,4 +39,14 @@ public class MemoryTokenStorager implements TokenStorager {
public void caching(String cacheKey, Token token) throws WeixinException {
this.CONMAP.put(cacheKey, token);
}
@Override
public Token evict(String cacheKey) {
return this.CONMAP.remove(cacheKey);
}
@Override
public void clear() {
this.CONMAP.clear();
}
}

View File

@ -22,6 +22,7 @@ public class RedisTokenStorager implements TokenStorager {
private JedisPool jedisPool;
public final static int PORT = 6379;
public final static int MAX_TOTAL = 50;
public final static int MAX_IDLE = 5;
public final static int MAX_WAIT_MILLIS = 2000;
@ -29,7 +30,7 @@ public class RedisTokenStorager implements TokenStorager {
public final static boolean TEST_ON_RETURN = true;
public RedisTokenStorager() {
this("localhost", 6379);
this("localhost", PORT);
}
public RedisTokenStorager(String host, int port) {
@ -100,4 +101,29 @@ public class RedisTokenStorager implements TokenStorager {
token.setOriginalResult(map.get("originalResult"));
return token;
}
@Override
public Token evict(String cacheKey) {
Token token = null;
try {
token = lookup(cacheKey);
} catch (WeixinException e) {
; // never
}
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(cacheKey);
} finally {
if (jedis != null) {
jedis.close();
}
}
return token;
}
@Override
public void clear() {
// en....
}
}