新增缓存临界值变量
This commit is contained in:
parent
b4a5f0916a
commit
12cbeca91d
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<String, Token> CONMAP;
|
||||
|
||||
public MemoryTokenStorager() {
|
||||
this.CONMAP = new ConcurrentHashMap<String, Token>();
|
||||
}
|
||||
|
||||
@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<String, Token> CONMAP;
|
||||
|
||||
public MemoryTokenStorager() {
|
||||
this.CONMAP = new ConcurrentHashMap<String, Token>();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<String, String> 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<String, String> token2map(Token token) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
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<String, String> 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<String> 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<String, String> 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<String, String> token2map(Token token) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
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<String, String> 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<String> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -15,4 +15,8 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @see RedisTokenStorager
|
||||
*/
|
||||
public interface TokenStorager extends CacheStorager<Token> {
|
||||
/**
|
||||
* 考虑到程序的临界值,实际有效时间应该减去下面这个数
|
||||
*/
|
||||
final long CUTMS = 1 * 60 * 1000l;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user