CacheStorager新增移除、清除token两个接口方法
This commit is contained in:
parent
8c4d5a2798
commit
bca5816617
@ -16,7 +16,7 @@ public interface CacheStorager<T> {
|
|||||||
* 查找缓存中的对象
|
* 查找缓存中的对象
|
||||||
*
|
*
|
||||||
* @param cacheKey
|
* @param cacheKey
|
||||||
* 缓存名称
|
* 缓存key
|
||||||
* @return
|
* @return
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
@ -26,11 +26,25 @@ public interface CacheStorager<T> {
|
|||||||
* 缓存新的对象
|
* 缓存新的对象
|
||||||
*
|
*
|
||||||
* @param cacheKey
|
* @param cacheKey
|
||||||
* 缓存名称
|
* 缓存key
|
||||||
*
|
*
|
||||||
* @param t
|
* @param t
|
||||||
* 将要缓存的对象
|
* 将要缓存的对象
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
void caching(String cacheKey, T 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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
import com.foxinmy.weixin4j.util.FileUtil;
|
||||||
import com.foxinmy.weixin4j.xml.XmlStream;
|
import com.foxinmy.weixin4j.xml.XmlStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,4 +60,36 @@ public class FileTokenStorager implements TokenStorager {
|
|||||||
throw new WeixinException(e);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,4 +39,14 @@ public class MemoryTokenStorager implements TokenStorager {
|
|||||||
public void caching(String cacheKey, Token token) throws WeixinException {
|
public void caching(String cacheKey, Token token) throws WeixinException {
|
||||||
this.CONMAP.put(cacheKey, token);
|
this.CONMAP.put(cacheKey, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token evict(String cacheKey) {
|
||||||
|
return this.CONMAP.remove(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
this.CONMAP.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ public class RedisTokenStorager implements TokenStorager {
|
|||||||
|
|
||||||
private JedisPool jedisPool;
|
private JedisPool jedisPool;
|
||||||
|
|
||||||
|
public final static int PORT = 6379;
|
||||||
public final static int MAX_TOTAL = 50;
|
public final static int MAX_TOTAL = 50;
|
||||||
public final static int MAX_IDLE = 5;
|
public final static int MAX_IDLE = 5;
|
||||||
public final static int MAX_WAIT_MILLIS = 2000;
|
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 final static boolean TEST_ON_RETURN = true;
|
||||||
|
|
||||||
public RedisTokenStorager() {
|
public RedisTokenStorager() {
|
||||||
this("localhost", 6379);
|
this("localhost", PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RedisTokenStorager(String host, int port) {
|
public RedisTokenStorager(String host, int port) {
|
||||||
@ -100,4 +101,29 @@ public class RedisTokenStorager implements TokenStorager {
|
|||||||
token.setOriginalResult(map.get("originalResult"));
|
token.setOriginalResult(map.get("originalResult"));
|
||||||
return token;
|
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....
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user