package com.yeshi.fanli.util; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.yeshi.fanli.entity.common.ImageInfo; import com.yeshi.fanli.entity.taobao.PidUser; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo; import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig; import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.service.inter.config.ConfigService; import com.yeshi.fanli.service.inter.taobao.TaoBaoShopService; import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService; import com.yeshi.fanli.util.taobao.TaoBaoCouponUtil; import com.yeshi.fanli.util.taobao.TaoBaoUtil; import com.yeshi.fanli.util.taobao.TaoKeApiUtil; import org.yeshi.utils.JsonUtil; import net.sf.json.JSONArray; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; //抢红包采用的redis @Component public class RedisManager { @Resource private JedisPool jedisPool; @Resource private TaoBaoShopService taoBaoShopService; @Resource private TaoBaoUnionConfigService taoBaoUnionConfigService; @Resource private ConfigService configService; /** * 缓存字符串 * * @param key * @param value */ private void setString(String key, String value) { Jedis jedis = jedisPool.getResource(); try { jedis.set(key, value); } finally { jedisPool.returnResource(jedis); } } /** * 删除某个键值 * * @param key * @param value */ private void removeKey(String key) { Jedis jedis = jedisPool.getResource(); try { jedis.del(key); } finally { jedisPool.returnResource(jedis); } } /** * 缓存字符串 * * @param key * @param value * @param seconds * -缓存时间(s) */ private void setString(String key, String value, int seconds) { Jedis jedis = jedisPool.getResource(); try { jedis.setex(key, seconds, value); } finally { jedisPool.returnResource(jedis); } } private String getString(String key) { Jedis jedis = jedisPool.getResource(); try { return jedis.get(key); } finally { jedisPool.returnResource(jedis); } } public void cacheCommonString(String key, String value, int seconds) { setString(key, value, seconds); } public void cacheCommonString(String key, String value) { setString(key, value); } public String getCommonString(String key) { return getString(key); } public void removeCommonString(String key) { removeKey(key); } /** * 将信息永久保存到Redis * * @param goods */ public void saveTaoBaoGoodsBriefForever(TaoBaoGoodsBrief goods) { String key = "taobao-goods-" + goods.getAuctionId(); if (Constant.IS_OUTNET) { cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods)); } } /** * 临时存储淘宝的商品详情 * * @param goods */ public void saveTaoBaoGoodsBriefTemp(TaoBaoGoodsBrief goods) { if (goods == null) return; String key = "taobao-goods-temp-" + goods.getAuctionId(); if (Constant.IS_OUTNET) { // 暂存4个小时的分享 cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods), 60 * 60 * 4); } } public TaoBaoGoodsBrief getTaoBaoGoodsTemp(Long auctionId) { String key = "taobao-goods-temp-" + auctionId; String value = getCommonString(key); if (!StringUtil.isNullOrEmpty(value)) { return JsonUtil.getSimpleGson().fromJson(value, TaoBaoGoodsBrief.class); } return null; } /** * 删除缓存 * * @param auctionId */ public void deleteTaoBaoGoodsBrief(Long auctionId) { String key = "taobao-goods-" + auctionId; if (Constant.IS_OUTNET) removeKey(key); } public TaoBaoGoodsBrief getTaoBaoGoodsBrief(long auctionId) throws TaobaoGoodsDownException { long startTime = System.currentTimeMillis(); String key = "taobao-goods-" + auctionId; String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) { TaoBaoGoodsBrief goods = null; goods = TaoKeApiUtil.searchGoodsDetail(auctionId); if (goods != null) // 缓存20分钟 if (Constant.IS_OUTNET) cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods), 60 * 20); LogHelper.test(auctionId + "-获取商品详情耗时:" + (System.currentTimeMillis() - startTime)); return goods; } else {// 直接取缓存 return JsonUtil.getSimpleGson().fromJson(value, TaoBaoGoodsBrief.class); } } public List getTaoBaoGoodsDetailImgs(long auctionId) { String key = "taobao-goods-detailimgs-size-" + auctionId; String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) { List list = null; try { list = TaoBaoUtil.getTBDetailImageWithSizev2(auctionId, configService.getTaoBaoProxyIP()); } catch (Exception e) { e.printStackTrace(); } if (list == null || list.size() == 0) { list = TaoBaoUtil.getTBDetailImageWithSize(auctionId); } if (list != null && list.size() > 0) // 缓存1天 if (Constant.IS_OUTNET) cacheCommonString(key, JsonUtil.getSimpleGson().toJson(list), 60 * 60 * 24); return list; } else {// 直接取缓存 List imgList = new Gson().fromJson(value, new TypeToken>() { }.getType()); return imgList; } } public String getXCXCouponToken(TaoBaoGoodsBrief tb) { List configList = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID); String key = "taobao-couple-xcx-" + tb.getAuctionId(); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) { value = TaoKeApiUtil.getTKToken(tb.getPictUrl(), tb.getTitle(), TaoBaoCouponUtil .getCoupleUrl(tb.getCouponActivityId(), configList.get(0).getDefaultPid(), tb.getAuctionId() + "")); if (value != null) // 缓存20分钟 if (Constant.IS_OUTNET) cacheCommonString(key, value, 60 * 20); return value; } else {// 直接取缓存 return value; } } /** * IP访问频率限制 适用于 小程序,H5,WEB 同一IP,5s内限制请求100次 * * @param ip */ public boolean ipFrequencyLimit(String ip, String apiName) { String key = ip + "-" + StringUtil.Md5(apiName); Jedis jedis = jedisPool.getResource(); try { long count = jedis.incr(key); if (count == 1) jedis.expire(key, 5); if (count >= 100) return true; else return false; } finally { jedisPool.returnResource(jedis); } } /** * 获取淘宝图片 * * @param auctionId * @return */ public List getTBImg(Long auctionId) { String key = "taobao-img-" + auctionId; String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) { List list = TaoBaoUtil.getTbImg(auctionId + ""); if (Constant.IS_OUTNET && list != null && list.size() > 0) { value = new Gson().toJson(list); cacheCommonString(key, value, 60 * 60 * 2); } return list; } else { JSONArray array = JSONArray.fromObject(value); List list = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { list.add(array.getString(i)); } return list; } } /** * 获取淘宝店铺信息 * * @param shopTitle * @param sellerId * @return */ public TaoBaoShopInfo getTBShopInfo(String shopTitle, Long sellerId, Long auctionId) { String key = "taobao-shop-" + sellerId; String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) { TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief(); goods.setShopTitle(shopTitle); goods.setSellerId(sellerId); goods.setAuctionId(auctionId); TaoBaoShopInfo info = taoBaoShopService.getTaoBaoShopInfo(goods); if (Constant.IS_OUTNET && info != null) { value = new Gson().toJson(info); cacheCommonString(key, value, 60 * 60 * 2); } return info; } else { return new Gson().fromJson(value, TaoBaoShopInfo.class); } } /** * 是否限制发送短信 * * @param phone * @param type * @return */ public boolean isSmsFrequencyLimit(String phone, int type) { if (!Constant.IS_OUTNET) return false; String key = "sms-" + phone + "-" + type; String value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) return false; else return true; } /** * 设置短信60s倒计时 * * @param phone * @param type */ public void sendSms(String phone, int type) { if (!Constant.IS_OUTNET) return; String key = "sms-" + phone + "-" + type; setString(key, "1", 10); } public void clearSMSFrequencyLimit(String phone, int type) { if (!Constant.IS_OUTNET) return; String key = "sms-" + phone + "-" + type; removeKey(key); } /** * 保存验证码 * * @param phone * @param type * @param code */ public void saveSMSVCode(String phone, int type, String code) { if (!Constant.IS_OUTNET) return; String key = "smscode-" + phone + "-" + type; // 保存2分钟 setString(key, code, 120); } /** * * @param phone * @param type * @return */ public String getSMSVCode(String phone, int type) { if (!Constant.IS_OUTNET) return ""; String key = "smscode-" + phone + "-" + type; // 保存2分钟 return getString(key); } /** * 清除掉验证码 * * @param phone * @param type * @param code */ public void clearSMSVCode(String phone, int type) { if (!Constant.IS_OUTNET) return; String key = "smscode-" + phone + "-" + type; removeKey(key); } /** * 保存绑定支付宝短信验证码的正确性 */ public void saveBindAlipayAccountSMSState(String phone) { String key = "smsstate-alipay-" + phone; // 验证后十分钟有效 setString(key, "1", 10 * 60); } /** * 绑定支付宝发送的短信是否在有效期内 * * @param phone * @return */ public boolean isBindAlipayAccountSMSStateValid(String phone) { String key = "smsstate-alipay-" + phone; return !StringUtil.isNullOrEmpty(getString(key)); } }