RedisUtils.java
7.01 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.huaheng.common.redis;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.data.redis.core.*;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @ClassName RedisUtils
* @Description redis操作的工具类
* @CreateDate 2022/12/22 15:01
* @Version 1.0
**/
public class RedisUtils {
/**
* @Description 写入缓存
* @Param [redisTemplate, key, value]
* @Return void
**/
public static void set(RedisTemplate redisTemplate,final String key, Object value) throws Exception {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
}
/**
* @Description 写入缓存设置时效时间
* @Param [redisTemplate, key, value, expireTime]
* @Return void
**/
public static void set(RedisTemplate redisTemplate,final String key, Object value, Long expireTime) throws Exception{
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
/**
* @Description 批量删除key
* @Param [redisTemplate, pattern]
* @Return void
**/
public static void removePattern(RedisTemplate redisTemplate,final String pattern) throws Exception {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0){
redisTemplate.delete(keys);
}
}
/**
* @Description 删除对应的value
* @Param [redisTemplate, key]
* @Return void
**/
public static void remove(RedisTemplate redisTemplate,final String key) throws Exception{
if (exists(redisTemplate,key)) {
redisTemplate.delete(key);
}
}
/**
* @Description 判断缓存中是否有对应的value
* @Param [redisTemplate, key]
* @Return boolean
**/
public static boolean exists(RedisTemplate redisTemplate,final String key) throws Exception{
return redisTemplate.hasKey(key);
}
/**
* @Description 读取缓存
* @Param [redisTemplate, key]
* @Return java.lang.Object
**/
public static Object get(RedisTemplate redisTemplate,final String key) throws Exception{
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* @Description 哈希 添加
* @Param [redisTemplate, key, hashKey, value]
* @Return void
**/
public static void hmSet(RedisTemplate redisTemplate,String key, Object hashKey, Object value) throws Exception{
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* @Description 删除哈希
* @Param [redisTemplate, key, value]
* @Return void
**/
public static void hmDel(RedisTemplate redisTemplate,String key, Object... value) throws Exception{
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.delete(key,value);
}
/**
* @Description 哈希获取数据
* @Param [redisTemplate, key, hashKey]
* @Return java.lang.Object
**/
public static Object hmGet(RedisTemplate redisTemplate,String key, Object hashKey) throws Exception{
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* @Description 列表添加
* @Param [redisTemplate, k, v]
* @Return void
**/
public static void lPush(RedisTemplate redisTemplate,String k,Object v) throws Exception{
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* @Description 列表获取
* @Param [redisTemplate, k, l, l1]
* @Return java.util.List<java.lang.Object>
**/
public static List<Object> lRange(RedisTemplate redisTemplate,String k, long l, long l1) throws Exception{
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* @Description 集合添加
* @Param [redisTemplate, key, value]
* @Return void
**/
public static void add(RedisTemplate redisTemplate,String key,Object value )throws Exception{
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}
/**
* @Description 集合获取
* @Param [redisTemplate, key]
* @Return java.util.Set<java.lang.Object>
**/
public static Set<Object> setMembers(RedisTemplate redisTemplate,String key) throws Exception{
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* @Description 有序集合添加
* @Param [redisTemplate, key, value, scoure]
* @Return void
**/
public static void zAdd(RedisTemplate redisTemplate,String key,Object value,double scoure) throws Exception{
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* @Description 有序集合获取
* @Param [redisTemplate, key, scoure, scoure1]
* @Return java.util.Set<java.lang.Object>
**/
public static Set<Object> rangeByScore(RedisTemplate redisTemplate,String key,double scoure,double scoure1)throws Exception{
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
/**
* @Description 分布式上锁
* @Param redisTemplate key value exprieTime 过期时间,单位分钟
* @Return java.lang.Boolean
**/
public static Boolean tryLock(RedisTemplate redisTemplate,String key, long value,long exprieTime) {
if (redisTemplate.opsForValue().setIfAbsent(key, value, exprieTime ,TimeUnit.SECONDS)) {
return true;
}
Object currentValue = redisTemplate.opsForValue().get(key);
if (ObjectUtils.isNotEmpty(currentValue) && Long.valueOf(currentValue.toString()) < System.currentTimeMillis()) {
//获取上一个锁的时间 如果高并发的情况可能会出现已经被修改的问题 所以多一次判断保证线程的安全
Object oldValue = redisTemplate.opsForValue().getAndSet(key, value);
if (ObjectUtils.isNotEmpty(oldValue) && oldValue.equals(currentValue)) {
return true;
}
}
return false;
}
/**
* @Description 释放分布式锁
* @Param redisTemplate key value
* @Return void
**/
public static void unlock(RedisTemplate redisTemplate,String key, long value) {
Object currentValue = redisTemplate.opsForValue().get(key);
try {
if (ObjectUtils.isNotEmpty(currentValue) && currentValue.equals(value)) {
redisTemplate.opsForValue().getOperations().delete(key);
}
} catch (Exception e) {
}
}
}