新增缓存临界值变量
This commit is contained in:
parent
b4a5f0916a
commit
12cbeca91d
@ -1,95 +1,95 @@
|
|||||||
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.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.util.FileUtil;
|
||||||
import com.foxinmy.weixin4j.xml.XmlStream;
|
import com.foxinmy.weixin4j.xml.XmlStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用FILE保存TOKEN
|
* 用FILE保存TOKEN
|
||||||
*
|
*
|
||||||
* @className FileTokenStorager
|
* @className FileTokenStorager
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年1月9日
|
* @date 2015年1月9日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
*/
|
*/
|
||||||
public class FileTokenStorager implements TokenStorager {
|
public class FileTokenStorager implements TokenStorager {
|
||||||
|
|
||||||
private final String cachePath;
|
private final String cachePath;
|
||||||
|
|
||||||
public FileTokenStorager(String cachePath) {
|
public FileTokenStorager(String cachePath) {
|
||||||
this.cachePath = cachePath;
|
this.cachePath = cachePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token lookup(String cacheKey) throws WeixinException {
|
public Token lookup(String cacheKey) throws WeixinException {
|
||||||
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
||||||
cacheKey));
|
cacheKey));
|
||||||
try {
|
try {
|
||||||
if (token_file.exists()) {
|
if (token_file.exists()) {
|
||||||
Token token = XmlStream.fromXML(
|
Token token = XmlStream.fromXML(
|
||||||
new FileInputStream(token_file), Token.class);
|
new FileInputStream(token_file), Token.class);
|
||||||
if (token.getCreateTime() < 0) {
|
if (token.getCreateTime() < 0) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - 2) > System
|
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
|
||||||
.currentTimeMillis()) {
|
.currentTimeMillis()) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new WeixinException(e);
|
throw new WeixinException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void caching(String cacheKey, Token token) throws WeixinException {
|
public void caching(String cacheKey, Token token) throws WeixinException {
|
||||||
try {
|
try {
|
||||||
XmlStream.toXML(
|
XmlStream.toXML(
|
||||||
token,
|
token,
|
||||||
new FileOutputStream(new File(String.format("%s/%s.xml",
|
new FileOutputStream(new File(String.format("%s/%s.xml",
|
||||||
cachePath, cacheKey))));
|
cachePath, cacheKey))));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new WeixinException(e);
|
throw new WeixinException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token evict(String cacheKey) throws WeixinException {
|
public Token evict(String cacheKey) throws WeixinException {
|
||||||
Token token = null;
|
Token token = null;
|
||||||
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
||||||
cacheKey));
|
cacheKey));
|
||||||
try {
|
try {
|
||||||
if (token_file.exists()) {
|
if (token_file.exists()) {
|
||||||
token = XmlStream.fromXML(new FileInputStream(token_file),
|
token = XmlStream.fromXML(new FileInputStream(token_file),
|
||||||
Token.class);
|
Token.class);
|
||||||
token_file.delete();
|
token_file.delete();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
; // ingore
|
; // ingore
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() throws WeixinException {
|
public void clear() throws WeixinException {
|
||||||
File[] files = new File(cachePath).listFiles(new FileFilter() {
|
File[] files = new File(cachePath).listFiles(new FileFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File file) {
|
public boolean accept(File file) {
|
||||||
return file.isFile()
|
return file.isFile()
|
||||||
&& "xml".equals(FileUtil.getFileExtension(file
|
&& "xml".equals(FileUtil.getFileExtension(file
|
||||||
.getName()));
|
.getName()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (File token : files) {
|
for (File token : files) {
|
||||||
token.delete();
|
token.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,52 +1,52 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用内存保存TOKEN(不推荐使用)
|
* 用内存保存TOKEN(不推荐使用)
|
||||||
*
|
*
|
||||||
* @className MemoryTokenStorager
|
* @className MemoryTokenStorager
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2016年1月24日
|
* @date 2016年1月24日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see
|
* @see
|
||||||
*/
|
*/
|
||||||
public class MemoryTokenStorager implements TokenStorager {
|
public class MemoryTokenStorager implements TokenStorager {
|
||||||
|
|
||||||
private final Map<String, Token> CONMAP;
|
private final Map<String, Token> CONMAP;
|
||||||
|
|
||||||
public MemoryTokenStorager() {
|
public MemoryTokenStorager() {
|
||||||
this.CONMAP = new ConcurrentHashMap<String, Token>();
|
this.CONMAP = new ConcurrentHashMap<String, Token>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token lookup(String cacheKey) throws WeixinException {
|
public Token lookup(String cacheKey) throws WeixinException {
|
||||||
Token token = this.CONMAP.get(cacheKey);
|
Token token = this.CONMAP.get(cacheKey);
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - 2) > System
|
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
|
||||||
.currentTimeMillis()) {
|
.currentTimeMillis()) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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
|
@Override
|
||||||
public Token evict(String cacheKey) throws WeixinException {
|
public Token evict(String cacheKey) throws WeixinException {
|
||||||
return this.CONMAP.remove(cacheKey);
|
return this.CONMAP.remove(cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() throws WeixinException {
|
public void clear() throws WeixinException {
|
||||||
this.CONMAP.clear();
|
this.CONMAP.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,141 +1,141 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.JedisPoolConfig;
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
import redis.clients.jedis.Pipeline;
|
import redis.clients.jedis.Pipeline;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用REDIS保存TOKEN(推荐使用)
|
* 用REDIS保存TOKEN(推荐使用)
|
||||||
*
|
*
|
||||||
* @className RedisTokenStorager
|
* @className RedisTokenStorager
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年1月9日
|
* @date 2015年1月9日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
*/
|
*/
|
||||||
public class RedisTokenStorager implements TokenStorager {
|
public class RedisTokenStorager implements TokenStorager {
|
||||||
|
|
||||||
private JedisPool jedisPool;
|
private JedisPool jedisPool;
|
||||||
|
|
||||||
public final static int PORT = 6379;
|
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;
|
||||||
public final static boolean TEST_ON_BORROW = false;
|
public final static boolean TEST_ON_BORROW = false;
|
||||||
public final static boolean TEST_ON_RETURN = true;
|
public final static boolean TEST_ON_RETURN = true;
|
||||||
|
|
||||||
public RedisTokenStorager() {
|
public RedisTokenStorager() {
|
||||||
this("localhost", PORT);
|
this("localhost", PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RedisTokenStorager(String host, int port) {
|
public RedisTokenStorager(String host, int port) {
|
||||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||||
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
|
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
|
||||||
jedisPoolConfig.setMaxIdle(MAX_IDLE);
|
jedisPoolConfig.setMaxIdle(MAX_IDLE);
|
||||||
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
|
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
|
||||||
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
|
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
|
||||||
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN);
|
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN);
|
||||||
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RedisTokenStorager(String host, int port,
|
public RedisTokenStorager(String host, int port,
|
||||||
JedisPoolConfig jedisPoolConfig) {
|
JedisPoolConfig jedisPoolConfig) {
|
||||||
this(new JedisPool(jedisPoolConfig, host, port));
|
this(new JedisPool(jedisPoolConfig, host, port));
|
||||||
}
|
}
|
||||||
|
|
||||||
public RedisTokenStorager(JedisPool jedisPool) {
|
public RedisTokenStorager(JedisPool jedisPool) {
|
||||||
this.jedisPool = jedisPool;
|
this.jedisPool = jedisPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token lookup(String cacheKey) throws WeixinException {
|
public Token lookup(String cacheKey) throws WeixinException {
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
Map<String, String> map = jedis.hgetAll(cacheKey);
|
Map<String, String> map = jedis.hgetAll(cacheKey);
|
||||||
if (map != null && !map.isEmpty()) {
|
if (map != null && !map.isEmpty()) {
|
||||||
return map2token(map);
|
return map2token(map);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void caching(String cacheKey, Token token) throws WeixinException {
|
public void caching(String cacheKey, Token token) throws WeixinException {
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
jedis.hmset(cacheKey, token2map(token));
|
jedis.hmset(cacheKey, token2map(token));
|
||||||
if (token.getExpiresIn() > 0) {
|
if (token.getExpiresIn() > 0) {
|
||||||
jedis.expire(cacheKey, token.getExpiresIn());
|
jedis.expire(cacheKey, token.getExpiresIn() - (int)(CUTMS / 1000l));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<String, String> token2map(Token token) {
|
protected Map<String, String> token2map(Token token) {
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
map.put("accessToken", token.getAccessToken());
|
map.put("accessToken", token.getAccessToken());
|
||||||
map.put("originalResult", token.getOriginalResult());
|
map.put("originalResult", token.getOriginalResult());
|
||||||
map.put("createTime", Long.toString(token.getCreateTime()));
|
map.put("createTime", Long.toString(token.getCreateTime()));
|
||||||
map.put("expiresIn", Integer.toString(token.getExpiresIn()));
|
map.put("expiresIn", Integer.toString(token.getExpiresIn()));
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Token map2token(Map<String, String> map) {
|
protected Token map2token(Map<String, String> map) {
|
||||||
Token token = new Token(map.get("accessToken"));
|
Token token = new Token(map.get("accessToken"));
|
||||||
token.setCreateTime(Long.parseLong(map.get("createTime")));
|
token.setCreateTime(Long.parseLong(map.get("createTime")));
|
||||||
token.setExpiresIn(Integer.parseInt(map.get("expiresIn")));
|
token.setExpiresIn(Integer.parseInt(map.get("expiresIn")));
|
||||||
token.setOriginalResult(map.get("originalResult"));
|
token.setOriginalResult(map.get("originalResult"));
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token evict(String cacheKey) throws WeixinException {
|
public Token evict(String cacheKey) throws WeixinException {
|
||||||
Token token = lookup(cacheKey);
|
Token token = lookup(cacheKey);
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
jedis.del(cacheKey);
|
jedis.del(cacheKey);
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() throws WeixinException {
|
public void clear() throws WeixinException {
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
Set<String> cacheKeys = jedis.keys("weixin4j_*");
|
Set<String> cacheKeys = jedis.keys("weixin4j_*");
|
||||||
if (!cacheKeys.isEmpty()) {
|
if (!cacheKeys.isEmpty()) {
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
for (String cacheKey : cacheKeys) {
|
for (String cacheKey : cacheKeys) {
|
||||||
pipeline.del(cacheKey);
|
pipeline.del(cacheKey);
|
||||||
}
|
}
|
||||||
pipeline.sync();
|
pipeline.sync();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -15,4 +15,8 @@ import com.foxinmy.weixin4j.model.Token;
|
|||||||
* @see RedisTokenStorager
|
* @see RedisTokenStorager
|
||||||
*/
|
*/
|
||||||
public interface TokenStorager extends CacheStorager<Token> {
|
public interface TokenStorager extends CacheStorager<Token> {
|
||||||
|
/**
|
||||||
|
* 考虑到程序的临界值,实际有效时间应该减去下面这个数
|
||||||
|
*/
|
||||||
|
final long CUTMS = 1 * 60 * 1000l;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user