This commit is contained in:
jinyu 2017-06-22 13:23:44 +08:00
parent 1a42b479ba
commit dd45139b73
3 changed files with 162 additions and 168 deletions

View File

@ -2,14 +2,14 @@ package com.foxinmy.weixin4j.cache;
import java.util.Set; import java.util.Set;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.SerializationUtils;
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.util.Pool; import redis.clients.util.Pool;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.SerializationUtils;
/** /**
* 用Redis保存缓存对象(推荐使用) * 用Redis保存缓存对象(推荐使用)
* *
@ -18,124 +18,117 @@ import com.foxinmy.weixin4j.util.SerializationUtils;
* @date 2015年1月9日 * @date 2015年1月9日
* @since JDK 1.6 * @since JDK 1.6
*/ */
public class RedisCacheStorager<T extends Cacheable> implements public class RedisCacheStorager<T extends Cacheable> implements CacheStorager<T> {
CacheStorager<T> {
private Pool<Jedis> jedisPool; private Pool<Jedis> jedisPool;
private final static String HOST = "127.0.0.1"; private final static String HOST = "127.0.0.1";
private final static int PORT = 6379; private final static int PORT = 6379;
private final static int TIMEOUT = 5000; private final static int TIMEOUT = 5000;
private final static int MAX_TOTAL = 50; private final static int MAX_TOTAL = 50;
private final static int MAX_IDLE = 5; private final static int MAX_IDLE = 5;
private final static int MAX_WAIT_MILLIS = 5000; private final static int MAX_WAIT_MILLIS = 5000;
private final static boolean TEST_ON_BORROW = false; private final static boolean TEST_ON_BORROW = false;
private final static boolean TEST_ON_RETURN = true; private final static boolean TEST_ON_RETURN = true;
private final static JedisPoolConfig POOLCONFIG;
static {
POOLCONFIG = new JedisPoolConfig();
POOLCONFIG.setMaxTotal(MAX_TOTAL);
POOLCONFIG.setMaxIdle(MAX_IDLE);
POOLCONFIG.setMaxWaitMillis(MAX_WAIT_MILLIS);
POOLCONFIG.setTestOnBorrow(TEST_ON_BORROW);
POOLCONFIG.setTestOnReturn(TEST_ON_RETURN);
}
public RedisCacheStorager() { public RedisCacheStorager() {
this(HOST, PORT, TIMEOUT); this(HOST, PORT, TIMEOUT);
} }
public RedisCacheStorager(String host, int port, int timeout) { public RedisCacheStorager(String host, int port, int timeout) {
JedisPoolConfig jedisPoolConfig = defaultConfig(); this(host, port, timeout, null, POOLCONFIG);
this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); }
}
public RedisCacheStorager(String host, int port, int timeout, String password) {
JedisPoolConfig jedisPoolConfig = defaultConfig();
this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
private JedisPoolConfig defaultConfig(){
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);
return jedisPoolConfig;
}
public RedisCacheStorager(JedisPoolConfig jedisPoolConfig) { public RedisCacheStorager(String host, int port, int timeout, String password) {
this(new JedisPool(jedisPoolConfig, HOST, PORT, TIMEOUT)); this(host, port, timeout, password, POOLCONFIG);
} }
public RedisCacheStorager(String host, int port, int timeout, public RedisCacheStorager(JedisPoolConfig poolConfig) {
JedisPoolConfig jedisPoolConfig) { this(new JedisPool(poolConfig, HOST, PORT, TIMEOUT));
this(new JedisPool(jedisPoolConfig, host, port, timeout)); }
}
public RedisCacheStorager(Pool<Jedis> jedisPool) { public RedisCacheStorager(String host, int port, int timeout, String password, JedisPoolConfig poolConfig) {
this.jedisPool = jedisPool; this(new JedisPool(poolConfig, host, port, timeout, password));
} }
@SuppressWarnings("unchecked") public RedisCacheStorager(Pool<Jedis> jedisPool) {
@Override this.jedisPool = jedisPool;
public T lookup(String key) { }
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
byte[] value = jedis.get(key.getBytes(Consts.UTF_8));
return value != null ? (T) SerializationUtils.deserialize(value)
: null;
} finally {
if (jedis != null) {
jedis.close();
}
}
}
@Override @SuppressWarnings("unchecked")
public void caching(String key, T cache) { @Override
Jedis jedis = null; public T lookup(String key) {
try { Jedis jedis = null;
jedis = jedisPool.getResource(); try {
byte[] cacheKey = key.getBytes(Consts.UTF_8); jedis = jedisPool.getResource();
byte[] value = SerializationUtils.serialize(cache); byte[] value = jedis.get(key.getBytes(Consts.UTF_8));
if (cache.getExpires() > 0) { return value != null ? (T) SerializationUtils.deserialize(value) : null;
jedis.setex(cacheKey, } finally {
(int) (cache.getExpires() - CUTMS) / 1000, value); if (jedis != null) {
} else { jedis.close();
jedis.set(cacheKey, value); }
} }
jedis.sadd(ALLKEY, key); }
} finally {
if (jedis != null) {
jedis.close();
}
}
}
@Override @Override
public T evict(String key) { public void caching(String key, T cache) {
T cache = lookup(key); Jedis jedis = null;
Jedis jedis = null; try {
try { jedis = jedisPool.getResource();
jedis = jedisPool.getResource(); byte[] cacheKey = key.getBytes(Consts.UTF_8);
jedis.del(key); byte[] value = SerializationUtils.serialize(cache);
jedis.srem(ALLKEY, key); if (cache.getExpires() > 0) {
} finally { jedis.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000, value);
if (jedis != null) { } else {
jedis.close(); jedis.set(cacheKey, value);
} }
} jedis.sadd(ALLKEY, key);
return cache; } finally {
} if (jedis != null) {
jedis.close();
}
}
}
@Override @Override
public void clear() { public T evict(String key) {
Jedis jedis = null; T cache = lookup(key);
try { Jedis jedis = null;
jedis = jedisPool.getResource(); try {
Set<String> cacheKeys = jedis.smembers(ALLKEY); jedis = jedisPool.getResource();
if (!cacheKeys.isEmpty()) { jedis.del(key);
cacheKeys.add(ALLKEY); jedis.srem(ALLKEY, key);
jedis.del(cacheKeys.toArray(new String[cacheKeys.size()])); } finally {
} if (jedis != null) {
} finally { jedis.close();
if (jedis != null) { }
jedis.close(); }
} return cache;
} }
}
@Override
public void clear() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Set<String> cacheKeys = jedis.smembers(ALLKEY);
if (!cacheKeys.isEmpty()) {
cacheKeys.add(ALLKEY);
jedis.del(cacheKeys.toArray(new String[cacheKeys.size()]));
}
} finally {
if (jedis != null) {
jedis.close();
}
}
}
} }

