admin
2021-06-26 6b2670dfa68af9ce2e36a5f9580125f4fc6da570
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
package com.ks.push.manager;
 
import com.ks.lib.common.util.RedisUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;
 
@Component
public class RedisManager {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    private RedisUtil redisUtil = null;
 
    //注入Redis监听
    public void setRedisUtil() {
        if (redisUtil == null) {
            redisUtil = new RedisUtil(redisTemplate);
        }
    }
 
    private String getGetGoldCornCountKey(String appCode, String uid, String sourceCode) {
        String key = String.format("getcorncount-%s-%s-%s", appCode, uid, sourceCode);
        return key;
    }
 
    private String getGetGoldCornTimeKey(String appCode, String uid, String sourceCode) {
        String key = String.format("getcorntime-%s-%s-%s", appCode, uid, sourceCode);
        return key;
    }
 
    /**
     * 获取金币增加的次数
     *
     * @param appCode
     * @param uid
     * @return
     */
    public int getGoldCornAddRecordCount(String appCode, String uid, String sourceCode) {
        String key = getGetGoldCornCountKey(appCode, uid, sourceCode);
        Object value = redisTemplate.opsForValue().get(key);
        if (value == null) {
            return 0;
        }
        return Integer.parseInt(value + "");
    }
 
 
    public void addCornSuccess(String appCode, String uid, String sourceCode, int nextTimeSpan) {
        long now = System.currentTimeMillis();
        Date expireTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now + 1000 * 60 * 60 * 24L, "yyyyMMdd"), "yyyyMMdd"));
        String countKey = getGetGoldCornCountKey(appCode, uid, sourceCode);
        increCount(countKey, expireTime);
        String timeKey = getGetGoldCornTimeKey(appCode, uid, sourceCode);
        redisTemplate.opsForValue().set(timeKey, 1, nextTimeSpan == 0 ? 1 : nextTimeSpan, TimeUnit.SECONDS);
    }
 
    public boolean canAddCornWithTime(String appCode, String uid, String sourceCode) {
        String timeKey = getGetGoldCornTimeKey(appCode, uid, sourceCode);
        return !redisTemplate.hasKey(timeKey);
    }
 
    public long getCanAddCornExpireTime(String appCode, String uid, String sourceCode) {
        String timeKey = getGetGoldCornTimeKey(appCode, uid, sourceCode);
        return redisTemplate.getExpire(timeKey);
    }
 
 
    /**
     * 增加次数
     *
     * @param key
     * @param expireTime
     */
    private void increCount(String key, Date expireTime) {
        redisTemplate.opsForValue().increment(key, 1);
        redisTemplate.expireAt(key, expireTime);
    }
 
 
}