admin
2021-06-26 6b2670dfa68af9ce2e36a5f9580125f4fc6da570
service-goldcorn/src/main/java/com/ks/goldcorn/manager/RedisManager.java
@@ -3,8 +3,11 @@
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 {
@@ -20,9 +23,63 @@
        }
    }
    public void get() {
        redisUtil.get("");
    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);
    }
}