From 12cbeca91d4de388177473cd28f3d56e4158659d Mon Sep 17 00:00:00 2001 From: jinyu Date: Tue, 5 Apr 2016 14:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=BC=93=E5=AD=98=E4=B8=B4?= =?UTF-8?q?=E7=95=8C=E5=80=BC=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin4j/token/FileTokenStorager.java | 190 ++++++------ .../weixin4j/token/MemoryTokenStorager.java | 104 +++---- .../weixin4j/token/RedisTokenStorager.java | 280 +++++++++--------- .../foxinmy/weixin4j/token/TokenStorager.java | 4 + 4 files changed, 291 insertions(+), 287 deletions(-) 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 0498e503..e58d11af 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,95 +1,95 @@ -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; - -/** - * 用FILE保存TOKEN - * - * @className FileTokenStorager - * @author jy - * @date 2015年1月9日 - * @since JDK 1.6 - */ -public class FileTokenStorager implements TokenStorager { - - private final String cachePath; - - public FileTokenStorager(String cachePath) { - this.cachePath = cachePath; - } - - @Override - public Token lookup(String cacheKey) throws WeixinException { - File token_file = new File(String.format("%s/%s.xml", cachePath, - cacheKey)); - try { - if (token_file.exists()) { - Token token = XmlStream.fromXML( - new FileInputStream(token_file), Token.class); - if (token.getCreateTime() < 0) { - return token; - } - if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - 2) > System - .currentTimeMillis()) { - return token; - } - } - return null; - } catch (IOException e) { - throw new WeixinException(e); - } - } - - @Override - public void caching(String cacheKey, Token token) throws WeixinException { - try { - XmlStream.toXML( - token, - new FileOutputStream(new File(String.format("%s/%s.xml", - cachePath, cacheKey)))); - } catch (IOException e) { - throw new WeixinException(e); - } - } - - @Override - public Token evict(String cacheKey) throws WeixinException { - 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() throws WeixinException { - 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(); - } - } -} +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; + +/** + * 用FILE保存TOKEN + * + * @className FileTokenStorager + * @author jy + * @date 2015年1月9日 + * @since JDK 1.6 + */ +public class FileTokenStorager implements TokenStorager { + + private final String cachePath; + + public FileTokenStorager(String cachePath) { + this.cachePath = cachePath; + } + + @Override + public Token lookup(String cacheKey) throws WeixinException { + File token_file = new File(String.format("%s/%s.xml", cachePath, + cacheKey)); + try { + if (token_file.exists()) { + Token token = XmlStream.fromXML( + new FileInputStream(token_file), Token.class); + if (token.getCreateTime() < 0) { + return token; + } + if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System + .currentTimeMillis()) { + return token; + } + } + return null; + } catch (IOException e) { + throw new WeixinException(e); + } + } + + @Override + public void caching(String cacheKey, Token token) throws WeixinException { + try { + XmlStream.toXML( + token, + new FileOutputStream(new File(String.format("%s/%s.xml", + cachePath, cacheKey)))); + } catch (IOException e) { + throw new WeixinException(e); + } + } + + @Override + public Token evict(String cacheKey) throws WeixinException { + 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() throws WeixinException { + 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 9173ceb7..3436bde3 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 @@ -1,52 +1,52 @@ -package com.foxinmy.weixin4j.token; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import com.foxinmy.weixin4j.exception.WeixinException; -import com.foxinmy.weixin4j.model.Token; - -/** - * 用内存保存TOKEN(不推荐使用) - * - * @className MemoryTokenStorager - * @author jy - * @date 2016年1月24日 - * @since JDK 1.6 - * @see - */ -public class MemoryTokenStorager implements TokenStorager { - - private final Map CONMAP; - - public MemoryTokenStorager() { - this.CONMAP = new ConcurrentHashMap(); - } - - @Override - public Token lookup(String cacheKey) throws WeixinException { - Token token = this.CONMAP.get(cacheKey); - if (token != null) { - if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - 2) > System - .currentTimeMillis()) { - return token; - } - } - return null; - } - - @Override - public void caching(String cacheKey, Token token) throws WeixinException { - this.CONMAP.put(cacheKey, token); - } - - @Override - public Token evict(String cacheKey) throws WeixinException { - return this.CONMAP.remove(cacheKey); - } - - @Override - public void clear() throws WeixinException { - this.CONMAP.clear(); - } -} +package com.foxinmy.weixin4j.token; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import com.foxinmy.weixin4j.exception.WeixinException; +import com.foxinmy.weixin4j.model.Token; + +/** + * 用内存保存TOKEN(不推荐使用) + * + * @className MemoryTokenStorager + * @author jy + * @date 2016年1月24日 + * @since JDK 1.6 + * @see + */ +public class MemoryTokenStorager implements TokenStorager { + + private final Map CONMAP; + + public MemoryTokenStorager() { + this.CONMAP = new ConcurrentHashMap(); + } + + @Override + public Token lookup(String cacheKey) throws WeixinException { + Token token = this.CONMAP.get(cacheKey); + if (token != null) { + if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System + .currentTimeMillis()) { + return token; + } + } + return null; + } + + @Override + public void caching(String cacheKey, Token token) throws WeixinException { + this.CONMAP.put(cacheKey, token); + } + + @Override + public Token evict(String cacheKey) throws WeixinException { + return this.CONMAP.remove(cacheKey); + } + + @Override + public void clear() throws WeixinException { + 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 dd0ece12..659855bd 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 @@ -1,141 +1,141 @@ -package com.foxinmy.weixin4j.token; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; -import redis.clients.jedis.Pipeline; - -import com.foxinmy.weixin4j.exception.WeixinException; -import com.foxinmy.weixin4j.model.Token; - -/** - * 用REDIS保存TOKEN(推荐使用) - * - * @className RedisTokenStorager - * @author jy - * @date 2015年1月9日 - * @since JDK 1.6 - */ -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; - public final static boolean TEST_ON_BORROW = false; - public final static boolean TEST_ON_RETURN = true; - - public RedisTokenStorager() { - this("localhost", PORT); - } - - public RedisTokenStorager(String host, int port) { - JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); - jedisPoolConfig.setMaxTotal(MAX_TOTAL); - jedisPoolConfig.setMaxIdle(MAX_IDLE); - jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); - jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); - jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN); - this.jedisPool = new JedisPool(jedisPoolConfig, host, port); - } - - public RedisTokenStorager(String host, int port, - JedisPoolConfig jedisPoolConfig) { - this(new JedisPool(jedisPoolConfig, host, port)); - } - - public RedisTokenStorager(JedisPool jedisPool) { - this.jedisPool = jedisPool; - } - - @Override - public Token lookup(String cacheKey) throws WeixinException { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - Map map = jedis.hgetAll(cacheKey); - if (map != null && !map.isEmpty()) { - return map2token(map); - } - } finally { - if (jedis != null) { - jedis.close(); - } - } - return null; - } - - @Override - public void caching(String cacheKey, Token token) throws WeixinException { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - jedis.hmset(cacheKey, token2map(token)); - if (token.getExpiresIn() > 0) { - jedis.expire(cacheKey, token.getExpiresIn()); - } - } finally { - if (jedis != null) { - jedis.close(); - } - } - } - - protected Map token2map(Token token) { - Map map = new HashMap(); - map.put("accessToken", token.getAccessToken()); - map.put("originalResult", token.getOriginalResult()); - map.put("createTime", Long.toString(token.getCreateTime())); - map.put("expiresIn", Integer.toString(token.getExpiresIn())); - return map; - } - - protected Token map2token(Map map) { - Token token = new Token(map.get("accessToken")); - token.setCreateTime(Long.parseLong(map.get("createTime"))); - token.setExpiresIn(Integer.parseInt(map.get("expiresIn"))); - token.setOriginalResult(map.get("originalResult")); - return token; - } - - @Override - public Token evict(String cacheKey) throws WeixinException { - Token token = lookup(cacheKey); - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - jedis.del(cacheKey); - } finally { - if (jedis != null) { - jedis.close(); - } - } - return token; - } - - @Override - public void clear() throws WeixinException { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - Set cacheKeys = jedis.keys("weixin4j_*"); - if (!cacheKeys.isEmpty()) { - Pipeline pipeline = jedis.pipelined(); - for (String cacheKey : cacheKeys) { - pipeline.del(cacheKey); - } - pipeline.sync(); - } - } finally { - if (jedis != null) { - jedis.close(); - } - } - } +package com.foxinmy.weixin4j.token; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.Pipeline; + +import com.foxinmy.weixin4j.exception.WeixinException; +import com.foxinmy.weixin4j.model.Token; + +/** + * 用REDIS保存TOKEN(推荐使用) + * + * @className RedisTokenStorager + * @author jy + * @date 2015年1月9日 + * @since JDK 1.6 + */ +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; + public final static boolean TEST_ON_BORROW = false; + public final static boolean TEST_ON_RETURN = true; + + public RedisTokenStorager() { + this("localhost", PORT); + } + + public RedisTokenStorager(String host, int port) { + JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); + jedisPoolConfig.setMaxTotal(MAX_TOTAL); + jedisPoolConfig.setMaxIdle(MAX_IDLE); + jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); + jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); + jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN); + this.jedisPool = new JedisPool(jedisPoolConfig, host, port); + } + + public RedisTokenStorager(String host, int port, + JedisPoolConfig jedisPoolConfig) { + this(new JedisPool(jedisPoolConfig, host, port)); + } + + public RedisTokenStorager(JedisPool jedisPool) { + this.jedisPool = jedisPool; + } + + @Override + public Token lookup(String cacheKey) throws WeixinException { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + Map map = jedis.hgetAll(cacheKey); + if (map != null && !map.isEmpty()) { + return map2token(map); + } + } finally { + if (jedis != null) { + jedis.close(); + } + } + return null; + } + + @Override + public void caching(String cacheKey, Token token) throws WeixinException { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + jedis.hmset(cacheKey, token2map(token)); + if (token.getExpiresIn() > 0) { + jedis.expire(cacheKey, token.getExpiresIn() - (int)(CUTMS / 1000l)); + } + } finally { + if (jedis != null) { + jedis.close(); + } + } + } + + protected Map token2map(Token token) { + Map map = new HashMap(); + map.put("accessToken", token.getAccessToken()); + map.put("originalResult", token.getOriginalResult()); + map.put("createTime", Long.toString(token.getCreateTime())); + map.put("expiresIn", Integer.toString(token.getExpiresIn())); + return map; + } + + protected Token map2token(Map map) { + Token token = new Token(map.get("accessToken")); + token.setCreateTime(Long.parseLong(map.get("createTime"))); + token.setExpiresIn(Integer.parseInt(map.get("expiresIn"))); + token.setOriginalResult(map.get("originalResult")); + return token; + } + + @Override + public Token evict(String cacheKey) throws WeixinException { + Token token = lookup(cacheKey); + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + jedis.del(cacheKey); + } finally { + if (jedis != null) { + jedis.close(); + } + } + return token; + } + + @Override + public void clear() throws WeixinException { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + Set cacheKeys = jedis.keys("weixin4j_*"); + if (!cacheKeys.isEmpty()) { + Pipeline pipeline = jedis.pipelined(); + for (String cacheKey : cacheKeys) { + pipeline.del(cacheKey); + } + pipeline.sync(); + } + } finally { + if (jedis != null) { + jedis.close(); + } + } + } } \ No newline at end of file diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/TokenStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/TokenStorager.java index fede1d94..df8f6b92 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/TokenStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/TokenStorager.java @@ -15,4 +15,8 @@ import com.foxinmy.weixin4j.model.Token; * @see RedisTokenStorager */ public interface TokenStorager extends CacheStorager { + /** + * 考虑到程序的临界值,实际有效时间应该减去下面这个数 + */ + final long CUTMS = 1 * 60 * 1000l; }