2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
167
168
169
170
171
172
173
174
package com.landtool.lanbase.common.utils;
 
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
 
import com.google.gson.Gson;
 
/**
 * @author lanbase
 * @Description: TODO(Redis工具类)
 * @date 2017-6-23 15:07
 */
@Component
public class RedisUtils {
 
    //是否开启redis缓存  true开启   false关闭
    @Value("${spring.redis.open: #{false}}")
    private boolean open;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
 
    /**  默认过期时长,单位:秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
 
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;
 
    private final static Gson gson = new Gson();
 
    public boolean exists(String key) {
        if(!open){
            return false;
        }
 
        return redisTemplate.hasKey(key);
    }
 
    public void set(String key, Object value, long expire){
        if(!open){
            return;
        }
 
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
 
    public void set(String key, Object value){
        if(!open){
            return;
        }
 
        set(key, value, DEFAULT_EXPIRE);
    }
 
    public <T> T get(String key, Class<T> clazz, long expire) {
        if(!open){
            return null;
        }
 
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }
 
    public <T> T get(String key, Class<T> clazz) {
        if(!open){
            return null;
        }
 
        return get(key, clazz, NOT_EXPIRE);
    }
 
    public String get(String key, long expire) {
        if(!open){
            return null;
        }
 
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
 
    public String get(String key) {
        if(!open){
            return null;
        }
 
        return get(key, NOT_EXPIRE);
    }
 
    public void delete(String key) {
        if(!open){
            return;
        }
 
        if(exists(key)){
            redisTemplate.delete(key);
        }
    }
 
    public void delete(String... keys) {
        if(!open){
            return;
        }
 
        for (String key : keys) {
            redisTemplate.delete(key);
        }
    }
 
    public void deletePattern(String pattern) {
        if(!open){
            return;
        }
 
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }
 
    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(!open){
            return null;
        }
 
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return gson.toJson(object);
    }
 
    /**
     * JSON数据,转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        if(!open){
            return null;
        }
 
        return gson.fromJson(json, clazz);
    }
 
}