admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
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
package com.yeshi.fanli.util.cache;
 
import java.util.Calendar;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.google.gson.Gson;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Component
public class TaoBaoGoodsCacheUtil {
    @Resource
    private RedisManager redisManager;
 
    /**
     * 保存常规的淘宝商品详情(搜索,详情)
     * 
     * @param goods
     */
    public void saveCommonTaoBaoGoodsInfo(TaoBaoGoodsBrief goods) {
        if (goods == null || goods.getAuctionId() == null)
            return;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsCommon, goods.getAuctionId() + "");
        // 保存20分钟
        redisManager.cacheCommonString(key, new Gson().toJson(goods), 60 * 20);
    }
 
    /**
     * 获取缓存
     * 
     * @param auctionId
     * @return
     */
    public TaoBaoGoodsBrief getCommonTaoBaoGoodsInfo(long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsCommon, auctionId + "");
        String value = redisManager.getCommonString(key);
        if (!StringUtil.isNullOrEmpty(value)) {
            return new Gson().fromJson(value, TaoBaoGoodsBrief.class);
        } else
            return null;
    }
 
    /**
     * 是否可以添加到更新队列中
     * 
     * @param auctionId
     * @return
     */
    public boolean canAddToUpdateQueue(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoQueue, auctionId + "");
        String value = redisManager.getCommonString(key);
        if (StringUtil.isNullOrEmpty(value))
            return true;
        else
            return false;
    }
 
    /**
     * 增加添加到队列中的记录
     * 
     * @param auctionId
     */
    public void addAddToQueueHistory(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoQueue, auctionId + "");
        redisManager.cacheCommonString(key, "1", 60 * 60 * 2);// 有效期2个小时
    }
 
    /**
     * 查询商品是否需要更新
     * 
     * @param actionId
     * @return
     */
    public boolean needUpdate(Long actionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoUpdate, actionId + "");
        String value = redisManager.getCommonString(key);
        if (StringUtil.isNullOrEmpty(value))
            return true;
        else
            return false;
    }
 
    /**
     * 添加更新记录数据
     * 
     * @param actionId
     * @return
     */
    public void addUpdateHistory(Long actionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoUpdate, actionId + "");
        redisManager.cacheCommonString(key, "1", 60 * 60 * 1);// 有效期1个小时
    }
 
    /**
     * 删除更新记录
     */
    public void removeUpdateHistory() {
        // TODO 凌晨删除缓存记录
    }
 
    /**
     * 保存口令对应的商品ID
     * 
     * @param token
     * @param auctionId
     */
    public void saveTokenGoodsIdMap(String token, Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenGoodsMap, StringUtil.Md5(token));
        redisManager.cacheCommonString(key, auctionId + "", 60 * 30);// 有效期30分钟
    }
 
    /**
     * 通过口令获取商品ID
     * 
     * @param token
     * @return
     */
    public Long getGoodsIdByToken(String token) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenGoodsMap, StringUtil.Md5(token));
        String goodsId = redisManager.getCommonString(key);
        if (StringUtil.isNullOrEmpty(goodsId))
            return null;
        return Long.parseLong(goodsId);
    }
 
    /**
     * 缓存转链结果
     * @Title: cacheBaseConvertLink
     * @Description: 
     * @param auctionId
     * @param pid
     * @param link 
     * void 返回类型
     * @throws
     */
    public void cacheBaseConvertLink(Long auctionId, String pid, String link) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoBaseLink, StringUtil.Md5(auctionId + "#" + pid));
        //
        Calendar calendar = Calendar.getInstance();
        long now = calendar.getTimeInMillis();
        calendar.add(Calendar.HOUR, 1);
 
        Long time = TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(calendar.getTimeInMillis(), "yyyy-MM-dd HH"),
                "yyyy-MM-dd HH");
 
        int second = (int) ((time - now) / 1000);
        if (second <= 0)
            second = 1;
 
        redisManager.cacheCommonString(key, link, second);// 有效期30分钟
    }
 
    public String getBaseConvertLink(Long auctionId, String pid) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoBaseLink, StringUtil.Md5(auctionId + "#" + pid));
        String url = redisManager.getCommonString(key);
 
        return StringUtil.isNullOrEmpty(url) ? null : url;
    }
 
}