Commit f89ef3abf4f3210a6f768cc47644a99fee1a46f6

Authored by 648540858
1 parent 4f2d4738

优化移动位置notify消息处理

src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestProcessor.java
... ... @@ -148,26 +148,30 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
148 148  
149 149 MobilePosition mobilePosition = new MobilePosition();
150 150 mobilePosition.setCreateTime(DateUtil.getNow());
  151 +
151 152 Element deviceIdElement = rootElement.element("DeviceID");
152 153 String channelId = deviceIdElement.getTextTrim().toString();
153 154 Device device = redisCatchStorage.getDevice(deviceId);
154 155  
155 156 if (device == null) {
156   - // 根据通道id查询设备Id
157   - List<Device> deviceList = deviceChannelService.getDeviceByChannelId(channelId);
158   - if (deviceList.size() > 0) {
159   - device = deviceList.get(0);
160   - }else {
161   - logger.warn("[mobilePosition移动位置Notify] 未找到通道{}所属的设备", channelId);
162   - return;
  157 + device = redisCatchStorage.getDevice(channelId);
  158 + if (device == null) {
  159 + // 根据通道id查询设备Id
  160 + List<Device> deviceList = deviceChannelService.getDeviceByChannelId(channelId);
  161 + if (deviceList.size() > 0) {
  162 + device = deviceList.get(0);
  163 + }
163 164 }
164 165 }
165   - if (device != null) {
166   - if (!ObjectUtils.isEmpty(device.getName())) {
167   - mobilePosition.setDeviceName(device.getName());
168   - }
  166 + if (device == null) {
  167 + logger.warn("[mobilePosition移动位置Notify] 未找到通道{}所属的设备", channelId);
  168 + return;
  169 + }
  170 + if (!ObjectUtils.isEmpty(device.getName())) {
  171 + mobilePosition.setDeviceName(device.getName());
169 172 }
170   - mobilePosition.setDeviceId(XmlUtil.getText(rootElement, "DeviceID"));
  173 +
  174 + mobilePosition.setDeviceId(device.getDeviceId());
171 175 mobilePosition.setChannelId(channelId);
172 176 String time = XmlUtil.getText(rootElement, "Time");
173 177 mobilePosition.setTime(time);
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java
... ... @@ -81,7 +81,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
81 81 try {
82 82 RequestEventExt evtExt = (RequestEventExt) evt;
83 83 String requestAddress = evtExt.getRemoteIpAddress() + ":" + evtExt.getRemotePort();
84   - logger.info("[注册请求] 开始处理: {}", requestAddress);
  84 +
85 85 // MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
86 86 // QueryExp protocol = Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"));
87 87 //// ObjectName name = new ObjectName("*:type=Connector,*");
... ... @@ -104,6 +104,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
104 104 AddressImpl address = (AddressImpl) fromHeader.getAddress();
105 105 SipUri uri = (SipUri) address.getURI();
106 106 String deviceId = uri.getUser();
  107 + logger.info("[注册请求] 设备:{}, 开始处理: {}", deviceId, requestAddress);
107 108 Device device = deviceService.getDevice(deviceId);
108 109  
109 110 RemoteAddressInfo remoteAddressInfo = SipUtils.getRemoteAddressFromRequest(request,
... ... @@ -112,7 +113,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
112 113 if (device != null &&
113 114 device.getSipTransactionInfo() != null &&
114 115 request.getCallIdHeader().getCallId().equals(device.getSipTransactionInfo().getCallId())) {
115   - logger.info("[注册请求] 注册续订: {}", device.getDeviceId());
  116 + logger.info("[注册请求] 设备:{}, 注册续订: {}",device.getDeviceId(), device.getDeviceId());
116 117 device.setExpires(request.getExpires().getExpires());
117 118 device.setIp(remoteAddressInfo.getIp());
118 119 device.setPort(remoteAddressInfo.getPort());
... ... @@ -132,7 +133,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
132 133 String password = (device != null && !ObjectUtils.isEmpty(device.getPassword()))? device.getPassword() : sipConfig.getPassword();
133 134 AuthorizationHeader authHead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
134 135 if (authHead == null && !ObjectUtils.isEmpty(password)) {
135   - logger.info("[注册请求] 回复401: {}", requestAddress);
  136 + logger.info("[注册请求] 设备:{}, 回复401: {}",deviceId, requestAddress);
136 137 response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request);
137 138 new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain());
138 139 sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response);
... ... @@ -147,7 +148,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
147 148 // 注册失败
148 149 response = getMessageFactory().createResponse(Response.FORBIDDEN, request);
149 150 response.setReasonPhrase("wrong password");
150   - logger.info("[注册请求] 密码/SIP服务器ID错误, 回复403: {}", requestAddress);
  151 + logger.info("[注册请求] 设备:{}, 密码/SIP服务器ID错误, 回复403: {}", deviceId, requestAddress);
151 152 sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response);
152 153 return;
153 154 }
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
... ... @@ -289,7 +289,7 @@ public class PlayServiceImpl implements IPlayService {
289 289 // 检查是否有y字段
290 290 if (ssrcIndex >= 0) {
291 291 //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
292   - String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  292 + String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12).trim();
293 293 // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
294 294 if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
295 295 return;
... ...
src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMobilePositionMapper.java
1 1 package com.genersoft.iot.vmp.storager.dao;
2 2  
3   -import java.util.List;
4   -
5 3 import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
6   -import org.apache.ibatis.annotations.*;
  4 +import org.apache.ibatis.annotations.Delete;
  5 +import org.apache.ibatis.annotations.Insert;
  6 +import org.apache.ibatis.annotations.Mapper;
  7 +import org.apache.ibatis.annotations.Select;
  8 +
  9 +import java.util.List;
7 10  
8 11 @Mapper
9 12 public interface DeviceMobilePositionMapper {
10 13  
11 14 @Insert("INSERT INTO device_mobile_position (deviceId,channelId, deviceName, time, longitude, latitude, altitude, speed, direction, reportSource, longitudeGcj02, latitudeGcj02, longitudeWgs84, latitudeWgs84, createTime) " +
12   - "VALUES (#{deviceId},#{channelId}, #{deviceName}, #{time}, #{longitude}, #{latitude}, #{altitude}, #{speed}, #{direction}, #{reportSource}, #{longitudeGcj02}, #{latitudeGcj02}, #{longitudeWgs84}, #{latitudeWgs84}, #{createTime})")
  15 + "VALUES (#{deviceId}, #{channelId}, #{deviceName}, #{time}, #{longitude}, #{latitude}, #{altitude}, #{speed}, #{direction}, #{reportSource}, #{longitudeGcj02}, #{latitudeGcj02}, #{longitudeWgs84}, #{latitudeWgs84}, #{createTime})")
13 16 int insertNewPosition(MobilePosition mobilePosition);
14 17  
15 18 @Select(value = {" <script>" +
... ...
src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil2.java
1   -package com.genersoft.iot.vmp.utils.redis;
2   -
3   -import com.alibaba.fastjson2.JSONObject;
4   -import com.genersoft.iot.vmp.utils.SpringBeanFactory;
5   -import org.springframework.data.redis.core.*;
6   -import org.springframework.util.CollectionUtils;
7   -
8   -import java.util.*;
9   -import java.util.concurrent.TimeUnit;
10   -
11   -/**
12   - * Redis工具类
13   - * @author swwheihei
14   - * @date 2020年5月6日 下午8:27:29
15   - */
16   -@SuppressWarnings(value = {"rawtypes", "unchecked"})
17   -public class RedisUtil2 {
18   -
19   - private static RedisTemplate redisTemplate;
20   -
21   - static {
22   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
23   - }
24   -
25   - /**
26   - * 指定缓存失效时间
27   - * @param key 键
28   - * @param time 时间(秒)
29   - * @return true / false
30   - */
31   - public static boolean expire(String key, long time) {
32   - if (redisTemplate == null) {
33   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
34   - }
35   - try {
36   - if (time > 0) {
37   - redisTemplate.expire(key, time, TimeUnit.SECONDS);
38   - }
39   - return true;
40   - } catch (Exception e) {
41   - e.printStackTrace();
42   - return false;
43   - }
44   - }
45   -
46   - /**
47   - * 根据 key 获取过期时间
48   - * @param key 键
49   - */
50   - public static long getExpire(String key) {
51   - if (redisTemplate == null) {
52   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
53   - }
54   - return redisTemplate.getExpire(key, TimeUnit.SECONDS);
55   - }
56   -
57   - /**
58   - * 判断 key 是否存在
59   - * @param key 键
60   - * @return true / false
61   - */
62   - public static boolean hasKey(String key) {
63   - if (redisTemplate == null) {
64   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
65   - }
66   - try {
67   - return redisTemplate.hasKey(key);
68   - } catch (Exception e) {
69   - e.printStackTrace();
70   - return false;
71   - }
72   - }
73   -
74   - /**
75   - * 删除缓存
76   - * @SuppressWarnings("unchecked") 忽略类型转换警告
77   - * @param key 键(一个或者多个)
78   - */
79   - public static boolean del(String... key) {
80   - if (redisTemplate == null) {
81   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
82   - }
83   - try {
84   - if (key != null && key.length > 0) {
85   - if (key.length == 1) {
86   - redisTemplate.delete(key[0]);
87   - } else {
88   -// 传入一个 Collection<String> 集合
89   - redisTemplate.delete(CollectionUtils.arrayToList(key));
90   - }
91   - }
92   - return true;
93   - } catch (Exception e) {
94   - e.printStackTrace();
95   - return false;
96   - }
97   - }
98   -
99   -// ============================== String ==============================
100   -
101   - /**
102   - * 普通缓存获取
103   - * @param key 键
104   - * @return 值
105   - */
106   - public static Object get(String key) {
107   - if (redisTemplate == null) {
108   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
109   - }
110   - return key == null ? null : redisTemplate.opsForValue().get(key);
111   - }
112   -
113   - /**
114   - * 普通缓存放入
115   - * @param key 键
116   - * @param value 值
117   - * @return true / false
118   - */
119   - public static boolean set(String key, Object value) {
120   - if (redisTemplate == null) {
121   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
122   - }
123   - try {
124   - redisTemplate.opsForValue().set(key, value);
125   - return true;
126   - } catch (Exception e) {
127   - e.printStackTrace();
128   - return false;
129   - }
130   - }
131   -
132   - /**
133   - * 普通缓存放入并设置时间
134   - * @param key 键
135   - * @param value 值
136   - * @param time 时间(秒),如果 time < 0 则设置无限时间
137   - * @return true / false
138   - */
139   - public static boolean set(String key, Object value, long time) {
140   - if (redisTemplate == null) {
141   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
142   - }
143   - try {
144   - if (time > 0) {
145   - redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
146   - } else {
147   - set(key, value);
148   - }
149   - return true;
150   - } catch (Exception e) {
151   - e.printStackTrace();
152   - return false;
153   - }
154   - }
155   -
156   - /**
157   - * 递增
158   - * @param key 键
159   - * @param delta 递增大小
160   - * @return
161   - */
162   - public static long incr(String key, long delta) {
163   - if (redisTemplate == null) {
164   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
165   - }
166   - if (delta < 0) {
167   - throw new RuntimeException("递增因子必须大于 0");
168   - }
169   - return redisTemplate.opsForValue().increment(key, delta);
170   - }
171   -
172   - /**
173   - * 递减
174   - * @param key 键
175   - * @param delta 递减大小
176   - * @return
177   - */
178   - public static long decr(String key, long delta) {
179   - if (redisTemplate == null) {
180   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
181   - }
182   - if (delta < 0) {
183   - throw new RuntimeException("递减因子必须大于 0");
184   - }
185   - return redisTemplate.opsForValue().increment(key, delta);
186   - }
187   -
188   -// ============================== Map ==============================
189   -
190   - /**
191   - * HashGet
192   - * @param key 键(no null)
193   - * @param item 项(no null)
194   - * @return 值
195   - */
196   - public static Object hget(String key, String item) {
197   - if (redisTemplate == null) {
198   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
199   - }
200   - return redisTemplate.opsForHash().get(key, item);
201   - }
202   -
203   - /**
204   - * 获取 key 对应的 map
205   - * @param key 键(no null)
206   - * @return 对应的多个键值
207   - */
208   - public static Map<Object, Object> hmget(String key) {
209   - if (redisTemplate == null) {
210   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
211   - }
212   - return redisTemplate.opsForHash().entries(key);
213   - }
214   -
215   - /**
216   - * HashSet
217   - * @param key 键
218   - * @param map 值
219   - * @return true / false
220   - */
221   - public static boolean hmset(String key, Map<Object, Object> map) {
222   - if (redisTemplate == null) {
223   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
224   - }
225   - try {
226   - redisTemplate.opsForHash().putAll(key, map);
227   - return true;
228   - } catch (Exception e) {
229   - e.printStackTrace();
230   - return false;
231   - }
232   - }
233   -
234   - /**
235   - * HashSet 并设置时间
236   - * @param key 键
237   - * @param map 值
238   - * @param time 时间
239   - * @return true / false
240   - */
241   - public static boolean hmset(String key, Map<?, ?> map, long time) {
242   - if (redisTemplate == null) {
243   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
244   - }
245   - try {
246   - redisTemplate.opsForHash().putAll(key, map);
247   - if (time > 0) {
248   - expire(key, time);
249   - }
250   - return true;
251   - } catch (Exception e) {
252   - e.printStackTrace();
253   - return false;
254   - }
255   - }
256   -
257   - /**
258   - * 向一张 Hash表 中放入数据,如不存在则创建
259   - * @param key 键
260   - * @param item 项
261   - * @param value 值
262   - * @return true / false
263   - */
264   - public static boolean hset(String key, String item, Object value) {
265   - if (redisTemplate == null) {
266   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
267   - }
268   - try {
269   - redisTemplate.opsForHash().put(key, item, value);
270   - return true;
271   - } catch (Exception e) {
272   - e.printStackTrace();
273   - return false;
274   - }
275   - }
276   -
277   - /**
278   - * 向一张 Hash表 中放入数据,并设置时间,如不存在则创建
279   - * @param key 键
280   - * @param item 项
281   - * @param value 值
282   - * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖)
283   - * @return true / false
284   - */
285   - public static boolean hset(String key, String item, Object value, long time) {
286   - if (redisTemplate == null) {
287   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
288   - }
289   - try {
290   - redisTemplate.opsForHash().put(key, item, value);
291   - if (time > 0) {
292   - expire(key, time);
293   - }
294   - return true;
295   - } catch (Exception e) {
296   - e.printStackTrace();
297   - return false;
298   - }
299   - }
300   -
301   - /**
302   - * 删除 Hash表 中的值
303   - * @param key 键
304   - * @param item 项(可以多个,no null)
305   - */
306   - public static void hdel(String key, Object... item) {
307   - if (redisTemplate == null) {
308   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
309   - }
310   - redisTemplate.opsForHash().delete(key, item);
311   - }
312   -
313   - /**
314   - * 判断 Hash表 中是否有该键的值
315   - * @param key 键(no null)
316   - * @param item 值(no null)
317   - * @return true / false
318   - */
319   - public static boolean hHasKey(String key, String item) {
320   - if (redisTemplate == null) {
321   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
322   - }
323   - return redisTemplate.opsForHash().hasKey(key, item);
324   - }
325   -
326   - /**
327   - * Hash递增,如果不存在则创建一个,并把新增的值返回
328   - * @param key 键
329   - * @param item 项
330   - * @param by 递增大小 > 0
331   - * @return
332   - */
333   - public static Double hincr(String key, String item, Double by) {
334   - if (redisTemplate == null) {
335   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
336   - }
337   - return redisTemplate.opsForHash().increment(key, item, by);
338   - }
339   -
340   - /**
341   - * Hash递减
342   - * @param key 键
343   - * @param item 项
344   - * @param by 递减大小
345   - * @return
346   - */
347   - public static Double hdecr(String key, String item, Double by) {
348   - if (redisTemplate == null) {
349   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
350   - }
351   - return redisTemplate.opsForHash().increment(key, item, -by);
352   - }
353   -
354   -// ============================== Set ==============================
355   -
356   - /**
357   - * 根据 key 获取 set 中的所有值
358   - * @param key 键
359   - * @return 值
360   - */
361   - public static Set<Object> sGet(String key) {
362   - if (redisTemplate == null) {
363   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
364   - }
365   - try {
366   - return redisTemplate.opsForSet().members(key);
367   - } catch (Exception e) {
368   - e.printStackTrace();
369   - return null;
370   - }
371   - }
372   -
373   - /**
374   - * 从键为 key 的 set 中,根据 value 查询是否存在
375   - * @param key 键
376   - * @param value 值
377   - * @return true / false
378   - */
379   - public static boolean sHasKey(String key, Object value) {
380   - if (redisTemplate == null) {
381   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
382   - }
383   - try {
384   - return redisTemplate.opsForSet().isMember(key, value);
385   - } catch (Exception e) {
386   - e.printStackTrace();
387   - return false;
388   - }
389   - }
390   -
391   - /**
392   - * 将数据放入 set缓存
393   - * @param key 键值
394   - * @param values 值(可以多个)
395   - * @return 成功个数
396   - */
397   - public static long sSet(String key, Object... values) {
398   - if (redisTemplate == null) {
399   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
400   - }
401   - try {
402   - return redisTemplate.opsForSet().add(key, values);
403   - } catch (Exception e) {
404   - e.printStackTrace();
405   - return 0;
406   - }
407   - }
408   -
409   - /**
410   - * 将数据放入 set缓存,并设置时间
411   - * @param key 键
412   - * @param time 时间
413   - * @param values 值(可以多个)
414   - * @return 成功放入个数
415   - */
416   - public static long sSet(String key, long time, Object... values) {
417   - if (redisTemplate == null) {
418   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
419   - }
420   - try {
421   - long count = redisTemplate.opsForSet().add(key, values);
422   - if (time > 0) {
423   - expire(key, time);
424   - }
425   - return count;
426   - } catch (Exception e) {
427   - e.printStackTrace();
428   - return 0;
429   - }
430   - }
431   -
432   - /**
433   - * 获取 set缓存的长度
434   - * @param key 键
435   - * @return 长度
436   - */
437   - public static long sGetSetSize(String key) {
438   - if (redisTemplate == null) {
439   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
440   - }
441   - try {
442   - return redisTemplate.opsForSet().size(key);
443   - } catch (Exception e) {
444   - e.printStackTrace();
445   - return 0;
446   - }
447   - }
448   -
449   - /**
450   - * 移除 set缓存中,值为 value 的
451   - * @param key 键
452   - * @param values 值
453   - * @return 成功移除个数
454   - */
455   - public static long setRemove(String key, Object... values) {
456   - if (redisTemplate == null) {
457   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
458   - }
459   - try {
460   - return redisTemplate.opsForSet().remove(key, values);
461   - } catch (Exception e) {
462   - e.printStackTrace();
463   - return 0;
464   - }
465   - }
466   -// ============================== ZSet ==============================
467   -
468   - /**
469   - * 添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能; zadd
470   - *
471   - * @param key
472   - * @param value
473   - * @param score
474   - */
475   - public static void zAdd(Object key, Object value, double score) {
476   - if (redisTemplate == null) {
477   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
478   - }
479   - redisTemplate.opsForZSet().add(key, value, score);
480   - }
481   -
482   - /**
483   - * 删除元素 zrem
484   - *
485   - * @param key
486   - * @param value
487   - */
488   - public static void zRemove(Object key, Object value) {
489   - if (redisTemplate == null) {
490   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
491   - }
492   - redisTemplate.opsForZSet().remove(key, value);
493   - }
494   -
495   - /**
496   - * score的增加or减少 zincrby
497   - *
498   - * @param key
499   - * @param value
500   - * @param delta -1 表示减 1 表示加1
501   - */
502   - public static Double zIncrScore(Object key, Object value, double delta) {
503   - if (redisTemplate == null) {
504   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
505   - }
506   - return redisTemplate.opsForZSet().incrementScore(key, value, delta);
507   - }
508   -
509   - /**
510   - * 查询value对应的score zscore
511   - *
512   - * @param key
513   - * @param value
514   - * @return
515   - */
516   - public static Double zScore(Object key, Object value) {
517   - if (redisTemplate == null) {
518   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
519   - }
520   - return redisTemplate.opsForZSet().score(key, value);
521   - }
522   -
523   - /**
524   - * 判断value在zset中的排名 zrank
525   - *
526   - * @param key
527   - * @param value
528   - * @return
529   - */
530   - public static Long zRank(Object key, Object value) {
531   - if (redisTemplate == null) {
532   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
533   - }
534   - return redisTemplate.opsForZSet().rank(key, value);
535   - }
536   -
537   - /**
538   - * 返回集合的长度
539   - *
540   - * @param key
541   - * @return
542   - */
543   - public static Long zSize(Object key) {
544   - if (redisTemplate == null) {
545   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
546   - }
547   - return redisTemplate.opsForZSet().zCard(key);
548   - }
549   -
550   - /**
551   - * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容 zrange
552   - *
553   - * 返回有序的集合,score小的在前面
554   - *
555   - * @param key
556   - * @param start
557   - * @param end
558   - * @return
559   - */
560   - public static Set<Object> zRange(Object key, int start, int end) {
561   - if (redisTemplate == null) {
562   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
563   - }
564   - return redisTemplate.opsForZSet().range(key, start, end);
565   - }
566   - /**
567   - * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容
568   - *
569   - * @param key
570   - * @param start
571   - * @param end
572   - * @return
573   - */
574   - public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) {
575   - if (redisTemplate == null) {
576   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
577   - }
578   - return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
579   - }
580   - /**
581   - * 查询集合中指定顺序的值 zrevrange
582   - *
583   - * 返回有序的集合中,score大的在前面
584   - *
585   - * @param key
586   - * @param start
587   - * @param end
588   - * @return
589   - */
590   - public static Set<String> zRevRange(Object key, int start, int end) {
591   - if (redisTemplate == null) {
592   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
593   - }
594   - return redisTemplate.opsForZSet().reverseRange(key, start, end);
595   - }
596   - /**
597   - * 根据score的值,来获取满足条件的集合 zrangebyscore
598   - *
599   - * @param key
600   - * @param min
601   - * @param max
602   - * @return
603   - */
604   - public static Set<String> zSortRange(Object key, int min, int max) {
605   - if (redisTemplate == null) {
606   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
607   - }
608   - return redisTemplate.opsForZSet().rangeByScore(key, min, max);
609   - }
610   -
611   -
612   -// ============================== List ==============================
613   -
614   - /**
615   - * 获取 list缓存的内容
616   - * @param key 键
617   - * @param start 开始
618   - * @param end 结束(0 到 -1 代表所有值)
619   - * @return
620   - */
621   - public static List<Object> lGet(String key, long start, long end) {
622   - if (redisTemplate == null) {
623   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
624   - }
625   - try {
626   - return redisTemplate.opsForList().range(key, start, end);
627   - } catch (Exception e) {
628   - e.printStackTrace();
629   - return null;
630   - }
631   - }
632   -
633   - /**
634   - * 获取 list缓存的长度
635   - * @param key 键
636   - * @return 长度
637   - */
638   - public static long lGetListSize(String key) {
639   - if (redisTemplate == null) {
640   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
641   - }
642   - try {
643   - return redisTemplate.opsForList().size(key);
644   - } catch (Exception e) {
645   - e.printStackTrace();
646   - return 0;
647   - }
648   - }
649   -
650   - /**
651   - * 根据索引 index 获取键为 key 的 list 中的元素
652   - * @param key 键
653   - * @param index 索引
654   - * 当 index >= 0 时 {0:表头, 1:第二个元素}
655   - * 当 index < 0 时 {-1:表尾, -2:倒数第二个元素}
656   - * @return 值
657   - */
658   - public static Object lGetIndex(String key, long index) {
659   - if (redisTemplate == null) {
660   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
661   - }
662   - try {
663   - return redisTemplate.opsForList().index(key, index);
664   - } catch (Exception e) {
665   - e.printStackTrace();
666   - return null;
667   - }
668   - }
669   -
670   - /**
671   - * 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list
672   - * @param key 键
673   - * @param value 值
674   - * @return true / false
675   - */
676   - public static boolean lSet(String key, Object value) {
677   - if (redisTemplate == null) {
678   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
679   - }
680   - try {
681   - redisTemplate.opsForList().rightPush(key, value);
682   - return true;
683   - } catch (Exception e) {
684   - e.printStackTrace();
685   - return false;
686   - }
687   - }
688   -
689   - /**
690   - * 将值 value 插入键为 key 的 list 中,并设置时间
691   - * @param key 键
692   - * @param value 值
693   - * @param time 时间
694   - * @return true / false
695   - */
696   - public static boolean lSet(String key, Object value, long time) {
697   - if (redisTemplate == null) {
698   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
699   - }
700   - try {
701   - redisTemplate.opsForList().rightPush(key, value);
702   - if (time > 0) {
703   - expire(key, time);
704   - }
705   - return true;
706   - } catch (Exception e) {
707   - e.printStackTrace();
708   - return false;
709   - }
710   - }
711   -
712   - /**
713   - * 将 values 插入键为 key 的 list 中
714   - * @param key 键
715   - * @param values 值
716   - * @return true / false
717   - */
718   - public static boolean lSetList(String key, List<Object> values) {
719   - if (redisTemplate == null) {
720   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
721   - }
722   - try {
723   - redisTemplate.opsForList().rightPushAll(key, values);
724   - return true;
725   - } catch (Exception e) {
726   - e.printStackTrace();
727   - return false;
728   - }
729   - }
730   -
731   - /**
732   - * 将 values 插入键为 key 的 list 中,并设置时间
733   - * @param key 键
734   - * @param values 值
735   - * @param time 时间
736   - * @return true / false
737   - */
738   - public static boolean lSetList(String key, List<Object> values, long time) {
739   - if (redisTemplate == null) {
740   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
741   - }
742   - try {
743   - redisTemplate.opsForList().rightPushAll(key, values);
744   - if (time > 0) {
745   - expire(key, time);
746   - }
747   - return true;
748   - } catch (Exception e) {
749   - e.printStackTrace();
750   - return false;
751   - }
752   - }
753   -
754   - /**
755   - * 根据索引 index 修改键为 key 的值
756   - * @param key 键
757   - * @param index 索引
758   - * @param value 值
759   - * @return true / false
760   - */
761   - public static boolean lUpdateIndex(String key, long index, Object value) {
762   - if (redisTemplate == null) {
763   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
764   - }
765   - try {
766   - redisTemplate.opsForList().set(key, index, value);
767   - return true;
768   - } catch (Exception e) {
769   - e.printStackTrace();
770   - return false;
771   - }
772   - }
773   -
774   - /**
775   - * 在键为 key 的 list 中删除值为 value 的元素
776   - * @param key 键
777   - * @param count 如果 count == 0 则删除 list 中所有值为 value 的元素
778   - * 如果 count > 0 则删除 list 中最左边那个值为 value 的元素
779   - * 如果 count < 0 则删除 list 中最右边那个值为 value 的元素
780   - * @param value
781   - * @return
782   - */
783   - public static long lRemove(String key, long count, Object value) {
784   - if (redisTemplate == null) {
785   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
786   - }
787   - try {
788   - return redisTemplate.opsForList().remove(key, count, value);
789   - } catch (Exception e) {
790   - e.printStackTrace();
791   - return 0;
792   - }
793   - }
794   -
795   - /**
796   - * 在键为 key 的 list中移除第一个元素
797   - * @param key 键
798   - * @return
799   - */
800   - public static Object lLeftPop(String key) {
801   - if (redisTemplate == null) {
802   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
803   - }
804   - return redisTemplate.opsForList().leftPop(key);
805   - }
806   -
807   - /**
808   - * 在键为 key 的 list中移除、最后一个元素
809   - * @param key 键
810   - * @return
811   - */
812   - public static Object lrightPop(String key) {
813   - if (redisTemplate == null) {
814   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
815   - }
816   - return redisTemplate.opsForList().rightPop(key);
817   - }
818   -
819   - /**
820   - * 模糊查询
821   - * @param key 键
822   - * @return true / false
823   - */
824   - public static List<Object> keys(String key) {
825   - if (redisTemplate == null) {
826   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
827   - }
828   - try {
829   - Set<String> set = redisTemplate.keys(key);
830   - return new ArrayList<>(set);
831   - } catch (Exception e) {
832   - e.printStackTrace();
833   - return null;
834   - }
835   - }
836   -
837   -
838   - /**
839   - * 模糊查询
840   - * @param query 查询参数
841   - * @return
842   - */
843   -// public static List<Object> scan(String query) {
844   -// List<Object> result = new ArrayList<>();
  1 +//package com.genersoft.iot.vmp.utils.redis;
  2 +//
  3 +//import com.alibaba.fastjson2.JSONObject;
  4 +//import com.genersoft.iot.vmp.utils.SpringBeanFactory;
  5 +//import org.springframework.data.redis.core.*;
  6 +//import org.springframework.util.CollectionUtils;
  7 +//
  8 +//import java.util.*;
  9 +//import java.util.concurrent.TimeUnit;
  10 +//
  11 +///**
  12 +// * Redis工具类
  13 +// * @author swwheihei
  14 +// * @date 2020年5月6日 下午8:27:29
  15 +// */
  16 +//@SuppressWarnings(value = {"rawtypes", "unchecked"})
  17 +//public class RedisUtil2 {
  18 +//
  19 +// private static RedisTemplate redisTemplate;
  20 +//
  21 +// static {
  22 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  23 +// }
  24 +//
  25 +// /**
  26 +// * 指定缓存失效时间
  27 +// * @param key 键
  28 +// * @param time 时间(秒)
  29 +// * @return true / false
  30 +// */
  31 +// public static boolean expire(String key, long time) {
  32 +// if (redisTemplate == null) {
  33 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  34 +// }
  35 +// try {
  36 +// if (time > 0) {
  37 +// redisTemplate.expire(key, time, TimeUnit.SECONDS);
  38 +// }
  39 +// return true;
  40 +// } catch (Exception e) {
  41 +// e.printStackTrace();
  42 +// return false;
  43 +// }
  44 +// }
  45 +//
  46 +// /**
  47 +// * 根据 key 获取过期时间
  48 +// * @param key 键
  49 +// */
  50 +// public static long getExpire(String key) {
  51 +// if (redisTemplate == null) {
  52 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  53 +// }
  54 +// return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  55 +// }
  56 +//
  57 +// /**
  58 +// * 判断 key 是否存在
  59 +// * @param key 键
  60 +// * @return true / false
  61 +// */
  62 +// public static boolean hasKey(String key) {
  63 +// if (redisTemplate == null) {
  64 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  65 +// }
  66 +// try {
  67 +// return redisTemplate.hasKey(key);
  68 +// } catch (Exception e) {
  69 +// e.printStackTrace();
  70 +// return false;
  71 +// }
  72 +// }
  73 +//
  74 +// /**
  75 +// * 删除缓存
  76 +// * @SuppressWarnings("unchecked") 忽略类型转换警告
  77 +// * @param key 键(一个或者多个)
  78 +// */
  79 +// public static boolean del(String... key) {
  80 +// if (redisTemplate == null) {
  81 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  82 +// }
  83 +// try {
  84 +// if (key != null && key.length > 0) {
  85 +// if (key.length == 1) {
  86 +// redisTemplate.delete(key[0]);
  87 +// } else {
  88 +//// 传入一个 Collection<String> 集合
  89 +// redisTemplate.delete(CollectionUtils.arrayToList(key));
  90 +// }
  91 +// }
  92 +// return true;
  93 +// } catch (Exception e) {
  94 +// e.printStackTrace();
  95 +// return false;
  96 +// }
  97 +// }
  98 +//
  99 +//// ============================== String ==============================
  100 +//
  101 +// /**
  102 +// * 普通缓存获取
  103 +// * @param key 键
  104 +// * @return 值
  105 +// */
  106 +// public static Object get(String key) {
  107 +// if (redisTemplate == null) {
  108 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  109 +// }
  110 +// return key == null ? null : redisTemplate.opsForValue().get(key);
  111 +// }
  112 +//
  113 +// /**
  114 +// * 普通缓存放入
  115 +// * @param key 键
  116 +// * @param value 值
  117 +// * @return true / false
  118 +// */
  119 +// public static boolean set(String key, Object value) {
  120 +// if (redisTemplate == null) {
  121 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  122 +// }
  123 +// try {
  124 +// redisTemplate.opsForValue().set(key, value);
  125 +// return true;
  126 +// } catch (Exception e) {
  127 +// e.printStackTrace();
  128 +// return false;
  129 +// }
  130 +// }
  131 +//
  132 +// /**
  133 +// * 普通缓存放入并设置时间
  134 +// * @param key 键
  135 +// * @param value 值
  136 +// * @param time 时间(秒),如果 time < 0 则设置无限时间
  137 +// * @return true / false
  138 +// */
  139 +// public static boolean set(String key, Object value, long time) {
  140 +// if (redisTemplate == null) {
  141 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  142 +// }
  143 +// try {
  144 +// if (time > 0) {
  145 +// redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  146 +// } else {
  147 +// set(key, value);
  148 +// }
  149 +// return true;
  150 +// } catch (Exception e) {
  151 +// e.printStackTrace();
  152 +// return false;
  153 +// }
  154 +// }
  155 +//
  156 +// /**
  157 +// * 递增
  158 +// * @param key 键
  159 +// * @param delta 递增大小
  160 +// * @return
  161 +// */
  162 +// public static long incr(String key, long delta) {
  163 +// if (redisTemplate == null) {
  164 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  165 +// }
  166 +// if (delta < 0) {
  167 +// throw new RuntimeException("递增因子必须大于 0");
  168 +// }
  169 +// return redisTemplate.opsForValue().increment(key, delta);
  170 +// }
  171 +//
  172 +// /**
  173 +// * 递减
  174 +// * @param key 键
  175 +// * @param delta 递减大小
  176 +// * @return
  177 +// */
  178 +// public static long decr(String key, long delta) {
  179 +// if (redisTemplate == null) {
  180 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  181 +// }
  182 +// if (delta < 0) {
  183 +// throw new RuntimeException("递减因子必须大于 0");
  184 +// }
  185 +// return redisTemplate.opsForValue().increment(key, delta);
  186 +// }
  187 +//
  188 +//// ============================== Map ==============================
  189 +//
  190 +// /**
  191 +// * HashGet
  192 +// * @param key 键(no null)
  193 +// * @param item 项(no null)
  194 +// * @return 值
  195 +// */
  196 +// public static Object hget(String key, String item) {
  197 +// if (redisTemplate == null) {
  198 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  199 +// }
  200 +// return redisTemplate.opsForHash().get(key, item);
  201 +// }
  202 +//
  203 +// /**
  204 +// * 获取 key 对应的 map
  205 +// * @param key 键(no null)
  206 +// * @return 对应的多个键值
  207 +// */
  208 +// public static Map<Object, Object> hmget(String key) {
  209 +// if (redisTemplate == null) {
  210 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  211 +// }
  212 +// return redisTemplate.opsForHash().entries(key);
  213 +// }
  214 +//
  215 +// /**
  216 +// * HashSet
  217 +// * @param key 键
  218 +// * @param map 值
  219 +// * @return true / false
  220 +// */
  221 +// public static boolean hmset(String key, Map<Object, Object> map) {
  222 +// if (redisTemplate == null) {
  223 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  224 +// }
  225 +// try {
  226 +// redisTemplate.opsForHash().putAll(key, map);
  227 +// return true;
  228 +// } catch (Exception e) {
  229 +// e.printStackTrace();
  230 +// return false;
  231 +// }
  232 +// }
  233 +//
  234 +// /**
  235 +// * HashSet 并设置时间
  236 +// * @param key 键
  237 +// * @param map 值
  238 +// * @param time 时间
  239 +// * @return true / false
  240 +// */
  241 +// public static boolean hmset(String key, Map<?, ?> map, long time) {
  242 +// if (redisTemplate == null) {
  243 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  244 +// }
  245 +// try {
  246 +// redisTemplate.opsForHash().putAll(key, map);
  247 +// if (time > 0) {
  248 +// expire(key, time);
  249 +// }
  250 +// return true;
  251 +// } catch (Exception e) {
  252 +// e.printStackTrace();
  253 +// return false;
  254 +// }
  255 +// }
  256 +//
  257 +// /**
  258 +// * 向一张 Hash表 中放入数据,如不存在则创建
  259 +// * @param key 键
  260 +// * @param item 项
  261 +// * @param value 值
  262 +// * @return true / false
  263 +// */
  264 +// public static boolean hset(String key, String item, Object value) {
  265 +// if (redisTemplate == null) {
  266 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  267 +// }
  268 +// try {
  269 +// redisTemplate.opsForHash().put(key, item, value);
  270 +// return true;
  271 +// } catch (Exception e) {
  272 +// e.printStackTrace();
  273 +// return false;
  274 +// }
  275 +// }
  276 +//
  277 +// /**
  278 +// * 向一张 Hash表 中放入数据,并设置时间,如不存在则创建
  279 +// * @param key 键
  280 +// * @param item 项
  281 +// * @param value 值
  282 +// * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖)
  283 +// * @return true / false
  284 +// */
  285 +// public static boolean hset(String key, String item, Object value, long time) {
  286 +// if (redisTemplate == null) {
  287 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  288 +// }
  289 +// try {
  290 +// redisTemplate.opsForHash().put(key, item, value);
  291 +// if (time > 0) {
  292 +// expire(key, time);
  293 +// }
  294 +// return true;
  295 +// } catch (Exception e) {
  296 +// e.printStackTrace();
  297 +// return false;
  298 +// }
  299 +// }
  300 +//
  301 +// /**
  302 +// * 删除 Hash表 中的值
  303 +// * @param key 键
  304 +// * @param item 项(可以多个,no null)
  305 +// */
  306 +// public static void hdel(String key, Object... item) {
  307 +// if (redisTemplate == null) {
  308 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  309 +// }
  310 +// redisTemplate.opsForHash().delete(key, item);
  311 +// }
  312 +//
  313 +// /**
  314 +// * 判断 Hash表 中是否有该键的值
  315 +// * @param key 键(no null)
  316 +// * @param item 值(no null)
  317 +// * @return true / false
  318 +// */
  319 +// public static boolean hHasKey(String key, String item) {
  320 +// if (redisTemplate == null) {
  321 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  322 +// }
  323 +// return redisTemplate.opsForHash().hasKey(key, item);
  324 +// }
  325 +//
  326 +// /**
  327 +// * Hash递增,如果不存在则创建一个,并把新增的值返回
  328 +// * @param key 键
  329 +// * @param item 项
  330 +// * @param by 递增大小 > 0
  331 +// * @return
  332 +// */
  333 +// public static Double hincr(String key, String item, Double by) {
  334 +// if (redisTemplate == null) {
  335 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  336 +// }
  337 +// return redisTemplate.opsForHash().increment(key, item, by);
  338 +// }
  339 +//
  340 +// /**
  341 +// * Hash递减
  342 +// * @param key 键
  343 +// * @param item 项
  344 +// * @param by 递减大小
  345 +// * @return
  346 +// */
  347 +// public static Double hdecr(String key, String item, Double by) {
  348 +// if (redisTemplate == null) {
  349 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  350 +// }
  351 +// return redisTemplate.opsForHash().increment(key, item, -by);
  352 +// }
  353 +//
  354 +//// ============================== Set ==============================
  355 +//
  356 +// /**
  357 +// * 根据 key 获取 set 中的所有值
  358 +// * @param key 键
  359 +// * @return 值
  360 +// */
  361 +// public static Set<Object> sGet(String key) {
  362 +// if (redisTemplate == null) {
  363 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  364 +// }
  365 +// try {
  366 +// return redisTemplate.opsForSet().members(key);
  367 +// } catch (Exception e) {
  368 +// e.printStackTrace();
  369 +// return null;
  370 +// }
  371 +// }
  372 +//
  373 +// /**
  374 +// * 从键为 key 的 set 中,根据 value 查询是否存在
  375 +// * @param key 键
  376 +// * @param value 值
  377 +// * @return true / false
  378 +// */
  379 +// public static boolean sHasKey(String key, Object value) {
  380 +// if (redisTemplate == null) {
  381 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  382 +// }
  383 +// try {
  384 +// return redisTemplate.opsForSet().isMember(key, value);
  385 +// } catch (Exception e) {
  386 +// e.printStackTrace();
  387 +// return false;
  388 +// }
  389 +// }
  390 +//
  391 +// /**
  392 +// * 将数据放入 set缓存
  393 +// * @param key 键值
  394 +// * @param values 值(可以多个)
  395 +// * @return 成功个数
  396 +// */
  397 +// public static long sSet(String key, Object... values) {
  398 +// if (redisTemplate == null) {
  399 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  400 +// }
  401 +// try {
  402 +// return redisTemplate.opsForSet().add(key, values);
  403 +// } catch (Exception e) {
  404 +// e.printStackTrace();
  405 +// return 0;
  406 +// }
  407 +// }
  408 +//
  409 +// /**
  410 +// * 将数据放入 set缓存,并设置时间
  411 +// * @param key 键
  412 +// * @param time 时间
  413 +// * @param values 值(可以多个)
  414 +// * @return 成功放入个数
  415 +// */
  416 +// public static long sSet(String key, long time, Object... values) {
  417 +// if (redisTemplate == null) {
  418 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  419 +// }
  420 +// try {
  421 +// long count = redisTemplate.opsForSet().add(key, values);
  422 +// if (time > 0) {
  423 +// expire(key, time);
  424 +// }
  425 +// return count;
  426 +// } catch (Exception e) {
  427 +// e.printStackTrace();
  428 +// return 0;
  429 +// }
  430 +// }
  431 +//
  432 +// /**
  433 +// * 获取 set缓存的长度
  434 +// * @param key 键
  435 +// * @return 长度
  436 +// */
  437 +// public static long sGetSetSize(String key) {
  438 +// if (redisTemplate == null) {
  439 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  440 +// }
  441 +// try {
  442 +// return redisTemplate.opsForSet().size(key);
  443 +// } catch (Exception e) {
  444 +// e.printStackTrace();
  445 +// return 0;
  446 +// }
  447 +// }
  448 +//
  449 +// /**
  450 +// * 移除 set缓存中,值为 value 的
  451 +// * @param key 键
  452 +// * @param values 值
  453 +// * @return 成功移除个数
  454 +// */
  455 +// public static long setRemove(String key, Object... values) {
  456 +// if (redisTemplate == null) {
  457 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  458 +// }
  459 +// try {
  460 +// return redisTemplate.opsForSet().remove(key, values);
  461 +// } catch (Exception e) {
  462 +// e.printStackTrace();
  463 +// return 0;
  464 +// }
  465 +// }
  466 +//// ============================== ZSet ==============================
  467 +//
  468 +// /**
  469 +// * 添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能; zadd
  470 +// *
  471 +// * @param key
  472 +// * @param value
  473 +// * @param score
  474 +// */
  475 +// public static void zAdd(Object key, Object value, double score) {
  476 +// if (redisTemplate == null) {
  477 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  478 +// }
  479 +// redisTemplate.opsForZSet().add(key, value, score);
  480 +// }
  481 +//
  482 +// /**
  483 +// * 删除元素 zrem
  484 +// *
  485 +// * @param key
  486 +// * @param value
  487 +// */
  488 +// public static void zRemove(Object key, Object value) {
  489 +// if (redisTemplate == null) {
  490 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  491 +// }
  492 +// redisTemplate.opsForZSet().remove(key, value);
  493 +// }
  494 +//
  495 +// /**
  496 +// * score的增加or减少 zincrby
  497 +// *
  498 +// * @param key
  499 +// * @param value
  500 +// * @param delta -1 表示减 1 表示加1
  501 +// */
  502 +// public static Double zIncrScore(Object key, Object value, double delta) {
  503 +// if (redisTemplate == null) {
  504 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  505 +// }
  506 +// return redisTemplate.opsForZSet().incrementScore(key, value, delta);
  507 +// }
  508 +//
  509 +// /**
  510 +// * 查询value对应的score zscore
  511 +// *
  512 +// * @param key
  513 +// * @param value
  514 +// * @return
  515 +// */
  516 +// public static Double zScore(Object key, Object value) {
  517 +// if (redisTemplate == null) {
  518 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  519 +// }
  520 +// return redisTemplate.opsForZSet().score(key, value);
  521 +// }
  522 +//
  523 +// /**
  524 +// * 判断value在zset中的排名 zrank
  525 +// *
  526 +// * @param key
  527 +// * @param value
  528 +// * @return
  529 +// */
  530 +// public static Long zRank(Object key, Object value) {
  531 +// if (redisTemplate == null) {
  532 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  533 +// }
  534 +// return redisTemplate.opsForZSet().rank(key, value);
  535 +// }
  536 +//
  537 +// /**
  538 +// * 返回集合的长度
  539 +// *
  540 +// * @param key
  541 +// * @return
  542 +// */
  543 +// public static Long zSize(Object key) {
  544 +// if (redisTemplate == null) {
  545 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  546 +// }
  547 +// return redisTemplate.opsForZSet().zCard(key);
  548 +// }
  549 +//
  550 +// /**
  551 +// * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容 zrange
  552 +// *
  553 +// * 返回有序的集合,score小的在前面
  554 +// *
  555 +// * @param key
  556 +// * @param start
  557 +// * @param end
  558 +// * @return
  559 +// */
  560 +// public static Set<Object> zRange(Object key, int start, int end) {
  561 +// if (redisTemplate == null) {
  562 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  563 +// }
  564 +// return redisTemplate.opsForZSet().range(key, start, end);
  565 +// }
  566 +// /**
  567 +// * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容
  568 +// *
  569 +// * @param key
  570 +// * @param start
  571 +// * @param end
  572 +// * @return
  573 +// */
  574 +// public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) {
  575 +// if (redisTemplate == null) {
  576 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  577 +// }
  578 +// return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
  579 +// }
  580 +// /**
  581 +// * 查询集合中指定顺序的值 zrevrange
  582 +// *
  583 +// * 返回有序的集合中,score大的在前面
  584 +// *
  585 +// * @param key
  586 +// * @param start
  587 +// * @param end
  588 +// * @return
  589 +// */
  590 +// public static Set<String> zRevRange(Object key, int start, int end) {
  591 +// if (redisTemplate == null) {
  592 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  593 +// }
  594 +// return redisTemplate.opsForZSet().reverseRange(key, start, end);
  595 +// }
  596 +// /**
  597 +// * 根据score的值,来获取满足条件的集合 zrangebyscore
  598 +// *
  599 +// * @param key
  600 +// * @param min
  601 +// * @param max
  602 +// * @return
  603 +// */
  604 +// public static Set<String> zSortRange(Object key, int min, int max) {
  605 +// if (redisTemplate == null) {
  606 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  607 +// }
  608 +// return redisTemplate.opsForZSet().rangeByScore(key, min, max);
  609 +// }
  610 +//
  611 +//
  612 +//// ============================== List ==============================
  613 +//
  614 +// /**
  615 +// * 获取 list缓存的内容
  616 +// * @param key 键
  617 +// * @param start 开始
  618 +// * @param end 结束(0 到 -1 代表所有值)
  619 +// * @return
  620 +// */
  621 +// public static List<Object> lGet(String key, long start, long end) {
  622 +// if (redisTemplate == null) {
  623 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  624 +// }
  625 +// try {
  626 +// return redisTemplate.opsForList().range(key, start, end);
  627 +// } catch (Exception e) {
  628 +// e.printStackTrace();
  629 +// return null;
  630 +// }
  631 +// }
  632 +//
  633 +// /**
  634 +// * 获取 list缓存的长度
  635 +// * @param key 键
  636 +// * @return 长度
  637 +// */
  638 +// public static long lGetListSize(String key) {
  639 +// if (redisTemplate == null) {
  640 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  641 +// }
  642 +// try {
  643 +// return redisTemplate.opsForList().size(key);
  644 +// } catch (Exception e) {
  645 +// e.printStackTrace();
  646 +// return 0;
  647 +// }
  648 +// }
  649 +//
  650 +// /**
  651 +// * 根据索引 index 获取键为 key 的 list 中的元素
  652 +// * @param key 键
  653 +// * @param index 索引
  654 +// * 当 index >= 0 时 {0:表头, 1:第二个元素}
  655 +// * 当 index < 0 时 {-1:表尾, -2:倒数第二个元素}
  656 +// * @return 值
  657 +// */
  658 +// public static Object lGetIndex(String key, long index) {
  659 +// if (redisTemplate == null) {
  660 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  661 +// }
  662 +// try {
  663 +// return redisTemplate.opsForList().index(key, index);
  664 +// } catch (Exception e) {
  665 +// e.printStackTrace();
  666 +// return null;
  667 +// }
  668 +// }
  669 +//
  670 +// /**
  671 +// * 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list
  672 +// * @param key 键
  673 +// * @param value 值
  674 +// * @return true / false
  675 +// */
  676 +// public static boolean lSet(String key, Object value) {
  677 +// if (redisTemplate == null) {
  678 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  679 +// }
  680 +// try {
  681 +// redisTemplate.opsForList().rightPush(key, value);
  682 +// return true;
  683 +// } catch (Exception e) {
  684 +// e.printStackTrace();
  685 +// return false;
  686 +// }
  687 +// }
  688 +//
  689 +// /**
  690 +// * 将值 value 插入键为 key 的 list 中,并设置时间
  691 +// * @param key 键
  692 +// * @param value 值
  693 +// * @param time 时间
  694 +// * @return true / false
  695 +// */
  696 +// public static boolean lSet(String key, Object value, long time) {
  697 +// if (redisTemplate == null) {
  698 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  699 +// }
  700 +// try {
  701 +// redisTemplate.opsForList().rightPush(key, value);
  702 +// if (time > 0) {
  703 +// expire(key, time);
  704 +// }
  705 +// return true;
  706 +// } catch (Exception e) {
  707 +// e.printStackTrace();
  708 +// return false;
  709 +// }
  710 +// }
  711 +//
  712 +// /**
  713 +// * 将 values 插入键为 key 的 list 中
  714 +// * @param key 键
  715 +// * @param values 值
  716 +// * @return true / false
  717 +// */
  718 +// public static boolean lSetList(String key, List<Object> values) {
  719 +// if (redisTemplate == null) {
  720 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  721 +// }
845 722 // try {
846   -// Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field",
847   -// ScanOptions.scanOptions().match(query).count(1000).build());
848   -// while (cursor.hasNext()) {
849   -// Map.Entry<Object,Object> entry = cursor.next();
850   -// result.add(entry.getKey());
851   -// Object key = entry.getKey();
852   -// Object valueSet = entry.getValue();
  723 +// redisTemplate.opsForList().rightPushAll(key, values);
  724 +// return true;
  725 +// } catch (Exception e) {
  726 +// e.printStackTrace();
  727 +// return false;
  728 +// }
  729 +// }
  730 +//
  731 +// /**
  732 +// * 将 values 插入键为 key 的 list 中,并设置时间
  733 +// * @param key 键
  734 +// * @param values 值
  735 +// * @param time 时间
  736 +// * @return true / false
  737 +// */
  738 +// public static boolean lSetList(String key, List<Object> values, long time) {
  739 +// if (redisTemplate == null) {
  740 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  741 +// }
  742 +// try {
  743 +// redisTemplate.opsForList().rightPushAll(key, values);
  744 +// if (time > 0) {
  745 +// expire(key, time);
853 746 // }
854   -// //关闭cursor
855   -// cursor.close();
  747 +// return true;
  748 +// } catch (Exception e) {
  749 +// e.printStackTrace();
  750 +// return false;
  751 +// }
  752 +// }
  753 +//
  754 +// /**
  755 +// * 根据索引 index 修改键为 key 的值
  756 +// * @param key 键
  757 +// * @param index 索引
  758 +// * @param value 值
  759 +// * @return true / false
  760 +// */
  761 +// public static boolean lUpdateIndex(String key, long index, Object value) {
  762 +// if (redisTemplate == null) {
  763 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  764 +// }
  765 +// try {
  766 +// redisTemplate.opsForList().set(key, index, value);
  767 +// return true;
  768 +// } catch (Exception e) {
  769 +// e.printStackTrace();
  770 +// return false;
  771 +// }
  772 +// }
  773 +//
  774 +// /**
  775 +// * 在键为 key 的 list 中删除值为 value 的元素
  776 +// * @param key 键
  777 +// * @param count 如果 count == 0 则删除 list 中所有值为 value 的元素
  778 +// * 如果 count > 0 则删除 list 中最左边那个值为 value 的元素
  779 +// * 如果 count < 0 则删除 list 中最右边那个值为 value 的元素
  780 +// * @param value
  781 +// * @return
  782 +// */
  783 +// public static long lRemove(String key, long count, Object value) {
  784 +// if (redisTemplate == null) {
  785 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  786 +// }
  787 +// try {
  788 +// return redisTemplate.opsForList().remove(key, count, value);
  789 +// } catch (Exception e) {
  790 +// e.printStackTrace();
  791 +// return 0;
  792 +// }
  793 +// }
  794 +//
  795 +// /**
  796 +// * 在键为 key 的 list中移除第一个元素
  797 +// * @param key 键
  798 +// * @return
  799 +// */
  800 +// public static Object lLeftPop(String key) {
  801 +// if (redisTemplate == null) {
  802 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  803 +// }
  804 +// return redisTemplate.opsForList().leftPop(key);
  805 +// }
  806 +//
  807 +// /**
  808 +// * 在键为 key 的 list中移除、最后一个元素
  809 +// * @param key 键
  810 +// * @return
  811 +// */
  812 +// public static Object lrightPop(String key) {
  813 +// if (redisTemplate == null) {
  814 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  815 +// }
  816 +// return redisTemplate.opsForList().rightPop(key);
  817 +// }
  818 +//
  819 +// /**
  820 +// * 模糊查询
  821 +// * @param key 键
  822 +// * @return true / false
  823 +// */
  824 +// public static List<Object> keys(String key) {
  825 +// if (redisTemplate == null) {
  826 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  827 +// }
  828 +// try {
  829 +// Set<String> set = redisTemplate.keys(key);
  830 +// return new ArrayList<>(set);
856 831 // } catch (Exception e) {
857 832 // e.printStackTrace();
  833 +// return null;
  834 +// }
  835 +// }
  836 +//
  837 +//
  838 +// /**
  839 +// * 模糊查询
  840 +// * @param query 查询参数
  841 +// * @return
  842 +// */
  843 +//// public static List<Object> scan(String query) {
  844 +//// List<Object> result = new ArrayList<>();
  845 +//// try {
  846 +//// Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field",
  847 +//// ScanOptions.scanOptions().match(query).count(1000).build());
  848 +//// while (cursor.hasNext()) {
  849 +//// Map.Entry<Object,Object> entry = cursor.next();
  850 +//// result.add(entry.getKey());
  851 +//// Object key = entry.getKey();
  852 +//// Object valueSet = entry.getValue();
  853 +//// }
  854 +//// //关闭cursor
  855 +//// cursor.close();
  856 +//// } catch (Exception e) {
  857 +//// e.printStackTrace();
  858 +//// }
  859 +//// return result;
  860 +//// }
  861 +//
  862 +// /**
  863 +// * 模糊查询
  864 +// * @param query 查询参数
  865 +// * @return
  866 +// */
  867 +// public static List<Object> scan(String query) {
  868 +// if (redisTemplate == null) {
  869 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
858 870 // }
859   -// return result;
860   -// }
861   -
862   - /**
863   - * 模糊查询
864   - * @param query 查询参数
865   - * @return
866   - */
867   - public static List<Object> scan(String query) {
868   - if (redisTemplate == null) {
869   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
870   - }
871   - Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
872   - ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build();
873   - Cursor<byte[]> scan = connection.scan(scanOptions);
874   - Set<String> keys = new HashSet<>();
875   - while (scan.hasNext()) {
876   - byte[] next = scan.next();
877   - keys.add(new String(next));
878   - }
879   - return keys;
880   - });
881   -
882   - return new ArrayList<>(resultKeys);
883   - }
884   - public static List<Object> scan2(String query) {
885   - if (redisTemplate == null) {
886   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
887   - }
888   - Set<String> keys = redisTemplate.keys(query);
889   - return new ArrayList<>(keys);
890   - }
891   - // ============================== 消息发送与订阅 ==============================
892   - public static void convertAndSend(String channel, JSONObject msg) {
893   - if (redisTemplate == null) {
894   - redisTemplate = SpringBeanFactory.getBean("redisTemplate");
895   - }
896   - redisTemplate.convertAndSend(channel, msg);
897   - }
898   -
899   -}
  871 +// Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
  872 +// ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build();
  873 +// Cursor<byte[]> scan = connection.scan(scanOptions);
  874 +// Set<String> keys = new HashSet<>();
  875 +// while (scan.hasNext()) {
  876 +// byte[] next = scan.next();
  877 +// keys.add(new String(next));
  878 +// }
  879 +// return keys;
  880 +// });
  881 +//
  882 +// return new ArrayList<>(resultKeys);
  883 +// }
  884 +// public static List<Object> scan2(String query) {
  885 +// if (redisTemplate == null) {
  886 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  887 +// }
  888 +// Set<String> keys = redisTemplate.keys(query);
  889 +// return new ArrayList<>(keys);
  890 +// }
  891 +// // ============================== 消息发送与订阅 ==============================
  892 +// public static void convertAndSend(String channel, JSONObject msg) {
  893 +// if (redisTemplate == null) {
  894 +// redisTemplate = SpringBeanFactory.getBean("redisTemplate");
  895 +// }
  896 +// redisTemplate.convertAndSend(channel, msg);
  897 +// }
  898 +//
  899 +//}
... ...