diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/CacheStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/CacheStorager.java index 0d19e5e5..04fee404 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/CacheStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/CacheStorager.java @@ -16,7 +16,7 @@ public interface CacheStorager { * 查找缓存中的对象 * * @param cacheKey - * 缓存名称 + * 缓存key * @return * @throws WeixinException */ @@ -26,11 +26,25 @@ public interface CacheStorager { * 缓存新的对象 * * @param cacheKey - * 缓存名称 + * 缓存key * * @param t * 将要缓存的对象 * @throws WeixinException */ void caching(String cacheKey, T t) throws WeixinException; + + /** + * 移除缓存对象 + * + * @param cacheKey + * 缓存key + * @return 移除的对象 + */ + T evict(String cacheKey); + + /** + * 清除所有缓存对象(请慎重) + */ + void clear(); } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/FileTokenStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/FileTokenStorager.java index ce46ed8e..7cd3835c 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/FileTokenStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/FileTokenStorager.java @@ -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(); + } + } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/MemoryTokenStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/MemoryTokenStorager.java index a80438aa..3c2b864f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/MemoryTokenStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/MemoryTokenStorager.java @@ -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(); + } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java index 82206488..dae39c75 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java @@ -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.... + } } \ No newline at end of file