From 03c43f84ceb47b049ac8ea8fd4c07d42bc4af9fa Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Mon, 29 Aug 2022 14:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6redis=E6=9C=AA?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/MediaServerServiceImpl.java | 6 +- .../storager/impl/RedisCatchStorageImpl.java | 1 + .../iot/vmp/utils/SpringBeanFactory.java | 4 +- .../iot/vmp/utils/redis/RedisUtil.java | 164 ++++++++++++++++-- 4 files changed, 158 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index 9ee55310..d923755a 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -20,7 +20,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; import org.springframework.util.ObjectUtils; -import org.springframework.util.StringUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -40,7 +39,6 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo; import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; import com.genersoft.iot.vmp.utils.DateUtil; import com.genersoft.iot.vmp.utils.redis.RedisUtil; -import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -135,7 +133,7 @@ public class MediaServerServiceImpl implements IMediaServerService { logger.info("media server [ {} ] ssrcConfig is null", mediaServerItem.getId()); return null; }else { - String ssrc = null; + String ssrc; if (presetSsrc != null) { ssrc = presetSsrc; }else { @@ -497,7 +495,7 @@ public class MediaServerServiceImpl implements IMediaServerService { } // 获取分数最低的,及并发最低的 - Set objects = RedisUtil.ZRange(key, 0, -1); + Set objects = RedisUtil.zRange(key, 0, -1); ArrayList mediaServerObjectS = new ArrayList<>(objects); String mediaServerId = (String)mediaServerObjectS.get(0); diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java index a401ee2e..14a369cc 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java @@ -21,6 +21,7 @@ import com.genersoft.iot.vmp.utils.redis.RedisUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import java.util.*; diff --git a/src/main/java/com/genersoft/iot/vmp/utils/SpringBeanFactory.java b/src/main/java/com/genersoft/iot/vmp/utils/SpringBeanFactory.java index b155bcd2..53238754 100644 --- a/src/main/java/com/genersoft/iot/vmp/utils/SpringBeanFactory.java +++ b/src/main/java/com/genersoft/iot/vmp/utils/SpringBeanFactory.java @@ -33,11 +33,11 @@ public class SpringBeanFactory implements ApplicationContextAware { /** * 获取对象 这里重写了bean方法,起主要作用 */ - public static Object getBean(String beanId) throws BeansException { + public static T getBean(String beanId) throws BeansException { if (applicationContext == null) { return null; } - return applicationContext.getBean(beanId); + return (T) applicationContext.getBean(beanId); } /** diff --git a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java index 5ef0b397..0034c398 100644 --- a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java +++ b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java @@ -5,15 +5,13 @@ import java.util.concurrent.TimeUnit; import com.alibaba.fastjson.JSONObject; import com.genersoft.iot.vmp.utils.SpringBeanFactory; -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 */ @SuppressWarnings(value = {"rawtypes", "unchecked"}) public class RedisUtil { @@ -21,9 +19,9 @@ public class RedisUtil { private static RedisTemplate redisTemplate; static { - redisTemplate = (RedisTemplate)SpringBeanFactory.getBean("redisTemplate"); + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); } - + /** * 指定缓存失效时间 * @param key 键 @@ -31,6 +29,9 @@ public class RedisUtil { * @return true / false */ 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); @@ -45,9 +46,11 @@ public class RedisUtil { /** * 根据 key 获取过期时间 * @param key 键 - * @return */ public static long getExpire(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.getExpire(key, TimeUnit.SECONDS); } @@ -57,6 +60,9 @@ public class RedisUtil { * @return true / false */ public static boolean hasKey(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { return redisTemplate.hasKey(key); } catch (Exception e) { @@ -71,6 +77,9 @@ public class RedisUtil { * @param 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) { @@ -95,6 +104,9 @@ public class RedisUtil { * @return 值 */ public static Object get(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return key == null ? null : redisTemplate.opsForValue().get(key); } @@ -105,6 +117,9 @@ public class RedisUtil { * @return true / false */ public static boolean set(String key, Object value) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { redisTemplate.opsForValue().set(key, value); return true; @@ -122,6 +137,9 @@ public class RedisUtil { * @return true / false */ 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); @@ -142,6 +160,9 @@ public class RedisUtil { * @return */ public static long incr(String key, long delta) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } if (delta < 0) { throw new RuntimeException("递增因子必须大于 0"); } @@ -155,6 +176,9 @@ public class RedisUtil { * @return */ public static long decr(String key, long delta) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } if (delta < 0) { throw new RuntimeException("递减因子必须大于 0"); } @@ -170,6 +194,9 @@ public class RedisUtil { * @return 值 */ public static Object hget(String key, String item) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForHash().get(key, item); } @@ -179,6 +206,9 @@ public class RedisUtil { * @return 对应的多个键值 */ public static Map hmget(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForHash().entries(key); } @@ -189,6 +219,9 @@ public class RedisUtil { * @return true / false */ public static boolean hmset(String key, Map map) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { redisTemplate.opsForHash().putAll(key, map); return true; @@ -206,6 +239,9 @@ public class RedisUtil { * @return true / false */ 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) { @@ -226,6 +262,9 @@ public class RedisUtil { * @return true / false */ 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; @@ -244,6 +283,9 @@ public class RedisUtil { * @return true / false */ 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) { @@ -262,6 +304,9 @@ public class RedisUtil { * @param item 项(可以多个,no null) */ public static void hdel(String key, Object... item) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } redisTemplate.opsForHash().delete(key, item); } @@ -272,6 +317,9 @@ public class RedisUtil { * @return true / false */ public static boolean hHasKey(String key, String item) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForHash().hasKey(key, item); } @@ -283,6 +331,9 @@ public class RedisUtil { * @return */ public static Double hincr(String key, String item, Double by) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForHash().increment(key, item, by); } @@ -294,6 +345,9 @@ public class RedisUtil { * @return */ public static Double hdecr(String key, String item, Double by) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForHash().increment(key, item, -by); } @@ -305,6 +359,9 @@ public class RedisUtil { * @return 值 */ public static Set sGet(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { @@ -320,6 +377,9 @@ public class RedisUtil { * @return true / false */ 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) { @@ -335,6 +395,9 @@ public class RedisUtil { * @return 成功个数 */ 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) { @@ -351,6 +414,9 @@ public class RedisUtil { * @return 成功放入个数 */ 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) { @@ -369,6 +435,9 @@ public class RedisUtil { * @return 长度 */ public static long sGetSetSize(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { @@ -384,6 +453,9 @@ public class RedisUtil { * @return 成功移除个数 */ 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) { @@ -401,6 +473,9 @@ public class RedisUtil { * @param score */ public static void zAdd(Object key, Object value, double score) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } redisTemplate.opsForZSet().add(key, value, score); } @@ -411,6 +486,9 @@ public class RedisUtil { * @param value */ public static void zRemove(Object key, Object value) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } redisTemplate.opsForZSet().remove(key, value); } @@ -422,6 +500,9 @@ public class RedisUtil { * @param delta -1 表示减 1 表示加1 */ public static Double zIncrScore(Object key, Object value, double delta) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().incrementScore(key, value, delta); } @@ -433,6 +514,9 @@ public class RedisUtil { * @return */ public static Double zScore(Object key, Object value) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().score(key, value); } @@ -444,6 +528,9 @@ public class RedisUtil { * @return */ public static Long zRank(Object key, Object value) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().rank(key, value); } @@ -454,6 +541,9 @@ public class RedisUtil { * @return */ public static Long zSize(Object key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().zCard(key); } @@ -467,7 +557,10 @@ public class RedisUtil { * @param end * @return */ - public static Set ZRange(Object key, int start, int end) { + public static Set zRange(Object key, int start, int end) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().range(key, start, end); } /** @@ -479,6 +572,9 @@ public class RedisUtil { * @return */ public static Set> zRangeWithScore(Object key, int start, int end) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().rangeWithScores(key, start, end); } /** @@ -492,6 +588,9 @@ public class RedisUtil { * @return */ public static Set zRevRange(Object key, int start, int end) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().reverseRange(key, start, end); } /** @@ -503,6 +602,9 @@ public class RedisUtil { * @return */ public static Set zSortRange(Object key, int min, int max) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForZSet().rangeByScore(key, min, max); } @@ -517,6 +619,9 @@ public class RedisUtil { * @return */ public static List 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) { @@ -531,6 +636,9 @@ public class RedisUtil { * @return 长度 */ public static long lGetListSize(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { return redisTemplate.opsForList().size(key); } catch (Exception e) { @@ -548,6 +656,9 @@ public class RedisUtil { * @return 值 */ 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) { @@ -563,6 +674,9 @@ public class RedisUtil { * @return true / false */ public static boolean lSet(String key, Object value) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { redisTemplate.opsForList().rightPush(key, value); return true; @@ -580,6 +694,9 @@ public class RedisUtil { * @return true / false */ 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) { @@ -599,6 +716,9 @@ public class RedisUtil { * @return true / false */ public static boolean lSetList(String key, List values) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { redisTemplate.opsForList().rightPushAll(key, values); return true; @@ -616,6 +736,9 @@ public class RedisUtil { * @return true / false */ public static boolean lSetList(String key, List values, long time) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { redisTemplate.opsForList().rightPushAll(key, values); if (time > 0) { @@ -636,6 +759,9 @@ public class RedisUtil { * @return true / false */ 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; @@ -655,6 +781,9 @@ public class RedisUtil { * @return */ 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) { @@ -669,6 +798,9 @@ public class RedisUtil { * @return */ public static Object lLeftPop(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForList().leftPop(key); } @@ -678,6 +810,9 @@ public class RedisUtil { * @return */ public static Object lrightPop(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } return redisTemplate.opsForList().rightPop(key); } @@ -687,6 +822,9 @@ public class RedisUtil { * @return true / false */ public static List keys(String key) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } try { Set set = redisTemplate.keys(key); return new ArrayList<>(set); @@ -727,6 +865,9 @@ public class RedisUtil { * @return */ public static List scan(String query) { + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } Set resultKeys = (Set) redisTemplate.execute((RedisCallback>) connection -> { ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); Cursor scan = connection.scan(scanOptions); @@ -743,9 +884,10 @@ public class RedisUtil { // ============================== 消息发送与订阅 ============================== public static void convertAndSend(String channel, JSONObject msg) { -// redisTemplate.convertAndSend(channel, msg); + if (redisTemplate == null) { + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); + } redisTemplate.convertAndSend(channel, msg); - } }