2021与蓝度共同重构项目,服务端
liuhaonan
2021-12-24 4965d8affe8ab2f3302bda6c62f45dc5fd455f93
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
package com.sandu.ximon.admin.utils;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
/**
 * Redis工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("RedisUtils")
 
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
 
    private HashOperations<String, String, Object> hashOperations;
 
    private ListOperations<String, Object> listOperations;
 
    private SetOperations<String, Object> setOperations;
 
    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();
 
    /**
     *
     * @param key
     * @param value
     * @param expire    秒数
     * @return
     */
    public boolean set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return false;
    }
 
    public static RedisUtils getBean(){
        return (RedisUtils)SpringContextUtils.getBean("RedisUtils");
    }
 
    public boolean set(String key, Object value){
        return set(key, value, DEFAULT_EXPIRE);
    }
 
    public <T> T get(String key, Class<T> clazz, long expire) {
        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) {
        return get(key, clazz, NOT_EXPIRE);
    }
 
    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
 
    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }
 
    public boolean delete(String key) {
        return redisTemplate.delete(key);
    }
 
 
    public Set<String> allKeys(){
        return redisTemplate.keys("*");
    }
 
    public Set<String> keys(String str) {
        return redisTemplate.keys(str);
    }
 
    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        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){
        return gson.fromJson(json, clazz);
    }
 
 
    public String listToJson(List list) {
        return JSON.toJSONString(list);
    }
 
    public List jsonToList(String json, Class clazz){
        if(json.length() == 0) {
            return null;
        }
        JSONArray jsonArray = JSON.parseArray(json);
        return jsonArray.toJavaList(clazz);
    }
 
}