13693261870
19 小时以前 4c6dcf757cc7962a8dbaefd83588cc576ee44594
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
package com.terra.common.service;
 
import com.terra.common.entity.all.SettingData;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
/**
 * Redis服务类
 * @author WWW
 */
@Service
public class RedisService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    /**
     * 获取Redis模板
     */
    public RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }
 
    /**
     * 设置值到redis中
     *
     * @param key   键
     * @param value 值
     */
    public void put(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
 
    /**
     * 设置值到redis中,并设置过期时间
     *
     * @param key     键
     * @param value   值
     * @param timeout 时间
     * @param unit    单位
     */
    public void put(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }
 
    /**
     * 根据key获取value
     *
     * @param key 键
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
 
    /**
     * 是否存在key
     *
     * @param key 键
     */
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
 
    /**
     * 移除key
     *
     * @param key 键
     */
    public void delete(String key) {
        if (hasKey(key)) {
            redisTemplate.delete(key);
        }
    }
 
    /**
     * 清空指定键前缀
     *
     * @param subKeyString 键前缀
     */
    public void clearKeys(String subKeyString) {
        Set<String> keys = redisTemplate.keys(subKeyString + "*");
        if (!keys.isEmpty()) {
            redisTemplate.delete(keys);
        }
    }
 
    /**
     * 清空所有
     */
    public void clearAll() {
        Set<String> keys = redisTemplate.keys("*");
        if (!keys.isEmpty()) {
            redisTemplate.delete(keys);
        }
    }
 
    /**
     * 根据Key获取List集合
     */
    public <T> List<T> getListByKey(String key) {
        Object obj = get(key);
        if (obj instanceof List<?>) {
            return (List<T>) obj;
        }
 
        return null;
    }
 
    /**
     * 根据Key保存数据
     */
    public <T> void saveListByKey(String key, List<T> list) {
        if (null != list && !list.isEmpty()) {
            put(key, list, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES);
        }
    }
 
    /**
     * 根据Key保存数据
     */
    public <T> void saveListByKey(String key, List<T> list, Integer minutes) {
        if (null != list && !list.isEmpty()) {
            put(key, list, minutes, TimeUnit.MINUTES);
        }
    }
}