|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.utils.redis; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.SpringBeanFactory; | 
|---|
|  |  |  | import org.springframework.data.redis.core.*; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.*; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @description:Redis工具类 | 
|---|
|  |  |  | * @author: swwheihei | 
|---|
|  |  |  | * @date:   2020年5月6日 下午8:27:29 | 
|---|
|  |  |  | * Redis工具类 | 
|---|
|  |  |  | * @author swwheihei | 
|---|
|  |  |  | * @date 2020年5月6日 下午8:27:29 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | @SuppressWarnings(value = {"rawtypes", "unchecked"}) | 
|---|
|  |  |  | public class RedisUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | static { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 指定缓存失效时间 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @param time 时间(秒) | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean expire(String key, long time) { | 
|---|
|  |  |  | public static boolean expire(String key, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | redisTemplate.expire(key, time, TimeUnit.SECONDS); | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据 key 获取过期时间 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long getExpire(String key) { | 
|---|
|  |  |  | public static long getExpire(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.getExpire(key, TimeUnit.SECONDS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hasKey(String key) { | 
|---|
|  |  |  | public static boolean hasKey(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.hasKey(key); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @SuppressWarnings("unchecked") 忽略类型转换警告 | 
|---|
|  |  |  | * @param key 键(一个或者多个) | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean del(String... key) { | 
|---|
|  |  |  | public static boolean del(String... key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (key != null && key.length > 0) { | 
|---|
|  |  |  | if (key.length == 1) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return 值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object get(String key) { | 
|---|
|  |  |  | public static Object get(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return key == null ? null : redisTemplate.opsForValue().get(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean set(String key, Object value) { | 
|---|
|  |  |  | public static boolean set(String key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForValue().set(key, value); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param time 时间(秒),如果 time < 0 则设置无限时间 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean set(String key, Object value, long time) { | 
|---|
|  |  |  | public static boolean set(String key, Object value, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param delta 递增大小 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long incr(String key, long delta) { | 
|---|
|  |  |  | public static long incr(String key, long delta) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (delta < 0) { | 
|---|
|  |  |  | throw new RuntimeException("递增因子必须大于 0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param delta 递减大小 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long decr(String key, long delta) { | 
|---|
|  |  |  | public static long decr(String key, long delta) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (delta < 0) { | 
|---|
|  |  |  | throw new RuntimeException("递减因子必须大于 0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param item 项(no null) | 
|---|
|  |  |  | * @return 值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object hget(String key, String item) { | 
|---|
|  |  |  | public static Object hget(String key, String item) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForHash().get(key, item); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键(no null) | 
|---|
|  |  |  | * @return 对应的多个键值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Map<Object, Object> hmget(String key) { | 
|---|
|  |  |  | public static Map<Object, Object> hmget(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForHash().entries(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param map 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hmset(String key, Map<Object, Object> map) { | 
|---|
|  |  |  | public static boolean hmset(String key, Map<Object, Object> map) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForHash().putAll(key, map); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param time 时间 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hmset(String key, Map<Object, Object> map, long time) { | 
|---|
|  |  |  | public static boolean hmset(String key, Map<?, ?> map, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForHash().putAll(key, map); | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hset(String key, String item, Object value) { | 
|---|
|  |  |  | public static boolean hset(String key, String item, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForHash().put(key, item, value); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖) | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hset(String key, String item, Object value, long time) { | 
|---|
|  |  |  | public static boolean hset(String key, String item, Object value, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForHash().put(key, item, value); | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @param item 项(可以多个,no null) | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void hdel(String key, Object... item) { | 
|---|
|  |  |  | public static void hdel(String key, Object... item) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.opsForHash().delete(key, item); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param item 值(no null) | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean hHasKey(String key, String item) { | 
|---|
|  |  |  | public static boolean hHasKey(String key, String item) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForHash().hasKey(key, item); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param by 递增大小 > 0 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double hincr(String key, String item, Double by) { | 
|---|
|  |  |  | public static Double hincr(String key, String item, Double by) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForHash().increment(key, item, by); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param by 递减大小 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double hdecr(String key, String item, Double by) { | 
|---|
|  |  |  | public static Double hdecr(String key, String item, Double by) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForHash().increment(key, item, -by); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return 值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<Object> sGet(String key) { | 
|---|
|  |  |  | public static Set<Object> sGet(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForSet().members(key); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean sHasKey(String key, Object value) { | 
|---|
|  |  |  | public static boolean sHasKey(String key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForSet().isMember(key, value); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param values 值(可以多个) | 
|---|
|  |  |  | * @return 成功个数 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long sSet(String key, Object... values) { | 
|---|
|  |  |  | public static long sSet(String key, Object... values) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForSet().add(key, values); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param values 值(可以多个) | 
|---|
|  |  |  | * @return 成功放入个数 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long sSet(String key, long time, Object... values) { | 
|---|
|  |  |  | public static long sSet(String key, long time, Object... values) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | long count = redisTemplate.opsForSet().add(key, values); | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return 长度 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long sGetSetSize(String key) { | 
|---|
|  |  |  | public static long sGetSetSize(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForSet().size(key); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param values 值 | 
|---|
|  |  |  | * @return 成功移除个数 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long setRemove(String key, Object... values) { | 
|---|
|  |  |  | public static long setRemove(String key, Object... values) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForSet().remove(key, values); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param score | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zAdd(Object key, Object value, double score) { | 
|---|
|  |  |  | public static void zAdd(Object key, Object value, double score) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.opsForZSet().add(key, value, score); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zRemove(Object key, Object value) { | 
|---|
|  |  |  | public static void zRemove(Object key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.opsForZSet().remove(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param delta -1 表示减 1 表示加1 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zIncrScore(Object key, Object value, double delta) { | 
|---|
|  |  |  | public static Double zIncrScore(Object key, Object value, double delta) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().incrementScore(key, value, delta); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zScore(Object key, Object value) { | 
|---|
|  |  |  | public static Double zScore(Object key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().score(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zRank(Object key, Object value) { | 
|---|
|  |  |  | public static Long zRank(Object key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rank(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zSize(Object key) { | 
|---|
|  |  |  | public static Long zSize(Object key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().zCard(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<Object> ZRange(Object key, int start, int end) { | 
|---|
|  |  |  | public static Set<Object> zRange(Object key, int start, int end) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().range(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | 
|---|
|  |  |  | public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeWithScores(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zRevRange(Object key, int start, int end) { | 
|---|
|  |  |  | public static Set<String> zRevRange(Object key, int start, int end) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().reverseRange(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param max | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zSortRange(Object key, int min, int max) { | 
|---|
|  |  |  | public static Set<String> zSortRange(Object key, int min, int max) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeByScore(key, min, max); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end 结束(0 到 -1 代表所有值) | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<Object> lGet(String key, long start, long end) { | 
|---|
|  |  |  | public static List<Object> lGet(String key, long start, long end) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForList().range(key, start, end); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return 长度 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long lGetListSize(String key) { | 
|---|
|  |  |  | public static long lGetListSize(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForList().size(key); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | *              当 index < 0 时 {-1:表尾, -2:倒数第二个元素} | 
|---|
|  |  |  | * @return 值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object lGetIndex(String key, long index) { | 
|---|
|  |  |  | public static Object lGetIndex(String key, long index) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForList().index(key, index); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean lSet(String key, Object value) { | 
|---|
|  |  |  | public static boolean lSet(String key, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForList().rightPush(key, value); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param time 时间 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean lSet(String key, Object value, long time) { | 
|---|
|  |  |  | public static boolean lSet(String key, Object value, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForList().rightPush(key, value); | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param values 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean lSetList(String key, List<Object> values) { | 
|---|
|  |  |  | public static boolean lSetList(String key, List<Object> values) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForList().rightPushAll(key, values); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param time 时间 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean lSetList(String key, List<Object> values, long time) { | 
|---|
|  |  |  | public static boolean lSetList(String key, List<Object> values, long time) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForList().rightPushAll(key, values); | 
|---|
|  |  |  | if (time > 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value 值 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean lUpdateIndex(String key, long index, Object value) { | 
|---|
|  |  |  | public static boolean lUpdateIndex(String key, long index, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | redisTemplate.opsForList().set(key, index, value); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public long lRemove(String key, long count, Object value) { | 
|---|
|  |  |  | public static long lRemove(String key, long count, Object value) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | return redisTemplate.opsForList().remove(key, count, value); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 在键为 key 的 list中移除第一个元素 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Object lLeftPop(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForList().leftPop(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 在键为 key 的 list中移除、最后一个元素 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Object lrightPop(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisTemplate.opsForList().rightPop(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 模糊查询 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<Object> keys(String key) { | 
|---|
|  |  |  | public static List<Object> keys(String key) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Set<String> set = redisTemplate.keys(key); | 
|---|
|  |  |  | return new ArrayList<>(set); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param query 查询参数 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | //    public List<Object> scan(String query) { | 
|---|
|  |  |  | //    public static List<Object> scan(String query) { | 
|---|
|  |  |  | //        List<Object> result = new ArrayList<>(); | 
|---|
|  |  |  | //        try { | 
|---|
|  |  |  | //            Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field", | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param query 查询参数 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<Object> scan(String query) { | 
|---|
|  |  |  | Set<String> keys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { | 
|---|
|  |  |  | Set<String> keysTmp = new HashSet<>(); | 
|---|
|  |  |  | Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(query).count(1000).build()); | 
|---|
|  |  |  | while (cursor.hasNext()) { | 
|---|
|  |  |  | keysTmp.add(new String(cursor.next())); | 
|---|
|  |  |  | public static List<Object> scan(String query) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { | 
|---|
|  |  |  | ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); | 
|---|
|  |  |  | Cursor<byte[]> scan = connection.scan(scanOptions); | 
|---|
|  |  |  | Set<String> keys = new HashSet<>(); | 
|---|
|  |  |  | while (scan.hasNext()) { | 
|---|
|  |  |  | byte[] next = scan.next(); | 
|---|
|  |  |  | keys.add(new String(next)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return keysTmp; | 
|---|
|  |  |  | return keys; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | //        Set<String> keys = (Set<String>) redisTemplate.execute(new RedisCallback<Set<String>>(){ | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            @Override | 
|---|
|  |  |  | //            public Set<String> doInRedis(RedisConnection connection) throws DataAccessException { | 
|---|
|  |  |  | //                Set<String> keysTmp = new HashSet<>(); | 
|---|
|  |  |  | //                Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(query).count(1000).build()); | 
|---|
|  |  |  | //                while (cursor.hasNext()) { | 
|---|
|  |  |  | //                    keysTmp.add(new String(cursor.next())); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            return keysTmp; | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return new ArrayList<>(resultKeys); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static List<Object> scan2(String query) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Set<String> keys = redisTemplate.keys(query); | 
|---|
|  |  |  | return new ArrayList<>(keys); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //    ============================== 消息发送与订阅 ============================== | 
|---|
|  |  |  | public static void convertAndSend(String channel, JSONObject msg) { | 
|---|
|  |  |  | if (redisTemplate == null) { | 
|---|
|  |  |  | redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.convertAndSend(channel, msg); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|