zhouxiang
2022-03-24 fb3acc1ea0df99deb7c02b53440095ea0bd876d6
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
175
176
177
178
179
180
181
182
183
package com.hx.redis;
 
import org.junit.Before;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
 
/**
 * 文件处理工具
 *
 * @author wangrenhuang
 * @Date 2021-10-19
 */
@Component
public class RedisUtil {
    /**
     * [redis]
     */
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
 
    /**
     * [判断key是否存在]
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * [普通缓存获取]
     *
     * @param key 键
     * @return 值
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }
 
    /**
     * [普通缓存删除]
     *
     * @param key 键
     * @return 值
     */
    public boolean delete(String key) {
        try {
            Boolean aBoolean = redisTemplate.hasKey(key);
            return aBoolean == false ? true : redisTemplate.delete(key);
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * [普通缓存放入]
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
 
    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * 26
     * 指定缓存失效时间
     * 27
     *
     * @param key  键
     *             28
     * @param time 时间(秒)
     *             29
     * @return 30
     */
 
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
 
        }
 
    }
 
 
    /**
     * hash 删除操作
     *
     * @param key   键
     * @param valueKey 值
     * @return true成功 false 失败
     */
    public boolean hashDel(String key, Object... valueKey) {
        try {
            redisTemplate.opsForHash().delete(key,valueKey);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * hash 设置hashMapt操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSetMap(String key, Map<String,Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * hash 设置单个hashMap操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSet(String hashKey, String key, Object value) {
        try {
            redisTemplate.opsForHash().put(hashKey,key,value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}