郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.specialWork.service.impl;
 
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.specialWork.service.RedisService;
import com.gkhy.safePlatform.specialWork.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.concurrent.TimeUnit;
 
@Service("specialWorkRedisService")
public class RedisServiceImpl implements RedisService {
 
    @Autowired
    private RedisUtils redisUtils;
 
    @Override
    public void setConsumerMessageCacheKeyAndExpireTime(String key, Long expireTime) {
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        if (expireTime == null) {
            expireTime = 5L;
        }
        // 任意塞的一个值
        redisUtils.set(key, Boolean.TRUE, expireTime, TimeUnit.SECONDS);
    }
 
    @Override
    public String getCacheKey(String key) {
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        Object o = redisUtils.get(key);
        if (o != null) {
            return o.toString();
        }
        return null;
    }
}