diff --git a/src/main/java/cn/axzo/pokonyan/config/redis/RedisConfiguration.java b/src/main/java/cn/axzo/pokonyan/config/redis/RedisConfiguration.java index a6ae4a6..70f916b 100644 --- a/src/main/java/cn/axzo/pokonyan/config/redis/RedisConfiguration.java +++ b/src/main/java/cn/axzo/pokonyan/config/redis/RedisConfiguration.java @@ -3,6 +3,7 @@ package cn.axzo.pokonyan.config.redis; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; @@ -30,8 +31,9 @@ public class RedisConfiguration { /** * RedisTemplate配置 */ - @Bean - public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory, RedisSerializer redisKeySerializer, RedisSerializer redisValueSerializer) { + @Primary + @Bean(name = "genericRedisTemplate") + public StringRedisTemplate genericRedisTemplate(RedisConnectionFactory factory, RedisSerializer redisKeySerializer, RedisSerializer redisValueSerializer) { StringRedisTemplate redisTemplate = new StringRedisTemplate(); redisTemplate.setConnectionFactory(factory); @@ -48,4 +50,9 @@ public class RedisConfiguration { return redisTemplate; } + @Bean(name = "stringRedisTemplate") + public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) { + return new StringRedisTemplate(factory); + } + } diff --git a/src/main/java/cn/axzo/pokonyan/config/redis/RedisUtil.java b/src/main/java/cn/axzo/pokonyan/config/redis/RedisUtil.java index 4c40988..d2fc2ff 100644 --- a/src/main/java/cn/axzo/pokonyan/config/redis/RedisUtil.java +++ b/src/main/java/cn/axzo/pokonyan/config/redis/RedisUtil.java @@ -1,14 +1,11 @@ package cn.axzo.pokonyan.config.redis; -import cn.hutool.core.util.NumberUtil; import cn.hutool.extra.spring.SpringUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.InitializingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.data.redis.RedisSystemException; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.connection.RedisConnection; @@ -17,14 +14,12 @@ import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.connection.jedis.JedisConnection; import org.springframework.data.redis.connection.lettuce.LettuceConnection; import org.springframework.data.redis.core.Cursor; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; import org.springframework.data.redis.core.types.Expiration; import org.springframework.data.redis.serializer.SerializationException; import org.springframework.data.redis.serializer.StringRedisSerializer; -import org.springframework.stereotype.Component; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -40,13 +35,15 @@ import java.util.concurrent.TimeUnit; public class RedisUtil implements InitializingBean { /** 使用StringRedisTemplate(,其是RedisTemplate的定制化升级) */ - private static StringRedisTemplate redisTemplate; + private static StringRedisTemplate genericRedisTemplate; + private static StringRedisTemplate stringRedisTemplate; @Override public void afterPropertiesSet() throws Exception { - RedisUtil.redisTemplate = SpringUtil.getBean(StringRedisTemplate.class); - redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer(){ + RedisUtil.genericRedisTemplate = SpringUtil.getBean("genericRedisTemplate", StringRedisTemplate.class); + RedisUtil.stringRedisTemplate = SpringUtil.getBean("stringRedisTemplate", StringRedisTemplate.class); + genericRedisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer(){ @Override public byte[] serialize(Object object) throws SerializationException { return super.serialize(object); @@ -91,7 +88,7 @@ public class RedisUtil implements InitializingBean { public static Boolean delete(String key) { log.info("delete(...) => key= {}", key); // 返回值只可能为true/false, 不可能为null - Boolean result = redisTemplate.delete(key); + Boolean result = genericRedisTemplate.delete(key); log.info("delete(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -113,7 +110,7 @@ public class RedisUtil implements InitializingBean { */ public static Long delete(Collection keys) { log.info("delete(...) => keys= {}", keys); - Long count = redisTemplate.delete(keys); + Long count = genericRedisTemplate.delete(keys); log.info("delete(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -135,7 +132,7 @@ public class RedisUtil implements InitializingBean { */ public static byte[] dump(String key) { log.info("dump(...) =>key= {}", key); - byte[] result = redisTemplate.dump(key); + byte[] result = genericRedisTemplate.dump(key); log.info("dump(...) => result= {}", result); return result; } @@ -181,10 +178,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/8 11:36:45 */ public static void restore(String key, byte[] value, Long timeout, TimeUnit unit, - Boolean replace) { + Boolean replace) { log.info("restore(...) => key= {}, value= {}, timeout= {}, unit= {}, replace= {}", key, value, timeout, unit, replace); - redisTemplate.restore(key, value, timeout, unit, replace); + genericRedisTemplate.restore(key, value, timeout, unit, replace); } /** @@ -196,7 +193,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean hasKey(String key) { log.info("hasKey(...) => key= {}", key); - Boolean result = redisTemplate.hasKey(key); + Boolean result = genericRedisTemplate.hasKey(key); log.info("hasKey(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -221,7 +218,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean expire(String key, Long timeout, TimeUnit unit) { log.info("expire(...) => key= {}, timeout= {}, unit= {}", key, timeout, unit); - Boolean result = redisTemplate.expire(key, timeout, unit); + Boolean result = genericRedisTemplate.expire(key, timeout, unit); log.info("expire(...) => result is= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -245,7 +242,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean expireAt(String key, Date date) { log.info("expireAt(...) => key= {}, date= {}", key, date); - Boolean result = redisTemplate.expireAt(key, date); + Boolean result = genericRedisTemplate.expireAt(key, date); log.info("expireAt(...) => result is= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -269,7 +266,7 @@ public class RedisUtil implements InitializingBean { */ public static Set keys(String pattern) { log.info("keys(...) => pattern= {}", pattern); - Set keys = redisTemplate.keys(pattern); + Set keys = genericRedisTemplate.keys(pattern); log.info("keys(...) => keys= {}", keys); return keys; } @@ -291,7 +288,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean move(String key, int dbIndex) { log.info("move(...) => key = {}, dbIndex= {}", key, dbIndex); - Boolean result = redisTemplate.move(key, dbIndex); + Boolean result = genericRedisTemplate.move(key, dbIndex); log.info("move(...) =>result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -312,7 +309,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean persist(String key) { log.info("persist(...) => key= {}", key); - Boolean result = redisTemplate.persist(key); + Boolean result = genericRedisTemplate.persist(key); log.info("persist(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -351,7 +348,7 @@ public class RedisUtil implements InitializingBean { */ public static Long getExpire(String key, TimeUnit unit) { log.info("getExpire(...) =>key= {}, unit is= {}", key, unit); - Long result = redisTemplate.getExpire(key, unit); + Long result = genericRedisTemplate.getExpire(key, unit); log.info("getExpire(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -368,7 +365,7 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/8 14:11:43 */ public static String randomKey() { - String result = redisTemplate.randomKey(); + String result = genericRedisTemplate.randomKey(); log.info("randomKey(...) => result is= {}", result); return result; } @@ -393,7 +390,7 @@ public class RedisUtil implements InitializingBean { */ public static void rename(String oldKey, String newKey) { log.info("rename(...) => oldKey= {}, newKey= {}", oldKey, newKey); - redisTemplate.rename(oldKey, newKey); + genericRedisTemplate.rename(oldKey, newKey); } /** @@ -412,7 +409,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean renameIfAbsent(String oldKey, String newKey) { log.info("renameIfAbsent(...) => oldKey= {}, newKey= {}", oldKey, newKey); - Boolean result = redisTemplate.renameIfAbsent(oldKey, newKey); + Boolean result = genericRedisTemplate.renameIfAbsent(oldKey, newKey); log.info("renameIfAbsent(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -432,7 +429,7 @@ public class RedisUtil implements InitializingBean { */ public static DataType type(String key) { log.info("type(...) => key= {}", key); - DataType result = redisTemplate.type(key); + DataType result = genericRedisTemplate.type(key); log.info("type(...) => result= {}", result); return result; } @@ -462,7 +459,7 @@ public class RedisUtil implements InitializingBean { */ public static void set(String key, String value) { log.info("set(...) => key= {}, value= {}", key, value); - redisTemplate.opsForValue().set(key, value); + genericRedisTemplate.opsForValue().set(key, value); } /** @@ -491,7 +488,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean setBit(String key, Long offset, Boolean value) { log.info("setBit(...) => key= {}, offset= {}, value= {}", key, offset, value); - Boolean result = redisTemplate.opsForValue().setBit(key, offset, value); + Boolean result = genericRedisTemplate.opsForValue().setBit(key, offset, value); log.info("setBit(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -517,7 +514,7 @@ public class RedisUtil implements InitializingBean { public static void setEx(String key, String value, Long timeout, TimeUnit unit) { log.info("setEx(...) => key= {}, value= {}, timeout= {}, unit= {}", key, value, timeout, unit); - redisTemplate.opsForValue().set(key, value, timeout, unit); + genericRedisTemplate.opsForValue().set(key, value, timeout, unit); } /** @@ -534,7 +531,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean setIfAbsent(String key, String value) { log.info("setIfAbsent(...) => key= {}, value= {}", key, value); - Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value); + Boolean result = genericRedisTemplate.opsForValue().setIfAbsent(key, value); log.info("setIfAbsent(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -561,7 +558,7 @@ public class RedisUtil implements InitializingBean { public static Boolean setIfAbsent(String key, String value, Long timeout, TimeUnit unit) { log.info("setIfAbsent(...) => key= {}, value= {}, key= {}, value= {}", key, value, timeout, unit); - Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit); + Boolean result = genericRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit); log.info("setIfAbsent(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -594,7 +591,7 @@ public class RedisUtil implements InitializingBean { */ public static void setRange(String key, String replaceValue, Long offset) { log.info("setRange(...) => key= {}, replaceValue= {}, offset= {}", key, replaceValue, offset); - redisTemplate.opsForValue().set(key, replaceValue, offset); + genericRedisTemplate.opsForValue().set(key, replaceValue, offset); } /** @@ -610,7 +607,7 @@ public class RedisUtil implements InitializingBean { */ public static Long size(String key) { log.info("size(...) => key= {}", key); - Long result = redisTemplate.opsForValue().size(key); + Long result = genericRedisTemplate.opsForValue().size(key); log.info("size(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -629,7 +626,7 @@ public class RedisUtil implements InitializingBean { */ public static void multiSet(Map maps) { log.info("multiSet(...) => maps= {}", maps); - redisTemplate.opsForValue().multiSet(maps); + genericRedisTemplate.opsForValue().multiSet(maps); } /** @@ -652,7 +649,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean multiSetIfAbsent(Map maps) { log.info("multiSetIfAbsent(...) => maps= {}", maps); - Boolean result = redisTemplate.opsForValue().multiSetIfAbsent(maps); + Boolean result = genericRedisTemplate.opsForValue().multiSetIfAbsent(maps); log.info("multiSetIfAbsent(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -677,7 +674,7 @@ public class RedisUtil implements InitializingBean { */ public static Long incrBy(String key, Long increment) { log.info("incrBy(...) => key= {}, increment= {}", key, increment); - Long result = redisTemplate.opsForValue().increment(key, increment); + Long result = genericRedisTemplate.opsForValue().increment(key, increment); log.info("incrBy(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -706,7 +703,7 @@ public class RedisUtil implements InitializingBean { */ public static Double incrByFloat(String key, Double increment) { log.info("incrByFloat(...) => key= {}, increment= {}", key, increment); - Double result = redisTemplate.opsForValue().increment(key, increment); + Double result = genericRedisTemplate.opsForValue().increment(key, increment); log.info("incrByFloat(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -728,7 +725,7 @@ public class RedisUtil implements InitializingBean { */ public static Integer append(String key, String value) { log.info("append(...) => key= {}, value= {}", key, value); - Integer result = redisTemplate.opsForValue().append(key, value); + Integer result = genericRedisTemplate.opsForValue().append(key, value); log.info("append(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -748,7 +745,7 @@ public class RedisUtil implements InitializingBean { */ public static String get(String key) { log.info("get(...) => key= {}", key); - String result = redisTemplate.opsForValue().get(key); + String result = genericRedisTemplate.opsForValue().get(key); log.info("get(...) => result= {} ", result); return result; } @@ -770,7 +767,7 @@ public class RedisUtil implements InitializingBean { */ public static String getRange(String key, Long start, Long end) { log.info("getRange(...) => kry= {}", key); - String result = redisTemplate.opsForValue().get(key, start, end); + String result = genericRedisTemplate.opsForValue().get(key, start, end); log.info("getRange(...) => result= {} ", result); return result; } @@ -789,7 +786,7 @@ public class RedisUtil implements InitializingBean { */ public static String getAndSet(String key, String newValue) { log.info("getAndSet(...) => key= {}, value= {}", key, newValue); - String oldValue = redisTemplate.opsForValue().getAndSet(key, newValue); + String oldValue = genericRedisTemplate.opsForValue().getAndSet(key, newValue); log.info("getAndSet(...) => oldValue= {}", oldValue); return oldValue; } @@ -813,7 +810,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean getBit(String key, Long offset) { log.info("getBit(...) => key= {}, offset= {}", key, offset); - Boolean result = redisTemplate.opsForValue().getBit(key, offset); + Boolean result = genericRedisTemplate.opsForValue().getBit(key, offset); log.info("getBit(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -833,12 +830,405 @@ public class RedisUtil implements InitializingBean { */ public static List multiGet(Collection keys) { log.info("multiGet(...) => keys= {}", keys); - List result = redisTemplate.opsForValue().multiGet(keys); + List result = genericRedisTemplate.opsForValue().multiGet(keys); log.info("multiGet(...) => result= {}", result); return result; } } + public static class StringValueOps { + + /** + * 设置key-value + * + * 注: 若已存在相同的key, 那么原来的key-value会被丢弃。 + * + * @param key + * key + * @param value + * key对应的value + * @date 2020/3/8 15:40:59 + */ + public static void set(String key, String value) { + log.info("set(...) => key= {}, value= {}", key, value); + stringRedisTemplate.opsForValue().set(key, value); + } + + /** + * 处理redis中key对应的value值, 将第offset位的值, 设置为1或0。 + * + * 说明: 在redis中,存储的字符串都是以二级制的进行存在的; 如存储的key-value里,值为abc,实际上, + * 在redis里面存储的是011000010110001001100011,前8为对应a,中间8为对应b,后面8位对应c。 + * 示例:这里如果setBit(key, 6, true)的话,就是将索引位置6的那个数,设置值为1,值就变成 + * 了011000110110001001100011 + * 追注:offset即index,从0开始。 + * + * 注: 参数value为true, 则设置为1;参数value为false, 则设置为0。 + * + * 注: 若redis中不存在对应的key,那么会自动创建新的。 + * 注: offset可以超过value在二进制下的索引长度。 + * + * @param key + * 定位value的key + * @param offset + * 要改变的bit的索引 + * @param value + * 改为1或0, true - 改为1, false - 改为0 + * + * @return set是否成功 + * @date 2020/3/8 16:30:37 + */ + public static Boolean setBit(String key, Long offset, Boolean value) { + log.info("setBit(...) => key= {}, offset= {}, value= {}", key, offset, value); + Boolean result = stringRedisTemplate.opsForValue().setBit(key, offset, value); + log.info("setBit(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 设置key-value + * + * 注: 若已存在相同的key, 那么原来的key-value会被丢弃 + * + * @param key + * key + * @param value + * key对应的value + * @param timeout + * 过时时长 + * @param unit + * timeout的单位 + * @date 2020/3/8 15:40:59 + */ + public static void setEx(String key, String value, Long timeout, TimeUnit unit) { + log.info("setEx(...) => key= {}, value= {}, timeout= {}, unit= {}", + key, value, timeout, unit); + stringRedisTemplate.opsForValue().set(key, value, timeout, unit); + } + + /** + * 若不存在key时, 向redis中添加key-value, 返回成功/失败。 + * 若存在,则不作任何操作, 返回false。 + * + * @param key + * key + * @param value + * key对应的value + * + * @return set是否成功 + * @date 2020/3/8 16:51:36 + */ + public static Boolean setIfAbsent(String key, String value) { + log.info("setIfAbsent(...) => key= {}, value= {}", key, value); + Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(key, value); + log.info("setIfAbsent(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 若不存在key时, 向redis中添加一个(具有超时时长的)key-value, 返回成功/失败。 + * 若存在,则不作任何操作, 返回false。 + * + * @param key + * key + * @param value + * key对应的value + * @param timeout + * 超时时长 + * @param unit + * timeout的单位 + * + * @return set是否成功 + * @date 2020/3/8 16:51:36 + */ + public static Boolean setIfAbsent(String key, String value, Long timeout, TimeUnit unit) { + log.info("setIfAbsent(...) => key= {}, value= {}, key= {}, value= {}", key, value, timeout, + unit); + Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit); + log.info("setIfAbsent(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 从(redis中key对应的)value的offset位置起(包含该位置),用replaceValue替换对应长度的值。 + * + * 举例说明: + * 1.假设redis中存在key-value ("ds", "0123456789"); 调 + * 用setRange("ds", "abcdefghijk", 3)后, redis中该value值就变为了[012abcdefghijk] + * + * 2.假设redis中存在key-value ("jd", "0123456789");调 + * 用setRange("jd", "xyz", 3)后, redis中该value值就变为了[012xyz6789] + * + * 3.假设redis中存在key-value ("ey", "0123456789");调 + * 用setRange("ey", "qwer", 15)后, redis中该value值就变为了[0123456789 qwer] + * 注:case3比较特殊,offset超过了原value的长度了, 中间就会有一些空格来填充,但是如果在程序 + * 中直接输出的话,中间那部分空格可能会出现乱码。 + * + * @param key + * 定位key-value的key + * @param replaceValue + * 要替换的值 + * @param offset + * 起始位置 + * @date 2020/3/8 17:04:31 + */ + public static void setRange(String key, String replaceValue, Long offset) { + log.info("setRange(...) => key= {}, replaceValue= {}, offset= {}", key, replaceValue, offset); + stringRedisTemplate.opsForValue().set(key, replaceValue, offset); + } + + /** + * 获取到key对应的value的长度。 + * + * 注: 长度等于{@link String#length}。 + * 注: 若redis中不存在对应的key-value, 则返回值为0. + * + * @param key + * 定位value的key + * @return value的长度 + * @date 2020/3/8 17:14:30 + */ + public static Long size(String key) { + log.info("size(...) => key= {}", key); + Long result = stringRedisTemplate.opsForValue().size(key); + log.info("size(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 批量设置 key-value + * + * 注: 若存在相同的key, 则原来的key-value会被丢弃。 + * + * @param maps + * key-value 集 + * @date 2020/3/8 17:21:19 + */ + public static void multiSet(Map maps) { + log.info("multiSet(...) => maps= {}", maps); + stringRedisTemplate.opsForValue().multiSet(maps); + } + + /** + * 当redis中,不存在任何一个keys时, 才批量设置 key-value, 并返回成功/失败. + * 否者,不进行任何操作, 并返回false。 + * + * 即: 假设调用此方法时传入的参数map是这样的: {k1=v1, k2=v2, k3=v3} + * 那么redis中, k1、k2、k3都不存在时,才会批量设置key-value; + * 否则不会设置任何key-value。 + * + * 注: 若存在相同的key, 则原来的key-value会被丢弃。 + * + * 注: + * + * @param maps + * key-value 集 + * + * @return 操作是否成功 + * @date 2020/3/8 17:21:19 + */ + public static Boolean multiSetIfAbsent(Map maps) { + log.info("multiSetIfAbsent(...) => maps= {}", maps); + Boolean result = stringRedisTemplate.opsForValue().multiSetIfAbsent(maps); + log.info("multiSetIfAbsent(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 增/减 整数 + * + * 注: 负数则为减。 + * 注: 若key对应的value值不支持增/减操作(即: value不是数字), 那么会 + * 抛出org.springframework.data.redis.RedisSystemException + * + * @param key + * 用于定位value的key + * @param increment + * 增加多少 + * @return 增加后的总值。 + * @throws RedisSystemException key对应的value值不支持增/减操作时 + * @date 2020/3/8 17:45:51 + */ + public static Long incrBy(String key, Long increment) { + log.info("incrBy(...) => key= {}, increment= {}", key, increment); + Long result = stringRedisTemplate.opsForValue().increment(key, increment); + log.info("incrBy(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 增/减 浮点数 + * + * 注: 慎用浮点数,会有精度问题。 + * 如: 先 RedisUtil.StringOps.set("ds", "123"); + * 然后再RedisUtil.StringOps.incrByFloat("ds", 100.6); + * 就会看到精度问题。 + * 注: 负数则为减。 + * 注: 若key对应的value值不支持增/减操作(即: value不是数字), 那么会 + * 抛出org.springframework.data.redis.RedisSystemException + * + * @param key + * 用于定位value的key + * @param increment + * 增加多少 + * @return 增加后的总值。 + * @throws RedisSystemException key对应的value值不支持增/减操作时 + * @date 2020/3/8 17:45:51 + */ + public static Double incrByFloat(String key, Double increment) { + log.info("incrByFloat(...) => key= {}, increment= {}", key, increment); + Double result = stringRedisTemplate.opsForValue().increment(key, increment); + log.info("incrByFloat(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 追加值到末尾 + * + * 注: 当redis中原本不存在key时,那么(从效果上来看)此方法就等价于{@link this#set(String, String)} + * + * @param key + * 定位value的key + * @param value + * 要追加的value值 + * @return 追加后, 整个value的长度 + * @date 2020/3/8 17:59:21 + */ + public static Integer append(String key, String value) { + log.info("append(...) => key= {}, value= {}", key, value); + Integer result = stringRedisTemplate.opsForValue().append(key, value); + log.info("append(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 根据key,获取到对应的value值 + * + * @param key + * key-value对应的key + * @return 该key对应的值。 + * 注: 若key不存在, 则返回null。 + * + * @date 2020/3/8 16:27:41 + */ + public static String get(String key) { + log.info("get(...) => key= {}", key); + String result = stringRedisTemplate.opsForValue().get(key); + log.info("get(...) => result= {} ", result); + return result; + } + + /** + * 对(key对应的)value进行截取, 截取范围为[start, end] + * + * 注: 若[start, end]的范围不在value的范围中,那么返回的是空字符串 "" + * 注: 若value只有一部分在[start, end]的范围中,那么返回的是value对应部分的内容(即:不足的地方,并不会以空来填充) + * + * @param key + * 定位value的key + * @param start + * 起始位置 (从0开始) + * @param end + * 结尾位置 (从0开始) + * @return 截取后的字符串 + * @date 2020/3/8 18:08:45 + */ + public static String getRange(String key, Long start, Long end) { + log.info("getRange(...) => kry= {}", key); + String result = stringRedisTemplate.opsForValue().get(key, start, end); + log.info("getRange(...) => result= {} ", result); + return result; + } + + /** + * 给指定key设置新的value, 并返回旧的value + * + * 注: 若redis中不存在key, 那么此操作仍然可以成功, 不过返回的旧值是null + * + * @param key + * 定位value的key + * @param newValue + * 要为该key设置的新的value值 + * @return 旧的value值 + * @date 2020/3/8 18:14:24 + */ + public static String getAndSet(String key, String newValue) { + log.info("getAndSet(...) => key= {}, value= {}", key, newValue); + String oldValue = stringRedisTemplate.opsForValue().getAndSet(key, newValue); + log.info("getAndSet(...) => oldValue= {}", oldValue); + return oldValue; + } + + /** + * 获取(key对应的)value在二进制下,offset位置的bit值。 + * + * 注: 当offset的值在(二进制下的value的)索引范围外时, 返回的也是false。 + * + * 示例: + * RedisUtil.StringOps.set("akey", "a"); + * 字符串a, 转换为二进制为01100001 + * 那么getBit("akey", 6)获取到的结果为false。 + * + * @param key + * 定位value的key + * @param offset + * 定位bit的索引 + * @return offset位置对应的bit的值(true - 1, false - 0) + * @date 2020/3/8 18:21:10 + */ + public static Boolean getBit(String key, Long offset) { + log.info("getBit(...) => key= {}, offset= {}", key, offset); + Boolean result = stringRedisTemplate.opsForValue().getBit(key, offset); + log.info("getBit(...) => result= {}", result); + if (result == null) { + throw new RedisOpsResultIsNullException(); + } + return result; + } + + /** + * 批量获取value值 + * + * 注: 若redis中,对应的key不存在,那么该key对应的返回的value值为null + * + * @param keys + * key集 + * @return value值集合 + * @date 2020/3/8 18:26:33 + */ + public static List multiGet(Collection keys) { + log.info("multiGet(...) => keys= {}", keys); + List result = stringRedisTemplate.opsForValue().multiGet(keys); + log.info("multiGet(...) => result= {}", result); + return result; + } + } + + /** * hash相关操作 * @@ -868,7 +1258,7 @@ public class RedisUtil implements InitializingBean { */ public static void hPut(String key, String entryKey, String entryValue) { log.info("hPut(...) => key= {}, entryKey= {}, entryValue= {}", key, entryKey, entryValue); - redisTemplate.opsForHash().put(key, entryKey, entryValue); + genericRedisTemplate.opsForHash().put(key, entryKey, entryValue); } /** @@ -885,7 +1275,7 @@ public class RedisUtil implements InitializingBean { */ public static void hPutAll(String key, Map maps) { log.info("hPutAll(...) => key= {}, maps= {}", key, maps); - redisTemplate.opsForHash().putAll(key, maps); + genericRedisTemplate.opsForHash().putAll(key, maps); } /** @@ -905,7 +1295,7 @@ public class RedisUtil implements InitializingBean { public static Boolean hPutIfAbsent(String key, String entryKey, String entryValue) { log.info("hPutIfAbsent(...) => key= {}, entryKey= {}, entryValue= {}", key, entryKey, entryValue); - Boolean result = redisTemplate.opsForHash().putIfAbsent(key, entryKey, entryValue); + Boolean result = genericRedisTemplate.opsForHash().putIfAbsent(key, entryKey, entryValue); log.info("hPutIfAbsent(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -929,7 +1319,7 @@ public class RedisUtil implements InitializingBean { */ public static Object hGet(String key, String entryKey) { log.info("hGet(...) => key= {}, entryKey= {}", key, entryKey); - Object entryValue = redisTemplate.opsForHash().get(key, entryKey); + Object entryValue = genericRedisTemplate.opsForHash().get(key, entryKey); log.info("hGet(...) => entryValue= {}", entryValue); return entryValue; } @@ -947,7 +1337,7 @@ public class RedisUtil implements InitializingBean { */ public static Map hGetAll(String key) { log.info("hGetAll(...) => key= {}", key); - Map result = redisTemplate.opsForHash().entries(key); + Map result = genericRedisTemplate.opsForHash().entries(key); log.info("hGetAll(...) => result= {}", result); return result; } @@ -968,7 +1358,7 @@ public class RedisUtil implements InitializingBean { */ public static List hMultiGet(String key, Collection entryKeys) { log.info("hMultiGet(...) => key= {}, entryKeys= {}", key, entryKeys); - List entryValues = redisTemplate.opsForHash().multiGet(key, entryKeys); + List entryValues = genericRedisTemplate.opsForHash().multiGet(key, entryKeys); log.info("hMultiGet(...) => entryValues= {}", entryValues); return entryValues; } @@ -995,7 +1385,7 @@ public class RedisUtil implements InitializingBean { */ public static Long hDelete(String key, Object... entryKeys) { log.info("hDelete(...) => key= {}, entryKeys= {}", key, entryKeys); - Long count = redisTemplate.opsForHash().delete(key, entryKeys); + Long count = genericRedisTemplate.opsForHash().delete(key, entryKeys); log.info("hDelete(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -1019,7 +1409,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean hExists(String key, String entryKey) { log.info("hDelete(...) => key= {}, entryKeys= {}", key, entryKey); - Boolean exist = redisTemplate.opsForHash().hasKey(key, entryKey); + Boolean exist = genericRedisTemplate.opsForHash().hasKey(key, entryKey); log.info("hDelete(...) => exist= {}", exist); return exist; } @@ -1046,7 +1436,7 @@ public class RedisUtil implements InitializingBean { public static Long hIncrBy(String key, Object entryKey, Long increment) { log.info("hIncrBy(...) => key= {}, entryKey= {}, increment= {}", key, entryKey, increment); - Long result = redisTemplate.opsForHash().increment(key, entryKey, increment); + Long result = genericRedisTemplate.opsForHash().increment(key, entryKey, increment); log.info("hIncrBy(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -1078,7 +1468,7 @@ public class RedisUtil implements InitializingBean { public static Double hIncrByFloat(String key, Object entryKey, Double increment) { log.info("hIncrByFloat(...) => key= {}, entryKey= {}, increment= {}", key, entryKey, increment); - Double result = redisTemplate.opsForHash().increment(key, entryKey, increment); + Double result = genericRedisTemplate.opsForHash().increment(key, entryKey, increment); log.info("hIncrByFloat(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -1099,7 +1489,7 @@ public class RedisUtil implements InitializingBean { */ public static Set hKeys(String key) { log.info("hKeys(...) => key= {}", key); - Set entryKeys = redisTemplate.opsForHash().keys(key); + Set entryKeys = genericRedisTemplate.opsForHash().keys(key); log.info("hKeys(...) => entryKeys= {}", entryKeys); return entryKeys; } @@ -1117,7 +1507,7 @@ public class RedisUtil implements InitializingBean { */ public static List hValues(String key) { log.info("hValues(...) => key= {}", key); - List entryValues = redisTemplate.opsForHash().values(key); + List entryValues = genericRedisTemplate.opsForHash().values(key); log.info("hValues(...) => entryValues= {}", entryValues); return entryValues; } @@ -1135,7 +1525,7 @@ public class RedisUtil implements InitializingBean { */ public static Long hSize(String key) { log.info("hSize(...) => key= {}", key); - Long count = redisTemplate.opsForHash().size(key); + Long count = genericRedisTemplate.opsForHash().size(key); log.info("hSize(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -1145,7 +1535,7 @@ public class RedisUtil implements InitializingBean { /** * 根据options匹配到(key对应的)hash中的对应的entryKey, 并返回对应的entry集 - * + * * * 注: ScanOptions实例的创建方式举例: * 1、ScanOptions.NONE @@ -1157,7 +1547,7 @@ public class RedisUtil implements InitializingBean { * 匹配entryKey的条件 * 注: ScanOptions.NONE表示全部匹配。 * 注: ScanOptions.scanOptions().match(pattern).build()表示按照pattern匹配, - * 其中pattern中可以使用通配符 * ? 等, + * 其中pattern中可以使用通配符 * ? 等, * * 表示>=0个字符 * ? 表示有且只有一个字符 * 此处的匹配规则与{@link KeyOps#keys(String)}处的一样。 @@ -1167,7 +1557,7 @@ public class RedisUtil implements InitializingBean { */ public static Cursor> hScan(String key, ScanOptions options) { log.info("hScan(...) => key= {}, options= {}", key, JSON.toJSONString(options)); - Cursor> cursor = redisTemplate.opsForHash().scan(key, options); + Cursor> cursor = genericRedisTemplate.opsForHash().scan(key, options); log.info("hScan(...) => cursor= {}", JSON.toJSONString(cursor)); return cursor; } @@ -1207,7 +1597,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lLeftPush(String key, String item) { log.info("lLeftPush(...) => key= {}, item= {}", key, item); - Long size = redisTemplate.opsForList().leftPush(key, item); + Long size = genericRedisTemplate.opsForList().leftPush(key, item); log.info("lLeftPush(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1231,7 +1621,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lLeftPushAll(String key, String... items) { log.info("lLeftPushAll(...) => key= {}, items= {}", key, items); - Long size = redisTemplate.opsForList().leftPushAll(key, items); + Long size = genericRedisTemplate.opsForList().leftPushAll(key, items); log.info("lLeftPushAll(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1255,7 +1645,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lLeftPushAll(String key, Collection items) { log.info("lLeftPushAll(...) => key= {}, items= {}", key, items); - Long size = redisTemplate.opsForList().leftPushAll(key, items); + Long size = genericRedisTemplate.opsForList().leftPushAll(key, items); log.info("lLeftPushAll(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1277,7 +1667,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lLeftPushIfPresent(String key, String item) { log.info("lLeftPushIfPresent(...) => key= {}, item= {}", key, item); - Long size = redisTemplate.opsForList().leftPushIfPresent(key, item); + Long size = genericRedisTemplate.opsForList().leftPushIfPresent(key, item); log.info("lLeftPushIfPresent(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1301,7 +1691,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lLeftPush(String key, String pivot, String item) { log.info("lLeftPush(...) => key= {}, pivot= {}, item= {}", key, pivot, item); - Long size = redisTemplate.opsForList().leftPush(key, pivot, item); + Long size = genericRedisTemplate.opsForList().leftPush(key, pivot, item); log.info("lLeftPush(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1314,7 +1704,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRightPush(String key, String item) { log.info("lRightPush(...) => key= {}, item= {}", key, item); - Long size = redisTemplate.opsForList().rightPush(key, item); + Long size = genericRedisTemplate.opsForList().rightPush(key, item); log.info("lRightPush(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1327,7 +1717,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRightPushAll(String key, String... items) { log.info("lRightPushAll(...) => key= {}, items= {}", key, items); - Long size = redisTemplate.opsForList().rightPushAll(key, items); + Long size = genericRedisTemplate.opsForList().rightPushAll(key, items); log.info("lRightPushAll(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1340,7 +1730,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRightPushAll(String key, Collection items) { log.info("lRightPushAll(...) => key= {}, items= {}", key, items); - Long size = redisTemplate.opsForList().rightPushAll(key, items); + Long size = genericRedisTemplate.opsForList().rightPushAll(key, items); log.info("lRightPushAll(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1353,7 +1743,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRightPushIfPresent(String key, String item) { log.info("lRightPushIfPresent(...) => key= {}, item= {}", key, item); - Long size = redisTemplate.opsForList().rightPushIfPresent(key, item); + Long size = genericRedisTemplate.opsForList().rightPushIfPresent(key, item); log.info("lRightPushIfPresent(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1366,7 +1756,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRightPush(String key, String pivot, String item) { log.info("lLeftPush(...) => key= {}, pivot= {}, item= {}", key, pivot, item); - Long size = redisTemplate.opsForList().rightPush(key, pivot, item); + Long size = genericRedisTemplate.opsForList().rightPush(key, pivot, item); log.info("lLeftPush(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1388,7 +1778,7 @@ public class RedisUtil implements InitializingBean { */ public static String lLeftPop(String key) { log.info("lLeftPop(...) => key= {}", key); - String item = redisTemplate.opsForList().leftPop(key); + String item = genericRedisTemplate.opsForList().leftPop(key); log.info("lLeftPop(...) => item= {}", item); return item; } @@ -1414,7 +1804,7 @@ public class RedisUtil implements InitializingBean { */ public static String lLeftPop(String key, Long timeout, TimeUnit unit) { log.info("lLeftPop(...) => key= {}, timeout= {}, unit= {}", key, timeout, unit); - String item = redisTemplate.opsForList().leftPop(key, timeout, unit); + String item = genericRedisTemplate.opsForList().leftPop(key, timeout, unit); log.info("lLeftPop(...) => item= {}", item); return item; } @@ -1424,7 +1814,7 @@ public class RedisUtil implements InitializingBean { */ public static String lRightPop(String key) { log.info("lRightPop(...) => key= {}", key); - String item = redisTemplate.opsForList().rightPop(key); + String item = genericRedisTemplate.opsForList().rightPop(key); log.info("lRightPop(...) => item= {}", item); return item; } @@ -1434,7 +1824,7 @@ public class RedisUtil implements InitializingBean { */ public static String lRightPop(String key, Long timeout, TimeUnit unit) { log.info("lRightPop(...) => key= {}, timeout= {}, unit= {}", key, timeout, unit); - String item = redisTemplate.opsForList().rightPop(key, timeout, unit); + String item = genericRedisTemplate.opsForList().rightPop(key, timeout, unit); log.info("lRightPop(...) => item= {}", item); return item; } @@ -1460,7 +1850,7 @@ public class RedisUtil implements InitializingBean { public static String lRightPopAndLeftPush(String sourceKey, String destinationKey) { log.info("lRightPopAndLeftPush(...) => sourceKey= {}, destinationKey= {}", sourceKey, destinationKey); - String item = redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey); + String item = genericRedisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey); log.info("lRightPopAndLeftPush(...) => item= {}", item); return item; } @@ -1489,10 +1879,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/9 15:06:59 */ public static String lRightPopAndLeftPush(String sourceKey, String destinationKey, Long timeout, - TimeUnit unit) { + TimeUnit unit) { log.info("lRightPopAndLeftPush(...) => sourceKey= {}, destinationKey= {}, timeout= {}," + " unit= {}", sourceKey, destinationKey, timeout, unit); - String item = redisTemplate.opsForList() + String item = genericRedisTemplate.opsForList() .rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit); log.info("lRightPopAndLeftPush(...) => item= {}", item); return item; @@ -1514,7 +1904,7 @@ public class RedisUtil implements InitializingBean { */ public static void lSet(String key, Long index, String item) { log.info("lSet(...) => key= {}, index= {}, item= {}", key, index, item); - redisTemplate.opsForList().set(key, index, item); + genericRedisTemplate.opsForList().set(key, index, item); } /** @@ -1532,7 +1922,7 @@ public class RedisUtil implements InitializingBean { */ public static String lIndex(String key, Long index) { log.info("lIndex(...) => key= {}, index= {}", key, index); - String item = redisTemplate.opsForList().index(key, index); + String item = genericRedisTemplate.opsForList().index(key, index); log.info("lIndex(...) => item= {}", item); return item; } @@ -1558,7 +1948,7 @@ public class RedisUtil implements InitializingBean { */ public static List lRange(String key, Long start, Long end) { log.info("lRange(...) => key= {}, start= {}, end= {}", key, start, end); - List result = redisTemplate.opsForList().range(key, start, end); + List result = genericRedisTemplate.opsForList().range(key, start, end); log.info("lRange(...) => result= {}", result); return result; } @@ -1575,7 +1965,7 @@ public class RedisUtil implements InitializingBean { */ public static List lWholeList(String key) { log.info("lWholeList(...) => key= {}", key); - List result = redisTemplate.opsForList().range(key, 0, -1); + List result = genericRedisTemplate.opsForList().range(key, 0, -1); log.info("lWholeList(...) => result= {}", result); return result; } @@ -1594,7 +1984,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lSize(String key) { log.info("lSize(...) => key= {}", key); - Long size = redisTemplate.opsForList().size(key); + Long size = genericRedisTemplate.opsForList().size(key); log.info("lSize(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1625,7 +2015,7 @@ public class RedisUtil implements InitializingBean { */ public static Long lRemove(String key, Long expectCount, String item) { log.info("lRemove(...) => key= {}, expectCount= {}, item= {}", key, expectCount, item); - Long actualCount = redisTemplate.opsForList().remove(key, expectCount, item); + Long actualCount = genericRedisTemplate.opsForList().remove(key, expectCount, item); log.info("lRemove(...) => actualCount= {}", actualCount); if (actualCount == null) { throw new RedisOpsResultIsNullException(); @@ -1652,7 +2042,7 @@ public class RedisUtil implements InitializingBean { */ public static void lTrim(String key, Long start, Long end) { log.info("lTrim(...) => key= {}, start= {}, end= {}", key, start, end); - redisTemplate.opsForList().trim(key, start, end); + genericRedisTemplate.opsForList().trim(key, start, end); } } @@ -1670,29 +2060,29 @@ public class RedisUtil implements InitializingBean { */ public static class SetOps { - /** - * 向(key对应的)set中添加items + /** + * 向(key对应的)set中添加items * * 注: 若key不存在,则会自动创建。 - * 注: set中的元素会去重。 - * - * @param key - * 定位set的key - * @param items - * 要向(key对应的)set中添加的items - * - * @return 此次添加操作, 添加到set中的元素的个数 - * @date 2020/3/11 8:16:00 - */ - public static Long sAdd(String key, String... items) { - log.info("sAdd(...) => key= {}, items= {}", key, items); - Long count = redisTemplate.opsForSet().add(key, items); - log.info("sAdd(...) => count= {}", count); - if (count == null) { - throw new RedisOpsResultIsNullException(); - } - return count; + * 注: set中的元素会去重。 + * + * @param key + * 定位set的key + * @param items + * 要向(key对应的)set中添加的items + * + * @return 此次添加操作, 添加到set中的元素的个数 + * @date 2020/3/11 8:16:00 + */ + public static Long sAdd(String key, String... items) { + log.info("sAdd(...) => key= {}, items= {}", key, items); + Long count = genericRedisTemplate.opsForSet().add(key, items); + log.info("sAdd(...) => count= {}", count); + if (count == null) { + throw new RedisOpsResultIsNullException(); } + return count; + } /** * 从(key对应的)set中删除items @@ -1710,7 +2100,7 @@ public class RedisUtil implements InitializingBean { */ public static Long sRemove(String key, Object... items) { log.info("sRemove(...) => key= {}, items= {}", key, items); - Long count = redisTemplate.opsForSet().remove(key, items); + Long count = genericRedisTemplate.opsForSet().remove(key, items); log.info("sRemove(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -1734,7 +2124,7 @@ public class RedisUtil implements InitializingBean { */ public static String sPop(String key) { log.info("sPop(...) => key= {}", key); - String popItem = redisTemplate.opsForSet().pop(key); + String popItem = genericRedisTemplate.opsForSet().pop(key); log.info("sPop(...) => popItem= {}", popItem); return popItem; } @@ -1758,7 +2148,7 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/11 8:43:32 */ public static Boolean sMove(String sourceKey, String item, String destinationKey) { - Boolean result = redisTemplate.opsForSet().move(sourceKey, item, destinationKey); + Boolean result = genericRedisTemplate.opsForSet().move(sourceKey, item, destinationKey); log.info("sMove(...) => sourceKey= {}, destinationKey= {}, item= {}", sourceKey, destinationKey, item); log.info("sMove(...) => result= {}", result); @@ -1781,7 +2171,7 @@ public class RedisUtil implements InitializingBean { */ public static Long sSize(String key) { log.info("sSize(...) => key= {}", key); - Long size = redisTemplate.opsForSet().size(key); + Long size = genericRedisTemplate.opsForSet().size(key); log.info("sSize(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1804,7 +2194,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean sIsMember(String key, Object item) { log.info("sSize(...) => key= {}, size= {}", key, item); - Boolean result = redisTemplate.opsForSet().isMember(key, item); + Boolean result = genericRedisTemplate.opsForSet().isMember(key, item); log.info("sSize(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -1828,7 +2218,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sIntersect(String key, String otherKey) { log.info("sIntersect(...) => key= {}, otherKey= {}", key, otherKey); - Set intersectResult = redisTemplate.opsForSet().intersect(key, otherKey); + Set intersectResult = genericRedisTemplate.opsForSet().intersect(key, otherKey); log.info("sIntersect(...) => intersectResult= {}", intersectResult); return intersectResult; } @@ -1849,7 +2239,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sIntersect(String key, Collection otherKeys) { log.info("sIntersect(...) => key= {}, otherKeys= {}", key, otherKeys); - Set intersectResult = redisTemplate.opsForSet().intersect(key, otherKeys); + Set intersectResult = genericRedisTemplate.opsForSet().intersect(key, otherKeys); log.info("sIntersect(...) => intersectResult= {}", intersectResult); return intersectResult; } @@ -1862,7 +2252,7 @@ public class RedisUtil implements InitializingBean { * case3: 交集为空, 则不进行下面的操作, 直接返回0 * * 注: 求交集的部分,详见{@link SetOps#sIntersect(String, String)} - * + * * @param key * 定位其中一个set的键 * @param otherKey @@ -1876,7 +2266,7 @@ public class RedisUtil implements InitializingBean { public static Long sIntersectAndStore(String key, String otherKey, String storeKey) { log.info("sIntersectAndStore(...) => key= {}, otherKey= {}, storeKey= {}", key, otherKey, storeKey); - Long size = redisTemplate.opsForSet().intersectAndStore(key, otherKey, storeKey); + Long size = genericRedisTemplate.opsForSet().intersectAndStore(key, otherKey, storeKey); log.info("sIntersectAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1896,10 +2286,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/11 11:04:29 */ public static Long sIntersectAndStore(String key, Collection otherKeys, - String storeKey) { + String storeKey) { log.info("sIntersectAndStore(...) => key= {}, otherKeys= {}, storeKey= {}", key, otherKeys, storeKey); - Long size = redisTemplate.opsForSet().intersectAndStore(key, otherKeys, storeKey); + Long size = genericRedisTemplate.opsForSet().intersectAndStore(key, otherKeys, storeKey); log.info("sIntersectAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1922,7 +2312,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sUnion(String key, String otherKey) { log.info("sUnion(...) => key= {}, otherKey= {}", key, otherKey); - Set unionResult = redisTemplate.opsForSet().union(key, otherKey); + Set unionResult = genericRedisTemplate.opsForSet().union(key, otherKey); log.info("sUnion(...) => unionResult= {}", unionResult); return unionResult; } @@ -1942,7 +2332,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sUnion(String key, Collection otherKeys) { log.info("sUnion(...) => key= {}, otherKeys= {}", key, otherKeys); - Set unionResult = redisTemplate.opsForSet().union(key, otherKeys); + Set unionResult = genericRedisTemplate.opsForSet().union(key, otherKeys); log.info("sUnion(...) => unionResult= {}", unionResult); return unionResult; } @@ -1969,7 +2359,7 @@ public class RedisUtil implements InitializingBean { public static Long sUnionAndStore(String key, String otherKey, String storeKey) { log.info("sUnionAndStore(...) => key= {}, otherKey= {}, storeKey= {}", key, otherKey, storeKey); - Long size = redisTemplate.opsForSet().unionAndStore(key, otherKey, storeKey); + Long size = genericRedisTemplate.opsForSet().unionAndStore(key, otherKey, storeKey); log.info("sUnionAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -1999,7 +2389,7 @@ public class RedisUtil implements InitializingBean { public static Long sUnionAndStore(String key, Collection otherKeys, String storeKey) { log.info("sUnionAndStore(...) => key= {}, otherKeys= {}, storeKey= {}", key, otherKeys, storeKey); - Long size = redisTemplate.opsForSet().unionAndStore(key, otherKeys, storeKey); + Long size = genericRedisTemplate.opsForSet().unionAndStore(key, otherKeys, storeKey); log.info("sUnionAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2024,7 +2414,7 @@ public class RedisUtil implements InitializingBean { public static Set sDifference(String key, String otherKey) { log.info("sDifference(...) => key= {}, otherKey= {}", key, otherKey); - Set differenceResult = redisTemplate.opsForSet().difference(key, otherKey); + Set differenceResult = genericRedisTemplate.opsForSet().difference(key, otherKey); log.info("sDifference(...) => differenceResult= {}", differenceResult); return differenceResult; } @@ -2047,7 +2437,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sDifference(String key, Collection otherKeys) { log.info("sDifference(...) => key= {}, otherKeys= {}", key, otherKeys); - Set differenceResult = redisTemplate.opsForSet().difference(key, otherKeys); + Set differenceResult = genericRedisTemplate.opsForSet().difference(key, otherKeys); log.info("sDifference(...) => differenceResult= {}", differenceResult); return differenceResult; } @@ -2074,7 +2464,7 @@ public class RedisUtil implements InitializingBean { public static Long sDifferenceAndStore(String key, String otherKey, String storeKey) { log.info("sDifferenceAndStore(...) => key= {}, otherKey= {}, storeKey= {}", key, otherKey, storeKey); - Long size = redisTemplate.opsForSet().differenceAndStore(key, otherKey, storeKey); + Long size = genericRedisTemplate.opsForSet().differenceAndStore(key, otherKey, storeKey); log.info("sDifferenceAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2102,10 +2492,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/11 14:33:36 */ public static Long sDifferenceAndStore(String key, Collection otherKeys, - String storeKey) { + String storeKey) { log.info("sDifferenceAndStore(...) => key= {}, otherKeys= {}, storeKey= {}", key, otherKeys, storeKey); - Long size = redisTemplate.opsForSet().differenceAndStore(key, otherKeys, storeKey); + Long size = genericRedisTemplate.opsForSet().differenceAndStore(key, otherKeys, storeKey); log.info("sDifferenceAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2125,7 +2515,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sMembers(String key) { log.info("sMembers(...) => key= {}", key); - Set members = redisTemplate.opsForSet().members(key); + Set members = genericRedisTemplate.opsForSet().members(key); log.info("sMembers(...) => members= {}", members); return members; } @@ -2140,7 +2530,7 @@ public class RedisUtil implements InitializingBean { */ public static String sRandomMember(String key) { log.info("sRandomMember(...) => key= {}", key); - String randomItem = redisTemplate.opsForSet().randomMember(key); + String randomItem = genericRedisTemplate.opsForSet().randomMember(key); log.info("sRandomMember(...) => randomItem= {}", randomItem); return randomItem; } @@ -2161,7 +2551,7 @@ public class RedisUtil implements InitializingBean { */ public static List sRandomMembers(String key, Long count) { log.info("sRandomMembers(...) => key= {}, count= {}", key, count); - List randomItems = redisTemplate.opsForSet().randomMembers(key, count); + List randomItems = genericRedisTemplate.opsForSet().randomMembers(key, count); log.info("sRandomMembers(...) => randomItems= {}", randomItems); return randomItems; } @@ -2182,7 +2572,7 @@ public class RedisUtil implements InitializingBean { */ public static Set sDistinctRandomMembers(String key, Long count) { log.info("sDistinctRandomMembers(...) => key= {}, count= {}", key, count); - Set distinctRandomItems = redisTemplate.opsForSet().distinctRandomMembers(key, count); + Set distinctRandomItems = genericRedisTemplate.opsForSet().distinctRandomMembers(key, count); log.info("sDistinctRandomMembers(...) => distinctRandomItems= {}", distinctRandomItems); return distinctRandomItems; } @@ -2211,7 +2601,7 @@ public class RedisUtil implements InitializingBean { */ public static Cursor sScan(String key, ScanOptions options) { log.info("sScan(...) => key= {}, options= {}", key, JSON.toJSONString(options)); - Cursor cursor = redisTemplate.opsForSet().scan(key, options); + Cursor cursor = genericRedisTemplate.opsForSet().scan(key, options); log.info("sScan(...) => cursor= {}", JSON.toJSONString(cursor)); return cursor; } @@ -2254,7 +2644,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean zAdd(String key, String item, Double score) { log.info("zAdd(...) => key= {}, item= {}, score= {}", key, item, score); - Boolean result = redisTemplate.opsForZSet().add(key, item, score); + Boolean result = genericRedisTemplate.opsForZSet().add(key, item, score); log.info("zAdd(...) => result= {}", result); if (result == null) { throw new RedisOpsResultIsNullException(); @@ -2281,7 +2671,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zAdd(String key, Set> entries) { log.info("zAdd(...) => key= {}, entries= {}", key, JSON.toJSONString(entries)); - Long count = redisTemplate.opsForZSet().add(key, entries); + Long count = genericRedisTemplate.opsForZSet().add(key, entries); log.info("zAdd(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -2304,7 +2694,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zRemove(String key, Object... items) { log.info("zRemove(...) => key= {}, items= {}", key, items); - Long count = redisTemplate.opsForZSet().remove(key, items); + Long count = genericRedisTemplate.opsForZSet().remove(key, items); log.info("zRemove(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -2342,7 +2732,7 @@ public class RedisUtil implements InitializingBean { public static Long zRemoveRange(String key, Long startRange, Long endRange) { log.info("zRemoveRange(...) => key= {}, startRange= {}, endRange= {}", key, startRange, endRange); - Long count = redisTemplate.opsForZSet().removeRange(key, startRange, endRange); + Long count = genericRedisTemplate.opsForZSet().removeRange(key, startRange, endRange); log.info("zRemoveRange(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -2373,7 +2763,7 @@ public class RedisUtil implements InitializingBean { public static Long zRemoveRangeByScore(String key, Double minScore, Double maxScore) { log.info("zRemoveRangeByScore(...) => key= {}, startIndex= {}, startIndex= {}", key, minScore, maxScore); - Long count = redisTemplate.opsForZSet().removeRangeByScore(key, minScore, maxScore); + Long count = genericRedisTemplate.opsForZSet().removeRangeByScore(key, minScore, maxScore); log.info("zRemoveRangeByScore(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -2395,7 +2785,7 @@ public class RedisUtil implements InitializingBean { */ public static Double zIncrementScore(String key, String item, Double delta) { log.info("zIncrementScore(...) => key= {}, item= {}, delta= {}", key, item, delta); - Double scoreValue = redisTemplate.opsForZSet().incrementScore(key, item, delta); + Double scoreValue = genericRedisTemplate.opsForZSet().incrementScore(key, item, delta); log.info("zIncrementScore(...) => scoreValue= {}", scoreValue); if (scoreValue == null) { throw new RedisOpsResultIsNullException(); @@ -2420,7 +2810,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zRank(String key, Object item) { log.info("zRank(...) => key= {}, item= {}", key, item); - Long rank = redisTemplate.opsForZSet().rank(key, item); + Long rank = genericRedisTemplate.opsForZSet().rank(key, item); log.info("zRank(...) => rank= {}", rank); if (rank == null) { throw new RedisOpsResultIsNullException(); @@ -2445,7 +2835,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zReverseRank(String key, Object item) { log.info("zReverseRank(...) => key= {}, item= {}", key, item); - Long reverseRank = redisTemplate.opsForZSet().reverseRank(key, item); + Long reverseRank = genericRedisTemplate.opsForZSet().reverseRank(key, item); log.info("zReverseRank(...) => reverseRank= {}", reverseRank); if (reverseRank == null) { throw new RedisOpsResultIsNullException(); @@ -2478,7 +2868,7 @@ public class RedisUtil implements InitializingBean { */ public static Set zRange(String key, Long start, Long end) { log.info("zRange(...) => key= {}, start= {}, end= {}", key, start, end); - Set result = redisTemplate.opsForZSet().range(key, start, end); + Set result = genericRedisTemplate.opsForZSet().range(key, start, end); log.info("zRange(...) => result= {}", result); return result; } @@ -2496,7 +2886,7 @@ public class RedisUtil implements InitializingBean { */ public static Set zWholeSortSetItem(String key) { log.info("zWholeZSetItem(...) => key= {}", key); - Set result = redisTemplate.opsForZSet().range(key, 0, -1); + Set result = genericRedisTemplate.opsForZSet().range(key, 0, -1); log.info("zWholeZSetItem(...) =>result= {}", result); return result; } @@ -2528,7 +2918,7 @@ public class RedisUtil implements InitializingBean { */ public static Set> zRangeWithScores(String key, Long start, Long end) { log.info("zRangeWithScores(...) => key= {}, start= {}, end= {}", key, start, end); - Set> entries = redisTemplate.opsForZSet().rangeWithScores(key, start, end); + Set> entries = genericRedisTemplate.opsForZSet().rangeWithScores(key, start, end); log.info("zRangeWithScores(...) => entries= {}", JSON.toJSONString(entries)); return entries; } @@ -2546,7 +2936,7 @@ public class RedisUtil implements InitializingBean { */ public static Set> zWholeSortSetEntry(String key) { log.info("zWholeZSetEntry(...) => key= {}", key); - Set> entries = redisTemplate.opsForZSet().rangeWithScores(key, 0, -1); + Set> entries = genericRedisTemplate.opsForZSet().rangeWithScores(key, 0, -1); log.info("zWholeZSetEntry(...) => entries= {}", JSON.toJSONString(entries)); return entries; } @@ -2575,7 +2965,7 @@ public class RedisUtil implements InitializingBean { public static Set zRangeByScore(String key, Double minScore, Double maxScore) { log.info("zRangeByScore(...) => key= {}, minScore= {}, maxScore= {}", key, minScore, maxScore); - Set items = redisTemplate.opsForZSet().rangeByScore(key, minScore, maxScore); + Set items = genericRedisTemplate.opsForZSet().rangeByScore(key, minScore, maxScore); log.info("zRangeByScore(...) => items= {}", items); return items; } @@ -2608,10 +2998,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/12 9:50:40 */ public static Set zRangeByScore(String key, Double minScore, Double maxScore, - Long offset, Long count) { + Long offset, Long count) { log.info("zRangeByScore(...) => key= {}, minScore= {}, maxScore= {}, offset= {}, " + "count= {}", key, minScore, maxScore, offset, count); - Set items = redisTemplate.opsForZSet() + Set items = genericRedisTemplate.opsForZSet() .rangeByScore(key, minScore, maxScore, offset, count); log.info("zRangeByScore(...) => items= {}", items); return items; @@ -2636,10 +3026,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/12 10:02:07 */ public static Set> zRangeByScoreWithScores(String key, Double minScore, - Double maxScore) { + Double maxScore) { log.info("zRangeByScoreWithScores(...) => key= {}, minScore= {}, maxScore= {}", key, minScore, maxScore); - Set> entries = redisTemplate.opsForZSet() + Set> entries = genericRedisTemplate.opsForZSet() .rangeByScoreWithScores(key, minScore, maxScore); log.info("zRangeByScoreWithScores(...) => entries= {}", JSON.toJSONString(entries)); return entries; @@ -2665,12 +3055,12 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/12 11:09:06 */ public static Set> zRangeByScoreWithScores(String key, Double minScore, - Double maxScore, Long offset, - Long count) { + Double maxScore, Long offset, + Long count) { log.info("zRangeByScoreWithScores(...) => key= {}, minScore= {}, maxScore= {}," + " offset= {}, count= {}", key, minScore, maxScore, offset, count); - Set> entries = redisTemplate.opsForZSet() + Set> entries = genericRedisTemplate.opsForZSet() .rangeByScoreWithScores(key, minScore, maxScore, offset, count); log.info("zRangeByScoreWithScores(...) => entries= {}", JSON.toJSONString(entries)); @@ -2685,7 +3075,7 @@ public class RedisUtil implements InitializingBean { */ public static Set zReverseRange(String key, Long start, Long end) { log.info("zReverseRange(...) => key= {}, start= {}, end= {}", key, start, end); - Set entries = redisTemplate.opsForZSet().reverseRange(key, start, end); + Set entries = genericRedisTemplate.opsForZSet().reverseRange(key, start, end); log.info("zReverseRange(...) => entries= {}", entries); return entries; } @@ -2696,9 +3086,9 @@ public class RedisUtil implements InitializingBean { * @see SortSetOps#zRangeWithScores(String, Long, Long)。 只是zReverseRangeWithScores这里会提前多一个倒序。 */ public static Set> zReverseRangeWithScores(String key, Long start, - Long end) { + Long end) { log.info("zReverseRangeWithScores(...) => key= {}, start= {}, end= {}", key, start, end); - Set> entries = redisTemplate.opsForZSet() + Set> entries = genericRedisTemplate.opsForZSet() .reverseRangeWithScores(key, start, end); log.info("zReverseRangeWithScores(...) => entries= {}", JSON.toJSONString(entries)); return entries; @@ -2712,7 +3102,7 @@ public class RedisUtil implements InitializingBean { public static Set zReverseRangeByScore(String key, Double minScore, Double maxScore) { log.info("zReverseRangeByScore(...) => key= {}, minScore= {}, maxScore= {}", key, minScore, maxScore); - Set items = redisTemplate.opsForZSet().reverseRangeByScore(key, minScore, maxScore); + Set items = genericRedisTemplate.opsForZSet().reverseRangeByScore(key, minScore, maxScore); log.info("zReverseRangeByScore(...) => items= {}", items); return items; } @@ -2723,10 +3113,10 @@ public class RedisUtil implements InitializingBean { * @see SortSetOps#zRangeByScoreWithScores(String, Double, Double)。 只是zReverseRangeByScoreWithScores这里会提前多一个倒序。 */ public static Set> zReverseRangeByScoreWithScores(String key, - Double minScore, Double maxScore) { + Double minScore, Double maxScore) { log.info("zReverseRangeByScoreWithScores(...) => key= {}, minScore= {}, maxScore= {}", key, minScore, maxScore); - Set> entries = redisTemplate.opsForZSet() + Set> entries = genericRedisTemplate.opsForZSet() .reverseRangeByScoreWithScores(key, minScore, maxScore); log.info("zReverseRangeByScoreWithScores(...) => entries= {}", JSON.toJSONString(entries)); @@ -2740,10 +3130,10 @@ public class RedisUtil implements InitializingBean { * @see SortSetOps#zRangeByScore(String, Double, Double, Long, Long)。 只是zReverseRangeByScore这里会提前多一个倒序。 */ public static Set zReverseRangeByScore(String key, Double minScore, Double maxScore, - Long offset, Long count) { + Long offset, Long count) { log.info("zReverseRangeByScore(...) => key= {}, minScore= {}, maxScore= {}, offset= {}, " + "count= {}", key, minScore, maxScore, offset, count); - Set items = redisTemplate.opsForZSet() + Set items = genericRedisTemplate.opsForZSet() .reverseRangeByScore(key, minScore, maxScore, offset, count); log.info("items= {}", items); return items; @@ -2764,7 +3154,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zCount(String key, Double minScore, Double maxScore) { log.info("zCount(...) => key= {}, minScore= {}, maxScore= {}", key, minScore, maxScore); - Long count = redisTemplate.opsForZSet().count(key, minScore, maxScore); + Long count = genericRedisTemplate.opsForZSet().count(key, minScore, maxScore); log.info("zCount(...) => count= {}", count); if (count == null) { throw new RedisOpsResultIsNullException(); @@ -2785,7 +3175,7 @@ public class RedisUtil implements InitializingBean { */ public static Long zSize(String key) { log.info("zSize(...) => key= {}", key); - Long size = redisTemplate.opsForZSet().size(key); + Long size = genericRedisTemplate.opsForZSet().size(key); log.info("zSize(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2806,7 +3196,7 @@ public class RedisUtil implements InitializingBean { */ public static Long sortSetCard(String key) { log.info("zZCard(...) => key= {}", key); - Long size = redisTemplate.opsForZSet().zCard(key); + Long size = genericRedisTemplate.opsForZSet().zCard(key); log.info("zZCard(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2827,7 +3217,7 @@ public class RedisUtil implements InitializingBean { */ public static Double zScore(String key, Object item) { log.info("zScore(...) => key= {}, item= {}", key, item); - Double score = redisTemplate.opsForZSet().score(key, item); + Double score = genericRedisTemplate.opsForZSet().score(key, item); log.info("zScore(...) => score= {}", score); if (score == null) { throw new RedisOpsResultIsNullException(); @@ -2859,7 +3249,7 @@ public class RedisUtil implements InitializingBean { public static Long zUnionAndStore(String key, String otherKey, String storeKey) { log.info("zUnionAndStore(...) => key= {}, otherKey= {}, storeKey= {}", key, otherKey, storeKey); - Long size = redisTemplate.opsForZSet().unionAndStore(key, otherKey, storeKey); + Long size = genericRedisTemplate.opsForZSet().unionAndStore(key, otherKey, storeKey); log.info("zUnionAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2891,7 +3281,7 @@ public class RedisUtil implements InitializingBean { public static Long zUnionAndStore(String key, Collection otherKeys, String storeKey) { log.info("zUnionAndStore(...) => key= {}, otherKeys= {}, storeKey= {}", key, otherKeys, storeKey); - Long size = redisTemplate.opsForZSet().unionAndStore(key, otherKeys, storeKey); + Long size = genericRedisTemplate.opsForZSet().unionAndStore(key, otherKeys, storeKey); log.info("zUnionAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2927,7 +3317,7 @@ public class RedisUtil implements InitializingBean { public static Long zIntersectAndStore(String key, String otherKey, String storeKey) { log.info("zIntersectAndStore(...) => key= {}, otherKey= {}, storeKey= {}", key, otherKey, storeKey); - Long size = redisTemplate.opsForZSet().intersectAndStore(key, otherKey, storeKey); + Long size = genericRedisTemplate.opsForZSet().intersectAndStore(key, otherKey, storeKey); log.info("zIntersectAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -2953,10 +3343,10 @@ public class RedisUtil implements InitializingBean { * @date 2020/3/11 11:04:29 */ public static Long zIntersectAndStore(String key, Collection otherKeys, - String storeKey) { + String storeKey) { log.info("zIntersectAndStore(...) => key= {}, otherKeys= {}, storeKey= {}", key, otherKeys, storeKey); - Long size = redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, storeKey); + Long size = genericRedisTemplate.opsForZSet().intersectAndStore(key, otherKeys, storeKey); log.info("zIntersectAndStore(...) => size= {}", size); if (size == null) { throw new RedisOpsResultIsNullException(); @@ -3006,11 +3396,11 @@ public class RedisUtil implements InitializingBean { static { // 不论lua中0是否代表失败; 对于java的Boolean而言, 返回0, 则会被解析为false RELEASE_LOCK_LUA = "if redis.call('get',KEYS[1]) == ARGV[1] " - + "then " - + " return redis.call('del',KEYS[1]) " - + "else " - + " return 0 " - + "end "; + + "then " + + " return redis.call('del',KEYS[1]) " + + "else " + + " return 0 " + + "end "; } /** @@ -3039,7 +3429,7 @@ public class RedisUtil implements InitializingBean { * @return 是否成功 */ public static Boolean getLockUntilTimeout(final String key, final String value, - final Long retryTimeoutLimit) { + final Long retryTimeoutLimit) { return getLockUntilTimeout(key, value, DEFAULT_LOCK_TIMEOUT, DEFAULT_TIMEOUT_UNIT, retryTimeoutLimit); } @@ -3059,8 +3449,8 @@ public class RedisUtil implements InitializingBean { * @return 是否成功 */ public static Boolean getLockUntilTimeout(final String key, final String value, - final Long timeout, final TimeUnit unit, - final Long retryTimeoutLimit) { + final Long timeout, final TimeUnit unit, + final Long retryTimeoutLimit) { log.info("getLockUntilTimeout(...) => key= {}, value= {}, timeout= {}, unit= {}, " + "retryTimeoutLimit= {}ms", key, value, timeout, unit, retryTimeoutLimit); Long startTime = Instant.now().toEpochMilli(); @@ -3091,7 +3481,7 @@ public class RedisUtil implements InitializingBean { * @see LockOps#getLock(String, String, Long, TimeUnit, Boolean) */ public static Boolean getLock(final String key, final String value, - final Long timeout, final TimeUnit unit) { + final Long timeout, final TimeUnit unit) { return getLock(key, value, timeout, unit, true); } @@ -3121,13 +3511,13 @@ public class RedisUtil implements InitializingBean { * @return 是否成功 */ public static Boolean getLock(final String key, final String value, - final Long timeout, final TimeUnit unit, - Boolean recordLog) { + final Long timeout, final TimeUnit unit, + Boolean recordLog) { if (recordLog) { log.info("getLock(...) => key= {}, value= {}, timeout= {}, unit= {}, recordLog= {}", key, value, timeout, unit, recordLog); } - Boolean result = redisTemplate.execute((RedisConnection connection) -> + Boolean result = genericRedisTemplate.execute((RedisConnection connection) -> connection.set(key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8), Expiration.seconds(unit.toSeconds(timeout)), @@ -3158,7 +3548,7 @@ public class RedisUtil implements InitializingBean { */ public static Boolean releaseLock(final String key, final String value) { log.info("releaseLock(...) => key= {}, lockValue= {}", key, value); - Boolean result = redisTemplate.execute((RedisConnection connection) -> + Boolean result = genericRedisTemplate.execute((RedisConnection connection) -> connection.eval(RELEASE_LOCK_LUA.getBytes(), ReturnType.BOOLEAN, 1, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8))