View File

@ -30,14 +30,14 @@ public class RedisClusterCacheStorager<T extends Cacheable> implements
private final JedisCluster jedisCluster; private final JedisCluster jedisCluster;
public RedisClusterCacheStorager(Set<HostAndPort> nodes) { public RedisClusterCacheStorager(Set<HostAndPort> nodes) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); JedisPoolConfig poolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(MAX_TOTAL); poolConfig.setMaxTotal(MAX_TOTAL);
jedisPoolConfig.setMaxIdle(MAX_IDLE); poolConfig.setMaxIdle(MAX_IDLE);
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); poolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); poolConfig.setTestOnBorrow(TEST_ON_BORROW);
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN); poolConfig.setTestOnReturn(TEST_ON_RETURN);
this.jedisCluster = new JedisCluster(nodes, CONNECTION_TIMEOUT, this.jedisCluster = new JedisCluster(nodes, CONNECTION_TIMEOUT,
SO_TIMEOUT, MAX_REDIRECTIONS, jedisPoolConfig); SO_TIMEOUT, MAX_REDIRECTIONS, poolConfig);
} }
public RedisClusterCacheStorager(Set<HostAndPort> nodes, public RedisClusterCacheStorager(Set<HostAndPort> nodes,

View File

@ -1,5 +1,8 @@
package com.foxinmy.weixin4j.payment.mch; package com.foxinmy.weixin4j.payment.mch;
import java.util.HashMap;
import java.util.Map;
import com.foxinmy.weixin4j.model.WeixinPayAccount; import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.payment.PayRequest; import com.foxinmy.weixin4j.payment.PayRequest;
import com.foxinmy.weixin4j.type.SignType; import com.foxinmy.weixin4j.type.SignType;
@ -9,64 +12,62 @@ import com.foxinmy.weixin4j.util.MapUtil;
/** /**
* APP支付 * APP支付
* *
* @className APPPayRequest * @className APPPayRequest
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2015年12月25日 * @date 2015年12月25日
* @since JDK 1.6 * @since JDK 1.6
* @see com.foxinmy.weixin4j.payment.mch.PrePay * @see com.foxinmy.weixin4j.payment.mch.PrePay
* @see com.foxinmy.weixin4j.payment.PayRequest * @see com.foxinmy.weixin4j.payment.PayRequest
* @see <a * @see <a href=
* href="https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1">APP支付</a> * "https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1">APP支付</a>
*/ */
public class APPPayRequest extends AbstractPayRequest { public class APPPayRequest extends AbstractPayRequest {
public APPPayRequest(String prePayId, WeixinPayAccount payAccount) { public APPPayRequest(String prePayId, WeixinPayAccount payAccount) {
super(prePayId, payAccount); super(prePayId, payAccount);
} }
@Override @Override
public TradeType getPaymentType() { public TradeType getPaymentType() {
return TradeType.APP; return TradeType.APP;
} }
/** /**
* <font color="red">只做查看之用,请不要尝试作为支付请求</font> * <font color="red">只做查看之用,请不要尝试作为支付请求</font>
*/ */
@Override @Override
public PayRequest toRequestObject() { public PayRequest toRequestObject() {
PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), "Sign=WXPay");
"Sign=WXPay"); payRequest.setPartnerId(getPaymentAccount().getMchId());
payRequest.setPartnerId(getPaymentAccount().getMchId()); payRequest.setPrepayId(getPrePayId());
payRequest.setPrepayId(getPrePayId()); Map<String, String> map = new HashMap<String, String>();
String sign = DigestUtil.MD5( map.put("appid", getPaymentAccount().getId());
String.format("%s&key=%s", map.put("partnerid", getPaymentAccount().getMchId());
MapUtil.toJoinString(payRequest, false, true), map.put("prepayid", getPrePayId());
getPaymentAccount().getPaySignKey())).toUpperCase(); map.put("package", payRequest.getPackageInfo());
payRequest.setPaySign(sign); map.put("timestamp", payRequest.getTimeStamp());
payRequest.setSignType(SignType.MD5); map.put("noncestr", payRequest.getNonceStr());
return payRequest; String sign = DigestUtil.MD5(
} String.format("%s&key=%s", MapUtil.toJoinString(map, false, true), getPaymentAccount().getPaySignKey()))
.toUpperCase();
payRequest.setPaySign(sign);
payRequest.setSignType(SignType.MD5);
return payRequest;
}
@Override @Override
public String toRequestString() { public String toRequestString() {
PayRequest payRequest = toRequestObject(); PayRequest payRequest = toRequestObject();
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
content.append("<xml>"); content.append("<xml>");
content.append(String.format("<appid><![CDATA[%s]]></appid>", content.append(String.format("<appid><![CDATA[%s]]></appid>", payRequest.getAppId()));
payRequest.getAppId())); content.append(String.format("<partnerid><![CDATA[%s]]></partnerid>", getPaymentAccount().getPartnerId()));
content.append(String.format("<partnerid><![CDATA[%s]]></partnerid>", content.append(String.format("<prepayid><![CDATA[%s]]></prepayid>", payRequest.getPrepayId()));
getPaymentAccount().getPartnerId())); content.append(String.format("<package><![CDATA[%s]]></package>", payRequest.getPackageInfo()));
content.append(String.format("<prepayid><![CDATA[%s]]></prepayid>", content.append(String.format("<noncestr><![CDATA[%s]]></noncestr>", payRequest.getNonceStr()));
payRequest.getPrepayId())); content.append(String.format("<timestamp><![CDATA[%s]]></timestamp>", payRequest.getTimeStamp()));
content.append(String.format("<package><![CDATA[%s]]></package>", content.append(String.format("<sign><![CDATA[%s]]></sign>", payRequest.getPaySign()));
payRequest.getPackageInfo())); content.append("</xml>");
content.append(String.format("<noncestr><![CDATA[%s]]></noncestr>", return content.toString();
payRequest.getNonceStr())); }
content.append(String.format("<timestamp><![CDATA[%s]]></timestamp>",
payRequest.getTimeStamp()));
content.append(String.format("<sign><![CDATA[%s]]></sign>",
payRequest.getPaySign()));
content.append("</xml>");
return content.toString();
}
} }