Merge branch 'master' of git@github.com:foxinmy/weixin4j.git

This commit is contained in:
jinyu 2016-07-07 15:28:24 +08:00
commit 8c1e1719fa
8 changed files with 52 additions and 52 deletions

View File

@ -21,16 +21,16 @@ public class FileCacheStorager<T extends Cacheable> implements CacheStorager<T>
private final File tmpdir; private final File tmpdir;
private final String SEPARATOR = File.separator; private final String SEPARATOR = File.separator;
public FileCacheStorager(String cachePath) { public FileCacheStorager(String path) {
this.tmpdir = new File(String.format("%s%sweixin4j_token_temp", this.tmpdir = new File(String.format("%s%sweixin4j_token_temp",
cachePath, SEPARATOR)); path, SEPARATOR));
this.tmpdir.mkdirs(); this.tmpdir.mkdirs();
} }
@Override @Override
public T lookup(String cacheKey) { public T lookup(String key) {
File cacheFile = new File(String.format("%s%s%s", File cacheFile = new File(String.format("%s%s%s",
tmpdir.getAbsolutePath(), SEPARATOR, cacheKey)); tmpdir.getAbsolutePath(), SEPARATOR, key));
try { try {
if (cacheFile.exists()) { if (cacheFile.exists()) {
T cache = SerializationUtils.deserialize(new FileInputStream( T cache = SerializationUtils.deserialize(new FileInputStream(
@ -50,22 +50,22 @@ public class FileCacheStorager<T extends Cacheable> implements CacheStorager<T>
} }
@Override @Override
public void caching(String cacheKey, T cache) { public void caching(String key, T cache) {
try { try {
SerializationUtils.serialize( SerializationUtils.serialize(
cache, cache,
new FileOutputStream(new File(String.format("%s%s%s", new FileOutputStream(new File(String.format("%s%s%s",
tmpdir.getAbsolutePath(), SEPARATOR, cacheKey)))); tmpdir.getAbsolutePath(), SEPARATOR, key))));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public T evict(String cacheKey) { public T evict(String key) {
T cache = null; T cache = null;
File cacheFile = new File(String.format("%s%s%s", File cacheFile = new File(String.format("%s%s%s",
tmpdir.getAbsolutePath(), SEPARATOR, cacheKey)); tmpdir.getAbsolutePath(), SEPARATOR, key));
try { try {
if (cacheFile.exists()) { if (cacheFile.exists()) {
cache = SerializationUtils.deserialize(new FileInputStream( cache = SerializationUtils.deserialize(new FileInputStream(

View File

@ -35,32 +35,32 @@ public class MemcacheCacheStorager<T extends Cacheable> implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public T lookup(String cacheKey) { public T lookup(String key) {
return (T) mc.get(cacheKey); return (T) mc.get(key);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void caching(String cacheKey, T cache) { public void caching(String key, T cache) {
if (cache.getCreateTime() > 0l) { if (cache.getCreateTime() > 0l) {
mc.set(cacheKey, mc.set(key,
cache, cache,
new Date(cache.getCreateTime() + cache.getExpires() - CUTMS)); new Date(cache.getCreateTime() + cache.getExpires() - CUTMS));
} else { } else {
mc.set(cacheKey, cache); mc.set(key, cache);
} }
Set<String> all = (Set<String>) mc.get(ALLKEY); Set<String> all = (Set<String>) mc.get(ALLKEY);
all.add(cacheKey); all.add(key);
mc.set(ALLKEY, all); mc.set(ALLKEY, all);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public T evict(String cacheKey) { public T evict(String key) {
T cache = lookup(cacheKey); T cache = lookup(key);
mc.delete(cacheKey); mc.delete(key);
Set<String> all = (Set<String>) mc.get(ALLKEY); Set<String> all = (Set<String>) mc.get(ALLKEY);
all.remove(cacheKey); all.remove(key);
mc.set(ALLKEY, all); mc.set(ALLKEY, all);
return cache; return cache;
} }

View File

@ -22,8 +22,8 @@ public class MemoryCacheStorager<T extends Cacheable> implements
} }
@Override @Override
public T lookup(String cacheKey) { public T lookup(String key) {
T cache = this.CONMAP.get(cacheKey); T cache = this.CONMAP.get(key);
if (cache != null) { if (cache != null) {
if ((cache.getCreateTime() + cache.getExpires() - CUTMS) > System if ((cache.getCreateTime() + cache.getExpires() - CUTMS) > System
.currentTimeMillis()) { .currentTimeMillis()) {
@ -34,13 +34,13 @@ public class MemoryCacheStorager<T extends Cacheable> implements
} }
@Override @Override
public void caching(String cacheKey, T cache) { public void caching(String key, T cache) {
this.CONMAP.put(cacheKey, cache); this.CONMAP.put(key, cache);
} }
@Override @Override
public T evict(String cacheKey) { public T evict(String key) {
return this.CONMAP.remove(cacheKey); return this.CONMAP.remove(key);
} }
@Override @Override

View File

@ -60,11 +60,11 @@ public class RedisCacheStorager<T extends Cacheable> implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public T lookup(String cacheKey) { public T lookup(String key) {
Jedis jedis = null; Jedis jedis = null;
try { try {
jedis = jedisPool.getResource(); jedis = jedisPool.getResource();
byte[] value = jedis.get(cacheKey.getBytes(Consts.UTF_8)); byte[] value = jedis.get(key.getBytes(Consts.UTF_8));
return value != null ? (T) SerializationUtils.deserialize(value) return value != null ? (T) SerializationUtils.deserialize(value)
: null; : null;
} finally { } finally {
@ -75,19 +75,19 @@ public class RedisCacheStorager<T extends Cacheable> implements
} }
@Override @Override
public void caching(String cacheKey, T cache) { public void caching(String key, T cache) {
Jedis jedis = null; Jedis jedis = null;
try { try {
jedis = jedisPool.getResource(); jedis = jedisPool.getResource();
byte[] key = cacheKey.getBytes(Consts.UTF_8); byte[] cacheKey = key.getBytes(Consts.UTF_8);
byte[] value = SerializationUtils.serialize(cache); byte[] value = SerializationUtils.serialize(cache);
if (cache.getExpires() > 0) { if (cache.getExpires() > 0) {
jedis.setex(key, (int) (cache.getExpires() - CUTMS) / 1000, jedis.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000,
value); value);
} else { } else {
jedis.set(key, value); jedis.set(cacheKey, value);
} }
jedis.sadd(ALLKEY, cacheKey); jedis.sadd(ALLKEY, key);
} finally { } finally {
if (jedis != null) { if (jedis != null) {
jedis.close(); jedis.close();
@ -96,13 +96,13 @@ public class RedisCacheStorager<T extends Cacheable> implements
} }
@Override @Override
public T evict(String cacheKey) { public T evict(String key) {
T cache = lookup(cacheKey); T cache = lookup(key);
Jedis jedis = null; Jedis jedis = null;
try { try {
jedis = jedisPool.getResource(); jedis = jedisPool.getResource();
jedis.del(cacheKey); jedis.del(key);
jedis.srem(ALLKEY, cacheKey); jedis.srem(ALLKEY, key);
} finally { } finally {
if (jedis != null) { if (jedis != null) {
jedis.close(); jedis.close();

View File

@ -59,29 +59,29 @@ public class RedisClusterCacheStorager<T extends Cacheable> implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public T lookup(String cacheKey) { public T lookup(String key) {
byte[] value = jedisCluster.get(cacheKey.getBytes(Consts.UTF_8)); byte[] value = jedisCluster.get(key.getBytes(Consts.UTF_8));
return value != null ? (T) SerializationUtils.deserialize(value) : null; return value != null ? (T) SerializationUtils.deserialize(value) : null;
} }
@Override @Override
public void caching(String cacheKey, T cache) { public void caching(String key, T cache) {
byte[] key = cacheKey.getBytes(Consts.UTF_8); byte[] cacheKey = key.getBytes(Consts.UTF_8);
byte[] value = SerializationUtils.serialize(cache); byte[] value = SerializationUtils.serialize(cache);
if (cache.getExpires() > 0) { if (cache.getExpires() > 0) {
jedisCluster.setex(key, (int) (cache.getExpires() - CUTMS) / 1000, jedisCluster.setex(cacheKey,
value); (int) (cache.getExpires() - CUTMS) / 1000, value);
} else { } else {
jedisCluster.set(key, value); jedisCluster.set(cacheKey, value);
} }
jedisCluster.sadd(ALLKEY, cacheKey); jedisCluster.sadd(ALLKEY, key);
} }
@Override @Override
public T evict(String cacheKey) { public T evict(String key) {
T cache = lookup(cacheKey); T cache = lookup(key);
jedisCluster.del(cacheKey); jedisCluster.del(key);
jedisCluster.srem(ALLKEY, cacheKey); jedisCluster.srem(ALLKEY, key);
return cache; return cache;
} }

View File

@ -4,7 +4,7 @@ import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.model.Token; import com.foxinmy.weixin4j.model.Token;
/** /**
* 第三方应用组件永久授权码的存取 * 第三方应用永久授权码的存取
* *
* @className PerTicketManager * @className PerTicketManager
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)

View File

@ -125,7 +125,7 @@ public class WeixinComponentProxy {
* 组件ticket内容 * 组件ticket内容
* @throws WeixinException * @throws WeixinException
*/ */
public void cacheTicket(String componentId, String componentTicket) throws WeixinException { public void cacheComponentTicket(String componentId, String componentTicket) throws WeixinException {
component(componentId).getTicketManager().cachingTicket(componentTicket); component(componentId).getTicketManager().cachingTicket(componentTicket);
} }
@ -135,7 +135,7 @@ public class WeixinComponentProxy {
* @see {@link #getComponentAuthorizeURL(String, String,String)} * @see {@link #getComponentAuthorizeURL(String, String,String)}
* @param componentId * @param componentId
* 组件ID * 组件ID
* @see {@link #cacheTicket(String, String)} * @see {@link #cacheComponentTicket(String, String)}
* @return 请求授权的URL * @return 请求授权的URL
* @throws WeixinException * @throws WeixinException
*/ */

View File

@ -146,7 +146,7 @@ public class WeixinSuiteProxy {
* 推送suite_ticket协议</a> * 推送suite_ticket协议</a>
* @throws WeixinException * @throws WeixinException
*/ */
public void cacheTicket(String suiteId, String suiteTicket) throws WeixinException { public void cacheSuiteTicket(String suiteId, String suiteTicket) throws WeixinException {
suite(suiteId).getTicketManager().cachingTicket(suiteTicket); suite(suiteId).getTicketManager().cachingTicket(suiteTicket);
} }
@ -156,7 +156,7 @@ public class WeixinSuiteProxy {
* @see {@link #getSuiteAuthorizeURL(String, String,String)} * @see {@link #getSuiteAuthorizeURL(String, String,String)}
* @param suiteId * @param suiteId
* 套件ID * 套件ID
* @see {@link #cacheTicket(String, String)} * @see {@link #cacheSuiteTicket(String, String)}
* @return 请求授权的URL * @return 请求授权的URL
* @throws WeixinException * @throws WeixinException
*/ */