admin
2019-02-18 69ca458784b49c11b33dbee404ee242b4fb0b9c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package org.fanli.facade.goods.utils;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.fanli.facade.goods.entity.taobao.TaoBaoShopInfo;
import org.fanli.facade.goods.entity.taobao.TaoBaoUnionConfig;
import org.fanli.facade.goods.exception.taobao.TaobaoGoodsDownException;
import org.fanli.facade.goods.service.taobao.TaoBaoShopService;
import org.fanli.facade.goods.service.taobao.TaoBaoUnionConfigService;
import org.fanli.facade.goods.utils.taobao.TaoBaoCouponUtil;
import org.fanli.facade.goods.utils.taobao.TaoBaoUtil;
import org.fanli.facade.goods.utils.taobao.TaoKeGoodsApiUtil;
import org.springframework.stereotype.Component;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.base.Constant;
import com.yeshi.fanli.base.dto.ImageInfo;
import com.yeshi.fanli.base.entity.goods.TaoBaoGoodsBrief;
import com.yeshi.fanli.base.entity.user.PidUser;
import com.yeshi.fanli.base.log.LogHelper;
 
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
 
/**
 * 商品的Redis缓存
 * 
 * @author Administrator
 *
 */
@Component
public class RedisGoodsManager {
 
    @Resource
    private JedisPool jedisPool;
 
    @Resource
    private TaoBaoShopService taoBaoShopService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    /**
     * 缓存字符串
     * 
     * @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 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 = TaoKeGoodsApiUtil.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<ImageInfo> getTaoBaoGoodsDetailImgs(long auctionId) {
        String key = "taobao-goods-detailimgs-size-" + auctionId;
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
 
        if (StringUtil.isNullOrEmpty(value)) {
            List<ImageInfo> list = null;
            try {
                list = TaoBaoUtil.getTBDetailImageWithSizev2(auctionId);
            } 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<ImageInfo> imgList = new Gson().fromJson(value, new TypeToken<List<ImageInfo>>() {
            }.getType());
            return imgList;
        }
    }
 
    public String getXCXCouponToken(TaoBaoGoodsBrief tb) {
        List<TaoBaoUnionConfig> 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 = TaoKeGoodsApiUtil.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<String> getTBImg(Long auctionId) {
        String key = "taobao-img-" + auctionId;
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value)) {
            List<String> 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<String> 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);
        }
    }
}