From f4a0f2acc63d7785eab108419a4e16f5f688cb95 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 18 一月 2020 12:06:27 +0800 Subject: [PATCH] 用户注册信息 --- fanli/src/main/java/com/yeshi/fanli/util/RedisManager.java | 194 ++++++++++++++++++++++++++++++++++++------------ 1 files changed, 146 insertions(+), 48 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/RedisManager.java b/fanli/src/main/java/com/yeshi/fanli/util/RedisManager.java index 69bbcd6..799cb72 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/RedisManager.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/RedisManager.java @@ -5,13 +5,14 @@ import javax.annotation.Resource; -import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Component; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import com.yeshi.fanli.dto.pdd.PDDGoodsDetail; import com.yeshi.fanli.entity.common.ImageInfo; +import com.yeshi.fanli.entity.jd.JDGoods; import com.yeshi.fanli.entity.taobao.PidUser; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo; @@ -21,6 +22,8 @@ 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.jd.JDApiUtil; +import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil; import com.yeshi.fanli.util.taobao.TaoBaoCouponUtil; import com.yeshi.fanli.util.taobao.TaoBaoUtil; import com.yeshi.fanli.util.taobao.TaoKeApiUtil; @@ -28,6 +31,7 @@ import net.sf.json.JSONArray; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; +import redis.clients.jedis.params.SetParams; //鎶㈢孩鍖呴噰鐢ㄧ殑redis @Component @@ -53,10 +57,12 @@ */ private void setString(String key, String value) { Jedis jedis = jedisPool.getResource(); + SetParams params=new SetParams().nx().ex(60); + jedis.set(key, value, params); try { jedis.set(key, value); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -72,7 +78,7 @@ try { jedis.del(key); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -90,7 +96,7 @@ try { jedis.setex(key, seconds, value); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -99,7 +105,7 @@ try { return jedis.get(key); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -108,7 +114,7 @@ try { jedis.incr(key); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -117,7 +123,7 @@ try { jedis.expire(key, seconds); } finally { - jedisPool.returnResource(jedis); + jedis.close(); } } @@ -145,7 +151,7 @@ public void saveTaoBaoGoodsBriefTemp(TaoBaoGoodsBrief goods) { if (goods == null) return; - String key = "taobao-goods-temp-" + goods.getAuctionId(); + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsTemp, goods.getAuctionId() + ""); if (Constant.IS_OUTNET) { // 鏆傚瓨4涓皬鏃剁殑鍒嗕韩 cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods), 60 * 60 * 4); @@ -153,7 +159,7 @@ } public TaoBaoGoodsBrief getTaoBaoGoodsTemp(Long auctionId) { - String key = "taobao-goods-temp-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsTemp, auctionId + ""); String value = getCommonString(key); if (!StringUtil.isNullOrEmpty(value)) { return JsonUtil.getSimpleGson().fromJson(value, TaoBaoGoodsBrief.class); @@ -167,14 +173,14 @@ * @param auctionId */ public void deleteTaoBaoGoodsBrief(Long auctionId) { - String key = "taobao-goods-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoods, auctionId + ""); if (Constant.IS_OUTNET) removeKey(key); } public TaoBaoGoodsBrief getTaoBaoGoodsBrief(long auctionId) throws TaobaoGoodsDownException { long startTime = System.currentTimeMillis(); - String key = "taobao-goods-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoods, auctionId + ""); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); @@ -196,7 +202,7 @@ } public List<ImageInfo> getTaoBaoGoodsDetailImgs(long auctionId) { - String key = "taobao-goods-detailimgs-size-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsDetailimgs, auctionId + ""); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); @@ -225,7 +231,7 @@ public String getXCXCouponToken(TaoBaoGoodsBrief tb) { List<TaoBaoUnionConfig> configList = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID); - String key = "taobao-couple-xcx-" + tb.getAuctionId(); + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoCoupleXCX, tb.getAuctionId() + ""); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); @@ -248,7 +254,7 @@ * @param ip */ public boolean ipFrequencyLimit(String ip, String apiName) { - String key = ip + "-" + StringUtil.Md5(apiName); + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.emptyKey, ip + "-" + StringUtil.Md5(apiName)); Jedis jedis = jedisPool.getResource(); try { long count = jedis.incr(key); @@ -259,7 +265,23 @@ else return false; } finally { - jedisPool.returnResource(jedis); + jedis.close(); + } + } + + public boolean frequencyLimit(String key, int timeS, int num) { + key = RedisKeyEnum.getRedisKey(RedisKeyEnum.frequencyLimit, key); + Jedis jedis = jedisPool.getResource(); + try { + long count = jedis.incr(key); + if (count == 1) + jedis.expire(key, timeS); + if (count >= num) + return true; + else + return false; + } finally { + jedis.close(); } } @@ -270,7 +292,7 @@ * @return */ public List<String> getTBImg(Long auctionId) { - String key = "taobao-img-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoImgs, auctionId + ""); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); @@ -299,7 +321,7 @@ * @return */ public TaoBaoShopInfo getTBShopInfo(String shopTitle, Long sellerId, Long auctionId) { - String key = "taobao-shop-" + sellerId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoShop, sellerId +""); String value = ""; if (Constant.IS_OUTNET) value = getCommonString(key); @@ -348,7 +370,7 @@ public boolean isSmsFrequencyLimit(String phone, int type) { if (!Constant.IS_OUTNET) return false; - String key = "sms-" + phone + "-" + type; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS, phone + "-" + type); String value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) return false; @@ -365,14 +387,14 @@ public void sendSms(String phone, int type) { if (!Constant.IS_OUTNET) return; - String key = "sms-" + phone + "-" + type; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS, phone + "-" + type); setString(key, "1", 10); } public void clearSMSFrequencyLimit(String phone, int type) { if (!Constant.IS_OUTNET) return; - String key = "sms-" + phone + "-" + type; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS, phone + "-" + type); removeKey(key); } @@ -385,9 +407,7 @@ */ public void saveSMSVCode(String phone, int type, String code) { - if (!Constant.IS_OUTNET) - return; - String key = "smscode-" + phone + "-" + type; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type); // 淇濆瓨2鍒嗛挓 setString(key, code, 120); } @@ -399,9 +419,9 @@ * @return */ public String getSMSVCode(String phone, int type) { - if (!Constant.IS_OUTNET) - return ""; - String key = "smscode-" + phone + "-" + type; + // if (!Constant.IS_OUTNET) + // return ""; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type); // 淇濆瓨2鍒嗛挓 return getString(key); } @@ -414,9 +434,9 @@ * @param code */ public void clearSMSVCode(String phone, int type) { - if (!Constant.IS_OUTNET) - return; - String key = "smscode-" + phone + "-" + type; + // if (!Constant.IS_OUTNET) + // return; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type); removeKey(key); } @@ -424,8 +444,7 @@ * 淇濆瓨缁戝畾鏀粯瀹濈煭淇¢獙璇佺爜鐨勬纭�� */ public void saveBindAlipayAccountSMSState(String phone) { - String key = "smsstate-alipay-" + phone; - + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSStateAlipay, phone + "" ); // 楠岃瘉鍚庡崄鍒嗛挓鏈夋晥 setString(key, "1", 10 * 60); } @@ -437,7 +456,7 @@ * @return */ public boolean isBindAlipayAccountSMSStateValid(String phone) { - String key = "smsstate-alipay-" + phone; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSStateAlipay, phone + "" ); return !StringUtil.isNullOrEmpty(getString(key)); } @@ -449,8 +468,7 @@ */ public void setInviteShortLink(long uid, String shortlink) { String value = ""; - String key = "invite-shortlink-" + uid; - + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.inviteShortLink, uid + "" ); if (Constant.IS_OUTNET) { value = getCommonString(key); @@ -469,10 +487,8 @@ * @return */ public String getInviteShortLink(long uid) { - String value = ""; - String key = "invite-shortlink-" + uid; - + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.inviteShortLink, uid + "" ); if (Constant.IS_OUTNET) value = getCommonString(key); @@ -486,7 +502,7 @@ * @param token */ public void saveCommonTaoToken(Long auctionId, String token) { - String key = "taobao-common-token-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenCommon, auctionId + "" ); if (Constant.IS_OUTNET) { if (!StringUtil.isNullOrEmpty(token)) { // 鍙d护缂撳瓨10澶� @@ -502,8 +518,37 @@ * @return */ public String getCommonTaoToken(Long auctionId) { - String key = "taobao-common-token-" + auctionId; + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenCommon, auctionId + "" ); + if (Constant.IS_OUTNET) { + return getCommonString(key); + } + return null; + } + /** + * 淇濆瓨娣樼ぜ閲戠殑鍙d护 + * + * @param url + * @param token + */ + public void saveTLJToken(String url, String token) { + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenTLJ, StringUtil.Md5(url)); + if (Constant.IS_OUTNET) { + if (!StringUtil.isNullOrEmpty(token)) { + // 鍙d护缂撳瓨10澶� + cacheCommonString(key, token, 60 * 60 * 24 * 10); + } + } + } + + /** + * 鑾峰彇娣樼ぜ閲戝彛浠� + * + * @param url + * @return + */ + public String getTLJToken(String url) { + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenTLJ, StringUtil.Md5(url)); if (Constant.IS_OUTNET) { return getCommonString(key); } @@ -517,7 +562,7 @@ * @param key * @param seconds */ - public void save(Class<?> clazz, String key, Integer seconds) { + public void saveObj(Object clazz, String key, Integer seconds) { if (clazz == null) return; String value = new Gson().toJson(clazz); @@ -527,29 +572,82 @@ cacheCommonString(key, value); } - public void saveList(List<Class<?>> clazzList, String key, Integer seconds) { + /** + * 淇濆瓨鍒楄〃 + * + * @param clazzList + * @param key + * @param seconds + */ + public <T> T saveObjList(List<T> clazzList, String key, Integer seconds) { if (clazzList == null) - return; + return null; String value = new Gson().toJson(clazzList); if (seconds != null) cacheCommonString(key, value, seconds); else cacheCommonString(key, value); + return null; } + /** + * 鑾峰彇瀵硅薄 + * + * @param clazz + * @param key + * @return + */ public Class<?> getObj(Class<?> clazz, String key) { String value = getCommonString(key); return (Class<?>) new Gson().fromJson(value, clazz); } - public List<T> getObjList(Class<T> clazz, String key) { - List<T> list = new ArrayList<>(); + /** + * 鑾峰彇鍒楄〃 + * + * @param clazz + * @param key + * @return + */ + public <T> List<T> getObjList(Class<T> clazz, String key) { String value = getCommonString(key); - JSONArray array = JSONArray.fromObject(value); - for (int i = 0; i < array.size(); i++) { - list.add(new Gson().fromJson(array.optJSONObject(i).toString(), clazz)); + return JsonUtil.jsonToList(value, clazz); + } + + public JDGoods getJDGoods(long goodsId) { + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.jingDongGoods, goodsId +""); + String value = getCommonString(key); + if (StringUtil.isNullOrEmpty(value)) { + JDGoods jdGoods = JDApiUtil.queryGoodsDetail(goodsId); + if (jdGoods == null) { + jdGoods = JDApiUtil.getGoodsDetail(goodsId); + } + + // 缂撳瓨20鍒嗛挓 + if (jdGoods != null) + cacheCommonString(key, JsonUtil.getSimpleGson().toJson(jdGoods), 60 * 20); + + return jdGoods; + } else {// 鐩存帴鍙栫紦瀛� + return JsonUtil.getSimpleGson().fromJson(value, JDGoods.class); } - return list; + } + + public PDDGoodsDetail getPDDGoodsDetail(long goodsId) { + String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.pinDuoDuoGoods, goodsId +""); + + String value = getCommonString(key); + if (StringUtil.isNullOrEmpty(value)) { + PDDGoodsDetail pddGoods = PinDuoDuoApiUtil.getGoodsDetail(goodsId); + + // 缂撳瓨20鍒嗛挓 + if (pddGoods != null) + cacheCommonString(key, JsonUtil.getSimpleGson().toJson(pddGoods), 60 * 20); + + return pddGoods; + } else {// 鐩存帴鍙栫紦瀛� + return JsonUtil.getSimpleGson().fromJson(value, PDDGoodsDetail.class); + } } } -- Gitblit v1.8.0