Commit 4f2d47385d3535edf73634831c56f7fc18ff3494
1 parent
053cd130
去除redis工具类直接使用RedisTemplate存取数据
Showing
18 changed files
with
1251 additions
and
1177 deletions
src/main/java/com/genersoft/iot/vmp/conf/redis/RedisConfig.java
| 1 | 1 | package com.genersoft.iot.vmp.conf.redis; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -import com.genersoft.iot.vmp.common.VideoManagerConstants; | |
| 5 | -import com.genersoft.iot.vmp.service.redisMsg.*; | |
| 6 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | -import org.springframework.cache.annotation.CachingConfigurerSupport; | |
| 4 | +import com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer; | |
| 8 | 5 | import org.springframework.context.annotation.Bean; |
| 9 | 6 | import org.springframework.context.annotation.Configuration; |
| 10 | 7 | import org.springframework.core.annotation.Order; |
| 11 | 8 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 12 | 9 | import org.springframework.data.redis.core.RedisTemplate; |
| 13 | -import org.springframework.data.redis.listener.PatternTopic; | |
| 14 | -import org.springframework.data.redis.listener.RedisMessageListenerContainer; | |
| 15 | 10 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 16 | 11 | |
| 17 | -import com.genersoft.iot.vmp.utils.redis.FastJsonRedisSerializer; | |
| 18 | - | |
| 19 | 12 | |
| 20 | 13 | /** |
| 21 | - * @description:Redis中间件配置类,使用spring-data-redis集成,自动从application.yml中加载redis配置 | |
| 22 | - * @author: swwheihei | |
| 23 | - * @date: 2019年5月30日 上午10:58:25 | |
| 14 | + * Redis中间件配置类,使用spring-data-redis集成,自动从application.yml中加载redis配置 | |
| 15 | + * swwheihei | |
| 16 | + * 2019年5月30日 上午10:58:25 | |
| 24 | 17 | * |
| 25 | 18 | */ |
| 26 | 19 | @Configuration |
| 27 | 20 | @Order(value=1) |
| 28 | -public class RedisConfig extends CachingConfigurerSupport { | |
| 29 | - | |
| 30 | - @Autowired | |
| 31 | - private RedisGpsMsgListener redisGPSMsgListener; | |
| 32 | - | |
| 33 | - @Autowired | |
| 34 | - private RedisAlarmMsgListener redisAlarmMsgListener; | |
| 35 | - | |
| 36 | - @Autowired | |
| 37 | - private RedisStreamMsgListener redisStreamMsgListener; | |
| 38 | - | |
| 39 | - @Autowired | |
| 40 | - private RedisGbPlayMsgListener redisGbPlayMsgListener; | |
| 41 | - | |
| 42 | - @Autowired | |
| 43 | - private RedisPushStreamStatusMsgListener redisPushStreamStatusMsgListener; | |
| 21 | +public class RedisConfig { | |
| 44 | 22 | |
| 45 | - @Autowired | |
| 46 | - private RedisPushStreamStatusListMsgListener redisPushStreamListMsgListener; | |
| 47 | - | |
| 48 | - @Autowired | |
| 49 | - private RedisPushStreamResponseListener redisPushStreamResponseListener; | |
| 50 | 23 | |
| 51 | 24 | @Bean |
| 52 | 25 | public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { |
| 53 | 26 | RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); |
| 54 | 27 | // 使用fastJson序列化 |
| 55 | - FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); | |
| 28 | + GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer(); | |
| 56 | 29 | // value值的序列化采用fastJsonRedisSerializer |
| 57 | 30 | redisTemplate.setValueSerializer(fastJsonRedisSerializer); |
| 58 | 31 | redisTemplate.setHashValueSerializer(fastJsonRedisSerializer); |
| ... | ... | @@ -63,27 +36,4 @@ public class RedisConfig extends CachingConfigurerSupport { |
| 63 | 36 | redisTemplate.setConnectionFactory(redisConnectionFactory); |
| 64 | 37 | return redisTemplate; |
| 65 | 38 | } |
| 66 | - | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * redis消息监听器容器 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器 | |
| 70 | - * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理 | |
| 71 | - * | |
| 72 | - * @param connectionFactory | |
| 73 | - * @return | |
| 74 | - */ | |
| 75 | - @Bean | |
| 76 | - RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { | |
| 77 | - | |
| 78 | - RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | |
| 79 | - container.setConnectionFactory(connectionFactory); | |
| 80 | - container.addMessageListener(redisGPSMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_GPS)); | |
| 81 | - container.addMessageListener(redisAlarmMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM_RECEIVE)); | |
| 82 | - container.addMessageListener(redisStreamMsgListener, new PatternTopic(VideoManagerConstants.WVP_MSG_STREAM_CHANGE_PREFIX + "PUSH")); | |
| 83 | - container.addMessageListener(redisGbPlayMsgListener, new PatternTopic(RedisGbPlayMsgListener.WVP_PUSH_STREAM_KEY)); | |
| 84 | - container.addMessageListener(redisPushStreamStatusMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_STATUS_CHANGE)); | |
| 85 | - container.addMessageListener(redisPushStreamListMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_LIST_CHANGE)); | |
| 86 | - container.addMessageListener(redisPushStreamResponseListener, new PatternTopic(VideoManagerConstants.VM_MSG_STREAM_PUSH_RESPONSE)); | |
| 87 | - return container; | |
| 88 | - } | |
| 89 | 39 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/redis/RedisMsgListenConfig.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.conf.redis; | |
| 2 | + | |
| 3 | + | |
| 4 | +import com.genersoft.iot.vmp.common.VideoManagerConstants; | |
| 5 | +import com.genersoft.iot.vmp.service.redisMsg.*; | |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | +import org.springframework.context.annotation.Bean; | |
| 8 | +import org.springframework.context.annotation.Configuration; | |
| 9 | +import org.springframework.core.annotation.Order; | |
| 10 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | |
| 11 | +import org.springframework.data.redis.listener.PatternTopic; | |
| 12 | +import org.springframework.data.redis.listener.RedisMessageListenerContainer; | |
| 13 | + | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * @description:Redis中间件配置类,使用spring-data-redis集成,自动从application.yml中加载redis配置 | |
| 17 | + * @author: swwheihei | |
| 18 | + * @date: 2019年5月30日 上午10:58:25 | |
| 19 | + * | |
| 20 | + */ | |
| 21 | +@Configuration | |
| 22 | +@Order(value=1) | |
| 23 | +public class RedisMsgListenConfig { | |
| 24 | + | |
| 25 | + @Autowired | |
| 26 | + private RedisGpsMsgListener redisGPSMsgListener; | |
| 27 | + | |
| 28 | + @Autowired | |
| 29 | + private RedisAlarmMsgListener redisAlarmMsgListener; | |
| 30 | + | |
| 31 | + @Autowired | |
| 32 | + private RedisStreamMsgListener redisStreamMsgListener; | |
| 33 | + | |
| 34 | + @Autowired | |
| 35 | + private RedisGbPlayMsgListener redisGbPlayMsgListener; | |
| 36 | + | |
| 37 | + @Autowired | |
| 38 | + private RedisPushStreamStatusMsgListener redisPushStreamStatusMsgListener; | |
| 39 | + | |
| 40 | + @Autowired | |
| 41 | + private RedisPushStreamStatusListMsgListener redisPushStreamListMsgListener; | |
| 42 | + | |
| 43 | + @Autowired | |
| 44 | + private RedisPushStreamResponseListener redisPushStreamResponseListener; | |
| 45 | + | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * redis消息监听器容器 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器 | |
| 49 | + * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理 | |
| 50 | + * | |
| 51 | + * @param connectionFactory | |
| 52 | + * @return | |
| 53 | + */ | |
| 54 | + @Bean | |
| 55 | + RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { | |
| 56 | + | |
| 57 | + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | |
| 58 | + container.setConnectionFactory(connectionFactory); | |
| 59 | + container.addMessageListener(redisGPSMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_GPS)); | |
| 60 | + container.addMessageListener(redisAlarmMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM_RECEIVE)); | |
| 61 | + container.addMessageListener(redisStreamMsgListener, new PatternTopic(VideoManagerConstants.WVP_MSG_STREAM_CHANGE_PREFIX + "PUSH")); | |
| 62 | + container.addMessageListener(redisGbPlayMsgListener, new PatternTopic(RedisGbPlayMsgListener.WVP_PUSH_STREAM_KEY)); | |
| 63 | + container.addMessageListener(redisPushStreamStatusMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_STATUS_CHANGE)); | |
| 64 | + container.addMessageListener(redisPushStreamListMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_LIST_CHANGE)); | |
| 65 | + container.addMessageListener(redisPushStreamResponseListener, new PatternTopic(VideoManagerConstants.VM_MSG_STREAM_PUSH_RESPONSE)); | |
| 66 | + return container; | |
| 67 | + } | |
| 68 | +} | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java
| ... | ... | @@ -72,22 +72,23 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 72 | 72 | **/ |
| 73 | 73 | @Override |
| 74 | 74 | public void configure(WebSecurity web) { |
| 75 | - | |
| 76 | - ArrayList<String> matchers = new ArrayList<>(); | |
| 77 | - matchers.add("/"); | |
| 78 | - matchers.add("/#/**"); | |
| 79 | - matchers.add("/static/**"); | |
| 80 | - matchers.add("/index.html"); | |
| 81 | - matchers.add("/doc.html"); | |
| 82 | - matchers.add("/webjars/**"); | |
| 83 | - matchers.add("/swagger-resources/**"); | |
| 84 | - matchers.add("/v3/api-docs/**"); | |
| 85 | - matchers.add("/js/**"); | |
| 86 | - matchers.add("/api/device/query/snap/**"); | |
| 87 | - matchers.add("/record_proxy/*/**"); | |
| 88 | - matchers.addAll(userSetting.getInterfaceAuthenticationExcludes()); | |
| 89 | - // 可以直接访问的静态数据 | |
| 90 | - web.ignoring().antMatchers(matchers.toArray(new String[0])); | |
| 75 | + if (userSetting.isInterfaceAuthentication()) { | |
| 76 | + ArrayList<String> matchers = new ArrayList<>(); | |
| 77 | + matchers.add("/"); | |
| 78 | + matchers.add("/#/**"); | |
| 79 | + matchers.add("/static/**"); | |
| 80 | + matchers.add("/index.html"); | |
| 81 | + matchers.add("/doc.html"); | |
| 82 | + matchers.add("/webjars/**"); | |
| 83 | + matchers.add("/swagger-resources/**"); | |
| 84 | + matchers.add("/v3/api-docs/**"); | |
| 85 | + matchers.add("/js/**"); | |
| 86 | + matchers.add("/api/device/query/snap/**"); | |
| 87 | + matchers.add("/record_proxy/*/**"); | |
| 88 | + matchers.addAll(userSetting.getInterfaceAuthenticationExcludes()); | |
| 89 | + // 可以直接访问的静态数据 | |
| 90 | + web.ignoring().antMatchers(matchers.toArray(new String[0])); | |
| 91 | + } | |
| 91 | 92 | } |
| 92 | 93 | |
| 93 | 94 | /** | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java
| ... | ... | @@ -8,16 +8,15 @@ import com.genersoft.iot.vmp.utils.JsonUtil; |
| 8 | 8 | import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
| 9 | 9 | import gov.nist.javax.sip.message.SIPResponse; |
| 10 | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 11 | 12 | import org.springframework.stereotype.Component; |
| 12 | 13 | import org.springframework.util.ObjectUtils; |
| 13 | 14 | |
| 14 | 15 | import java.util.ArrayList; |
| 15 | 16 | import java.util.List; |
| 16 | 17 | |
| 17 | -/** | |
| 18 | - * @description:视频流session管理器,管理视频预览、预览回放的通信句柄 | |
| 19 | - * @author: swwheihei | |
| 20 | - * @date: 2020年5月13日 下午4:03:02 | |
| 18 | +/** | |
| 19 | + * 视频流session管理器,管理视频预览、预览回放的通信句柄 | |
| 21 | 20 | */ |
| 22 | 21 | @Component |
| 23 | 22 | public class VideoStreamSessionManager { |
| ... | ... | @@ -25,6 +24,9 @@ public class VideoStreamSessionManager { |
| 25 | 24 | @Autowired |
| 26 | 25 | private UserSetting userSetting; |
| 27 | 26 | |
| 27 | + @Autowired | |
| 28 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 29 | + | |
| 28 | 30 | public enum SessionType { |
| 29 | 31 | play, |
| 30 | 32 | playback, |
| ... | ... | @@ -52,7 +54,7 @@ public class VideoStreamSessionManager { |
| 52 | 54 | ssrcTransaction.setMediaServerId(mediaServerId); |
| 53 | 55 | ssrcTransaction.setType(type); |
| 54 | 56 | |
| 55 | - RedisUtil.set(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() | |
| 57 | + redisTemplate.opsForValue().set(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() | |
| 56 | 58 | + "_" + deviceId + "_" + channelId + "_" + callId + "_" + stream, ssrcTransaction); |
| 57 | 59 | } |
| 58 | 60 | |
| ... | ... | @@ -71,11 +73,11 @@ public class VideoStreamSessionManager { |
| 71 | 73 | stream ="*"; |
| 72 | 74 | } |
| 73 | 75 | String key = VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" + deviceId + "_" + channelId + "_" + callId+ "_" + stream; |
| 74 | - List<Object> scanResult = RedisUtil.scan(key); | |
| 76 | + List<Object> scanResult = RedisUtil.scan(redisTemplate, key); | |
| 75 | 77 | if (scanResult.size() == 0) { |
| 76 | 78 | return null; |
| 77 | 79 | } |
| 78 | - return (SsrcTransaction)RedisUtil.get((String) scanResult.get(0)); | |
| 80 | + return (SsrcTransaction)redisTemplate.opsForValue().get(scanResult.get(0)); | |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | 83 | public List<SsrcTransaction> getSsrcTransactionForAll(String deviceId, String channelId, String callId, String stream){ |
| ... | ... | @@ -92,13 +94,13 @@ public class VideoStreamSessionManager { |
| 92 | 94 | stream ="*"; |
| 93 | 95 | } |
| 94 | 96 | String key = VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" + deviceId + "_" + channelId + "_" + callId+ "_" + stream; |
| 95 | - List<Object> scanResult = RedisUtil.scan(key); | |
| 97 | + List<Object> scanResult = RedisUtil.scan(redisTemplate, key); | |
| 96 | 98 | if (scanResult.size() == 0) { |
| 97 | 99 | return null; |
| 98 | 100 | } |
| 99 | 101 | List<SsrcTransaction> result = new ArrayList<>(); |
| 100 | 102 | for (Object keyObj : scanResult) { |
| 101 | - result.add((SsrcTransaction)RedisUtil.get((String) keyObj)); | |
| 103 | + result.add((SsrcTransaction)redisTemplate.opsForValue().get(keyObj)); | |
| 102 | 104 | } |
| 103 | 105 | return result; |
| 104 | 106 | } |
| ... | ... | @@ -124,17 +126,17 @@ public class VideoStreamSessionManager { |
| 124 | 126 | if (ssrcTransaction == null) { |
| 125 | 127 | return; |
| 126 | 128 | } |
| 127 | - RedisUtil.del(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" | |
| 129 | + redisTemplate.delete(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" | |
| 128 | 130 | + deviceId + "_" + channelId + "_" + ssrcTransaction.getCallId() + "_" + ssrcTransaction.getStream()); |
| 129 | 131 | } |
| 130 | 132 | |
| 131 | 133 | |
| 132 | 134 | public List<SsrcTransaction> getAllSsrc() { |
| 133 | - List<Object> ssrcTransactionKeys = RedisUtil.scan(String.format("%s_*_*_*_*", VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX+ userSetting.getServerId())); | |
| 135 | + List<Object> ssrcTransactionKeys = RedisUtil.scan(redisTemplate, String.format("%s_*_*_*_*", VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX+ userSetting.getServerId())); | |
| 134 | 136 | List<SsrcTransaction> result= new ArrayList<>(); |
| 135 | - for (int i = 0; i < ssrcTransactionKeys.size(); i++) { | |
| 136 | - String key = (String)ssrcTransactionKeys.get(i); | |
| 137 | - SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(key, SsrcTransaction.class); | |
| 137 | + for (Object ssrcTransactionKey : ssrcTransactionKeys) { | |
| 138 | + String key = (String) ssrcTransactionKey; | |
| 139 | + SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(redisTemplate, key, SsrcTransaction.class); | |
| 138 | 140 | result.add(ssrcTransaction); |
| 139 | 141 | } |
| 140 | 142 | return result; | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java
| ... | ... | @@ -20,7 +20,7 @@ public interface ISIPCommanderForPlatform { |
| 20 | 20 | void register(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; |
| 21 | 21 | |
| 22 | 22 | void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; |
| 23 | - void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean registerAgain, boolean isRegister) throws SipException, InvalidArgumentException, ParseException; | |
| 23 | + void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean isRegister) throws SipException, InvalidArgumentException, ParseException; | |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * 向上级平台注销 | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java
| ... | ... | @@ -45,7 +45,7 @@ public class SIPRequestHeaderPlarformProvider { |
| 45 | 45 | @Autowired |
| 46 | 46 | private IRedisCatchStorage redisCatchStorage; |
| 47 | 47 | |
| 48 | - public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, long CSeq, String fromTag, String toTag, CallIdHeader callIdHeader, boolean isRegister) throws ParseException, InvalidArgumentException, PeerUnavailableException { | |
| 48 | + public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, long CSeq, String fromTag, String toTag, CallIdHeader callIdHeader, int expires) throws ParseException, InvalidArgumentException, PeerUnavailableException { | |
| 49 | 49 | Request request = null; |
| 50 | 50 | String sipAddress = parentPlatform.getDeviceIp() + ":" + parentPlatform.getDevicePort(); |
| 51 | 51 | //请求行 |
| ... | ... | @@ -78,8 +78,8 @@ public class SIPRequestHeaderPlarformProvider { |
| 78 | 78 | .createSipURI(parentPlatform.getDeviceGBId(), sipAddress)); |
| 79 | 79 | request.addHeader(sipLayer.getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); |
| 80 | 80 | |
| 81 | - ExpiresHeader expires = sipLayer.getSipFactory().createHeaderFactory().createExpiresHeader(isRegister ? parentPlatform.getExpires() : 0); | |
| 82 | - request.addHeader(expires); | |
| 81 | + ExpiresHeader expiresHeader = sipLayer.getSipFactory().createHeaderFactory().createExpiresHeader(expires); | |
| 82 | + request.addHeader(expiresHeader); | |
| 83 | 83 | |
| 84 | 84 | request.addHeader(SipUtils.createUserAgentHeader(sipLayer.getSipFactory(), gitUtil)); |
| 85 | 85 | |
| ... | ... | @@ -87,10 +87,10 @@ public class SIPRequestHeaderPlarformProvider { |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, String fromTag, String toTag, |
| 90 | - WWWAuthenticateHeader www , CallIdHeader callIdHeader, boolean isRegister) throws ParseException, PeerUnavailableException, InvalidArgumentException { | |
| 90 | + WWWAuthenticateHeader www , CallIdHeader callIdHeader, int expires) throws ParseException, PeerUnavailableException, InvalidArgumentException { | |
| 91 | 91 | |
| 92 | 92 | |
| 93 | - Request registerRequest = createRegisterRequest(parentPlatform, redisCatchStorage.getCSEQ(), fromTag, toTag, callIdHeader, isRegister); | |
| 93 | + Request registerRequest = createRegisterRequest(parentPlatform, redisCatchStorage.getCSEQ(), fromTag, toTag, callIdHeader, expires); | |
| 94 | 94 | SipURI requestURI = sipLayer.getSipFactory().createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort()); |
| 95 | 95 | if (www == null) { |
| 96 | 96 | AuthorizationHeader authorizationHeader = sipLayer.getSipFactory().createHeaderFactory().createAuthorizationHeader("Digest"); | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
| ... | ... | @@ -71,23 +71,23 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform { |
| 71 | 71 | |
| 72 | 72 | @Override |
| 73 | 73 | public void register(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { |
| 74 | - register(parentPlatform, null, null, errorEvent, okEvent, false, true); | |
| 74 | + register(parentPlatform, null, null, errorEvent, okEvent, true); | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | @Override |
| 78 | 78 | public void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { |
| 79 | 79 | |
| 80 | - register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, false, true); | |
| 80 | + register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, true); | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | @Override |
| 84 | 84 | public void unregister(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { |
| 85 | - register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, false, false); | |
| 85 | + register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, false); | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | @Override |
| 89 | 89 | public void register(ParentPlatform parentPlatform, @Nullable SipTransactionInfo sipTransactionInfo, @Nullable WWWAuthenticateHeader www, |
| 90 | - SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean registerAgain, boolean isRegister) throws SipException, InvalidArgumentException, ParseException { | |
| 90 | + SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean isRegister) throws SipException, InvalidArgumentException, ParseException { | |
| 91 | 91 | Request request; |
| 92 | 92 | |
| 93 | 93 | CallIdHeader callIdHeader = sipSender.getNewCallIdHeader(parentPlatform.getDeviceIp(),parentPlatform.getTransport()); |
| ... | ... | @@ -105,10 +105,10 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform { |
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - if (!registerAgain ) { | |
| 108 | + if (www == null ) { | |
| 109 | 109 | request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, |
| 110 | 110 | redisCatchStorage.getCSEQ(), fromTag, |
| 111 | - toTag, callIdHeader, isRegister); | |
| 111 | + toTag, callIdHeader, isRegister? parentPlatform.getExpires() : 0); | |
| 112 | 112 | // 将 callid 写入缓存, 等注册成功可以更新状态 |
| 113 | 113 | String callIdFromHeader = callIdHeader.getCallId(); |
| 114 | 114 | redisCatchStorage.updatePlatformRegisterInfo(callIdFromHeader, PlatformRegisterInfo.getInstance(parentPlatform.getServerGBId(), isRegister)); |
| ... | ... | @@ -126,7 +126,7 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform { |
| 126 | 126 | }); |
| 127 | 127 | |
| 128 | 128 | }else { |
| 129 | - request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, fromTag, toTag, www, callIdHeader, isRegister); | |
| 129 | + request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, fromTag, toTag, www, callIdHeader, isRegister? parentPlatform.getExpires() : 0); | |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | sipSender.transmitRequest(parentPlatform.getDeviceIp(), request, null, okEvent); |
| ... | ... | @@ -497,10 +497,10 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform { |
| 497 | 497 | private void sendNotify(ParentPlatform parentPlatform, String catalogXmlContent, |
| 498 | 498 | SubscribeInfo subscribeInfo, SipSubscribe.Event errorEvent, SipSubscribe.Event okEvent ) |
| 499 | 499 | throws SipException, ParseException, InvalidArgumentException { |
| 500 | - MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipLayer.getSipFactory().createMessageFactory(); | |
| 500 | + MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipLayer.getSipFactory().createMessageFactory(); | |
| 501 | 501 | String characterSet = parentPlatform.getCharacterSet(); |
| 502 | - // 设置编码, 防止中文乱码 | |
| 503 | - messageFactory.setDefaultContentEncodingCharset(characterSet); | |
| 502 | + // 设置编码, 防止中文乱码 | |
| 503 | + messageFactory.setDefaultContentEncodingCharset(characterSet); | |
| 504 | 504 | |
| 505 | 505 | SIPRequest notifyRequest = headerProviderPlatformProvider.createNotifyRequest(parentPlatform, catalogXmlContent, subscribeInfo); |
| 506 | 506 | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/RecordInfoResponseMessageHandler.java
| ... | ... | @@ -10,7 +10,6 @@ import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessag |
| 10 | 10 | import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.ResponseMessageHandler; |
| 11 | 11 | import com.genersoft.iot.vmp.utils.DateUtil; |
| 12 | 12 | import com.genersoft.iot.vmp.utils.UJson; |
| 13 | -import com.genersoft.iot.vmp.utils.redis.RedisUtil; | |
| 14 | 13 | import gov.nist.javax.sip.message.SIPRequest; |
| 15 | 14 | import org.dom4j.Element; |
| 16 | 15 | import org.slf4j.Logger; |
| ... | ... | @@ -18,6 +17,7 @@ import org.slf4j.LoggerFactory; |
| 18 | 17 | import org.springframework.beans.factory.InitializingBean; |
| 19 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
| 20 | 19 | import org.springframework.beans.factory.annotation.Qualifier; |
| 20 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 21 | 21 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 22 | 22 | import org.springframework.stereotype.Component; |
| 23 | 23 | import org.springframework.util.ObjectUtils; |
| ... | ... | @@ -29,6 +29,7 @@ import javax.sip.message.Response; |
| 29 | 29 | import java.text.ParseException; |
| 30 | 30 | import java.util.*; |
| 31 | 31 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 32 | +import java.util.concurrent.TimeUnit; | |
| 32 | 33 | import java.util.stream.Collectors; |
| 33 | 34 | |
| 34 | 35 | import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText; |
| ... | ... | @@ -57,6 +58,9 @@ public class RecordInfoResponseMessageHandler extends SIPRequestProcessorParent |
| 57 | 58 | @Autowired |
| 58 | 59 | private ThreadPoolTaskExecutor taskExecutor; |
| 59 | 60 | |
| 61 | + @Autowired | |
| 62 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 63 | + | |
| 60 | 64 | private Long recordInfoTtl = 1800L; |
| 61 | 65 | |
| 62 | 66 | @Override |
| ... | ... | @@ -130,10 +134,11 @@ public class RecordInfoResponseMessageHandler extends SIPRequestProcessorParent |
| 130 | 134 | .collect(Collectors.toMap(record -> record.getStartTime()+ record.getEndTime(), UJson::writeJson)); |
| 131 | 135 | // 获取任务结果数据 |
| 132 | 136 | String resKey = VideoManagerConstants.REDIS_RECORD_INFO_RES_PRE + channelId + sn; |
| 133 | - RedisUtil.hmset(resKey, map, recordInfoTtl); | |
| 137 | + redisTemplate.opsForHash().putAll(resKey, map); | |
| 138 | + redisTemplate.expire(resKey, recordInfoTtl, TimeUnit.SECONDS); | |
| 134 | 139 | String resCountKey = VideoManagerConstants.REDIS_RECORD_INFO_RES_COUNT_PRE + channelId + sn; |
| 135 | - long incr = RedisUtil.incr(resCountKey, map.size()); | |
| 136 | - RedisUtil.expire(resCountKey, recordInfoTtl); | |
| 140 | + long incr = redisTemplate.opsForValue().increment(resCountKey, map.size()); | |
| 141 | + redisTemplate.expire(resCountKey, recordInfoTtl, TimeUnit.SECONDS); | |
| 137 | 142 | recordInfo.setRecordList(recordList); |
| 138 | 143 | recordInfo.setCount(Math.toIntExact(incr)); |
| 139 | 144 | eventPublisher.recordEndEventPush(recordInfo); |
| ... | ... | @@ -141,7 +146,7 @@ public class RecordInfoResponseMessageHandler extends SIPRequestProcessorParent |
| 141 | 146 | return; |
| 142 | 147 | } |
| 143 | 148 | // 已接收完成 |
| 144 | - List<RecordItem> resList = RedisUtil.hmget(resKey).values().stream().map(e -> UJson.readJson(e.toString(), RecordItem.class)).collect(Collectors.toList()); | |
| 149 | + List<RecordItem> resList = redisTemplate.opsForHash().entries(resKey).values().stream().map(e -> UJson.readJson(e.toString(), RecordItem.class)).collect(Collectors.toList()); | |
| 145 | 150 | if (resList.size() < sumNum) { |
| 146 | 151 | return; |
| 147 | 152 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java
| ... | ... | @@ -92,7 +92,7 @@ public class RegisterResponseProcessor extends SIPResponseProcessorAbstract { |
| 92 | 92 | WWWAuthenticateHeader www = (WWWAuthenticateHeader)response.getHeader(WWWAuthenticateHeader.NAME); |
| 93 | 93 | SipTransactionInfo sipTransactionInfo = new SipTransactionInfo(response); |
| 94 | 94 | try { |
| 95 | - sipCommanderForPlatform.register(parentPlatform, sipTransactionInfo, www, null, null, true, platformRegisterInfo.isRegister()); | |
| 95 | + sipCommanderForPlatform.register(parentPlatform, sipTransactionInfo, www, null, null, platformRegisterInfo.isRegister()); | |
| 96 | 96 | } catch (SipException | InvalidArgumentException | ParseException e) { |
| 97 | 97 | logger.error("[命令发送失败] 国标级联 再次注册: {}", e.getMessage()); |
| 98 | 98 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
| ... | ... | @@ -10,7 +10,6 @@ import com.genersoft.iot.vmp.conf.UserSetting; |
| 10 | 10 | import com.genersoft.iot.vmp.conf.exception.ControllerException; |
| 11 | 11 | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; |
| 12 | 12 | import com.genersoft.iot.vmp.gb28181.session.SsrcConfig; |
| 13 | -import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | |
| 14 | 13 | import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils; |
| 15 | 14 | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; |
| 16 | 15 | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; |
| ... | ... | @@ -33,6 +32,7 @@ import org.slf4j.Logger; |
| 33 | 32 | import org.slf4j.LoggerFactory; |
| 34 | 33 | import org.springframework.beans.factory.annotation.Autowired; |
| 35 | 34 | import org.springframework.beans.factory.annotation.Value; |
| 35 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 36 | 36 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 37 | 37 | import org.springframework.stereotype.Service; |
| 38 | 38 | import org.springframework.transaction.TransactionDefinition; |
| ... | ... | @@ -75,13 +75,11 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 75 | 75 | private MediaServerMapper mediaServerMapper; |
| 76 | 76 | |
| 77 | 77 | @Autowired |
| 78 | - DataSourceTransactionManager dataSourceTransactionManager; | |
| 78 | + private DataSourceTransactionManager dataSourceTransactionManager; | |
| 79 | 79 | |
| 80 | 80 | @Autowired |
| 81 | - TransactionDefinition transactionDefinition; | |
| 81 | + private TransactionDefinition transactionDefinition; | |
| 82 | 82 | |
| 83 | - @Autowired | |
| 84 | - private VideoStreamSessionManager streamSession; | |
| 85 | 83 | |
| 86 | 84 | @Autowired |
| 87 | 85 | private ZLMRTPServerFactory zlmrtpServerFactory; |
| ... | ... | @@ -95,6 +93,9 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 95 | 93 | @Autowired |
| 96 | 94 | private IRedisCatchStorage redisCatchStorage; |
| 97 | 95 | |
| 96 | + @Autowired | |
| 97 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 98 | + | |
| 98 | 99 | /** |
| 99 | 100 | * 初始化 |
| 100 | 101 | */ |
| ... | ... | @@ -109,12 +110,13 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 109 | 110 | if (mediaServerItem.getSsrcConfig() == null) { |
| 110 | 111 | SsrcConfig ssrcConfig = new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain()); |
| 111 | 112 | mediaServerItem.setSsrcConfig(ssrcConfig); |
| 112 | - RedisUtil.set(VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(), mediaServerItem); | |
| 113 | + redisTemplate.opsForValue().set(VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(), mediaServerItem); | |
| 113 | 114 | } |
| 114 | 115 | // 查询redis是否存在此mediaServer |
| 115 | 116 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(); |
| 116 | - if (!RedisUtil.hasKey(key)) { | |
| 117 | - RedisUtil.set(key, mediaServerItem); | |
| 117 | + Boolean hasKey = redisTemplate.hasKey(key); | |
| 118 | + if (hasKey != null && ! hasKey) { | |
| 119 | + redisTemplate.opsForValue().set(key, mediaServerItem); | |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | 122 | } |
| ... | ... | @@ -159,7 +161,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 159 | 161 | } else { |
| 160 | 162 | rtpServerPort = mediaServerItem.getRtpProxyPort(); |
| 161 | 163 | } |
| 162 | - RedisUtil.set(key, mediaServerItem); | |
| 164 | + redisTemplate.opsForValue().set(key, mediaServerItem); | |
| 163 | 165 | return new SSRCInfo(rtpServerPort, ssrc, streamId); |
| 164 | 166 | } |
| 165 | 167 | } |
| ... | ... | @@ -193,7 +195,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 193 | 195 | ssrcConfig.releaseSsrc(ssrc); |
| 194 | 196 | mediaServerItem.setSsrcConfig(ssrcConfig); |
| 195 | 197 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(); |
| 196 | - RedisUtil.set(key, mediaServerItem); | |
| 198 | + redisTemplate.opsForValue().set(key, mediaServerItem); | |
| 197 | 199 | } |
| 198 | 200 | |
| 199 | 201 | /** |
| ... | ... | @@ -202,7 +204,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 202 | 204 | @Override |
| 203 | 205 | public void clearRTPServer(MediaServerItem mediaServerItem) { |
| 204 | 206 | mediaServerItem.setSsrcConfig(new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain())); |
| 205 | - RedisUtil.zAdd(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), mediaServerItem.getId(), 0); | |
| 207 | + redisTemplate.opsForZSet().add(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), mediaServerItem.getId(), 0); | |
| 206 | 208 | |
| 207 | 209 | } |
| 208 | 210 | |
| ... | ... | @@ -224,22 +226,22 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 224 | 226 | ); |
| 225 | 227 | } |
| 226 | 228 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItemInDataBase.getId(); |
| 227 | - RedisUtil.set(key, mediaServerItemInDataBase); | |
| 229 | + redisTemplate.opsForValue().set(key, mediaServerItemInDataBase); | |
| 228 | 230 | } |
| 229 | 231 | |
| 230 | 232 | @Override |
| 231 | 233 | public List<MediaServerItem> getAll() { |
| 232 | 234 | List<MediaServerItem> result = new ArrayList<>(); |
| 233 | - List<Object> mediaServerKeys = RedisUtil.scan(String.format("%S*", VideoManagerConstants.MEDIA_SERVER_PREFIX+ userSetting.getServerId() + "_" )); | |
| 235 | + List<Object> mediaServerKeys = RedisUtil.scan(redisTemplate, String.format("%S*", VideoManagerConstants.MEDIA_SERVER_PREFIX+ userSetting.getServerId() + "_" )); | |
| 234 | 236 | String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 235 | 237 | for (Object mediaServerKey : mediaServerKeys) { |
| 236 | 238 | String key = (String) mediaServerKey; |
| 237 | - MediaServerItem mediaServerItem = JsonUtil.redisJsonToObject(key, MediaServerItem.class); | |
| 239 | + MediaServerItem mediaServerItem = JsonUtil.redisJsonToObject(redisTemplate, key, MediaServerItem.class); | |
| 238 | 240 | if (Objects.isNull(mediaServerItem)) { |
| 239 | 241 | continue; |
| 240 | 242 | } |
| 241 | 243 | // 检查状态 |
| 242 | - Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId()); | |
| 244 | + Double aDouble = redisTemplate.opsForZSet().score(onlineKey, mediaServerItem.getId()); | |
| 243 | 245 | if (aDouble != null) { |
| 244 | 246 | mediaServerItem.setStatus(true); |
| 245 | 247 | } |
| ... | ... | @@ -265,13 +267,14 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 265 | 267 | @Override |
| 266 | 268 | public List<MediaServerItem> getAllOnline() { |
| 267 | 269 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 268 | - Set<String> mediaServerIdSet = RedisUtil.zRevRange(key, 0, -1); | |
| 270 | + Set<Object> mediaServerIdSet = redisTemplate.opsForZSet().reverseRange(key, 0, -1); | |
| 269 | 271 | |
| 270 | 272 | List<MediaServerItem> result = new ArrayList<>(); |
| 271 | 273 | if (mediaServerIdSet != null && mediaServerIdSet.size() > 0) { |
| 272 | - for (String mediaServerId : mediaServerIdSet) { | |
| 273 | - String serverKey = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; | |
| 274 | - result.add((MediaServerItem) RedisUtil.get(serverKey)); | |
| 274 | + for (Object mediaServerId : mediaServerIdSet) { | |
| 275 | + String mediaServerIdStr = (String) mediaServerId; | |
| 276 | + String serverKey = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerIdStr; | |
| 277 | + result.add((MediaServerItem) redisTemplate.opsForValue().get(serverKey)); | |
| 275 | 278 | } |
| 276 | 279 | } |
| 277 | 280 | Collections.reverse(result); |
| ... | ... | @@ -289,7 +292,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 289 | 292 | return null; |
| 290 | 293 | } |
| 291 | 294 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; |
| 292 | - return JsonUtil.redisJsonToObject(key, MediaServerItem.class); | |
| 295 | + return JsonUtil.redisJsonToObject(redisTemplate, key, MediaServerItem.class); | |
| 293 | 296 | } |
| 294 | 297 | |
| 295 | 298 | @Override |
| ... | ... | @@ -301,7 +304,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 301 | 304 | @Override |
| 302 | 305 | public void clearMediaServerForOnline() { |
| 303 | 306 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 304 | - RedisUtil.del(key); | |
| 307 | + redisTemplate.delete(key); | |
| 305 | 308 | } |
| 306 | 309 | |
| 307 | 310 | @Override |
| ... | ... | @@ -401,16 +404,16 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 401 | 404 | } |
| 402 | 405 | mediaServerMapper.update(serverItem); |
| 403 | 406 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + zlmServerConfig.getGeneralMediaServerId(); |
| 404 | - if (RedisUtil.get(key) == null) { | |
| 407 | + if (redisTemplate.opsForValue().get(key) == null) { | |
| 405 | 408 | SsrcConfig ssrcConfig = new SsrcConfig(zlmServerConfig.getGeneralMediaServerId(), null, sipConfig.getDomain()); |
| 406 | 409 | serverItem.setSsrcConfig(ssrcConfig); |
| 407 | 410 | }else { |
| 408 | - MediaServerItem mediaServerItemInRedis = JsonUtil.redisJsonToObject(key, MediaServerItem.class); | |
| 411 | + MediaServerItem mediaServerItemInRedis = JsonUtil.redisJsonToObject(redisTemplate, key, MediaServerItem.class); | |
| 409 | 412 | if (Objects.nonNull(mediaServerItemInRedis)) { |
| 410 | 413 | serverItem.setSsrcConfig(mediaServerItemInRedis.getSsrcConfig()); |
| 411 | 414 | } |
| 412 | 415 | } |
| 413 | - RedisUtil.set(key, serverItem); | |
| 416 | + redisTemplate.opsForValue().set(key, serverItem); | |
| 414 | 417 | resetOnlineServerItem(serverItem); |
| 415 | 418 | |
| 416 | 419 | |
| ... | ... | @@ -473,15 +476,15 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 473 | 476 | // 更新缓存 |
| 474 | 477 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 475 | 478 | // 使用zset的分数作为当前并发量, 默认值设置为0 |
| 476 | - if (RedisUtil.zScore(key, serverItem.getId()) == null) { // 不存在则设置默认值 已存在则重置 | |
| 477 | - RedisUtil.zAdd(key, serverItem.getId(), 0L); | |
| 479 | + if (redisTemplate.opsForZSet().score(key, serverItem.getId()) == null) { // 不存在则设置默认值 已存在则重置 | |
| 480 | + redisTemplate.opsForZSet().add(key, serverItem.getId(), 0L); | |
| 478 | 481 | // 查询服务流数量 |
| 479 | 482 | zlmresTfulUtils.getMediaList(serverItem, null, null, "rtsp",(mediaList ->{ |
| 480 | 483 | Integer code = mediaList.getInteger("code"); |
| 481 | 484 | if (code == 0) { |
| 482 | 485 | JSONArray data = mediaList.getJSONArray("data"); |
| 483 | 486 | if (data != null) { |
| 484 | - RedisUtil.zAdd(key, serverItem.getId(), data.size()); | |
| 487 | + redisTemplate.opsForZSet().add(key, serverItem.getId(), data.size()); | |
| 485 | 488 | } |
| 486 | 489 | } |
| 487 | 490 | })); |
| ... | ... | @@ -497,14 +500,14 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 497 | 500 | return; |
| 498 | 501 | } |
| 499 | 502 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 500 | - RedisUtil.zIncrScore(key, mediaServerId, 1); | |
| 503 | + redisTemplate.opsForZSet().incrementScore(key, mediaServerId, 1); | |
| 501 | 504 | |
| 502 | 505 | } |
| 503 | 506 | |
| 504 | 507 | @Override |
| 505 | 508 | public void removeCount(String mediaServerId) { |
| 506 | 509 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 507 | - RedisUtil.zIncrScore(key, mediaServerId, - 1); | |
| 510 | + redisTemplate.opsForZSet().incrementScore(key, mediaServerId, - 1); | |
| 508 | 511 | } |
| 509 | 512 | |
| 510 | 513 | /** |
| ... | ... | @@ -514,16 +517,14 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 514 | 517 | @Override |
| 515 | 518 | public MediaServerItem getMediaServerForMinimumLoad(Boolean hasAssist) { |
| 516 | 519 | String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); |
| 517 | - | |
| 518 | - if (RedisUtil.zSize(key) == null || RedisUtil.zSize(key) == 0) { | |
| 519 | - if (RedisUtil.zSize(key) == null || RedisUtil.zSize(key) == 0) { | |
| 520 | - logger.info("获取负载最低的节点时无在线节点"); | |
| 521 | - return null; | |
| 522 | - } | |
| 520 | + Long size = redisTemplate.opsForZSet().zCard(key); | |
| 521 | + if (size == null || size == 0) { | |
| 522 | + logger.info("获取负载最低的节点时无在线节点"); | |
| 523 | + return null; | |
| 523 | 524 | } |
| 524 | 525 | |
| 525 | 526 | // 获取分数最低的,及并发最低的 |
| 526 | - Set<Object> objects = RedisUtil.zRange(key, 0, -1); | |
| 527 | + Set<Object> objects = redisTemplate.opsForZSet().range(key, 0, -1); | |
| 527 | 528 | ArrayList<Object> mediaServerObjectS = new ArrayList<>(objects); |
| 528 | 529 | MediaServerItem mediaServerItem = null; |
| 529 | 530 | if (hasAssist == null) { |
| ... | ... | @@ -686,9 +687,9 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 686 | 687 | |
| 687 | 688 | @Override |
| 688 | 689 | public void delete(String id) { |
| 689 | - RedisUtil.zRemove(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), id); | |
| 690 | + redisTemplate.opsForZSet().remove(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), id); | |
| 690 | 691 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + id; |
| 691 | - RedisUtil.del(key); | |
| 692 | + redisTemplate.delete(key); | |
| 692 | 693 | } |
| 693 | 694 | @Override |
| 694 | 695 | public void deleteDb(String id){ |
| ... | ... | @@ -711,7 +712,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 711 | 712 | SsrcConfig ssrcConfig = new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain()); |
| 712 | 713 | mediaServerItem.setSsrcConfig(ssrcConfig); |
| 713 | 714 | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(); |
| 714 | - RedisUtil.set(key, mediaServerItem); | |
| 715 | + redisTemplate.opsForValue().set(key, mediaServerItem); | |
| 715 | 716 | clearRTPServer(mediaServerItem); |
| 716 | 717 | } |
| 717 | 718 | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + mediaServerItem.getId(); | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
| ... | ... | @@ -146,7 +146,7 @@ public class PlatformServiceImpl implements IPlatformService { |
| 146 | 146 | // 注销旧的 |
| 147 | 147 | try { |
| 148 | 148 | if (parentPlatformOld.isStatus()) { |
| 149 | - logger.info("保存平台{}时发现救平台在线,发送注销命令", parentPlatform.getDeviceGBId()); | |
| 149 | + logger.info("保存平台{}时发现救平台在线,发送注销命令", parentPlatformOld.getServerGBId()); | |
| 150 | 150 | commanderForPlatform.unregister(parentPlatformOld, parentPlatformCatchOld.getSipTransactionInfo(), null, eventResult -> { |
| 151 | 151 | logger.info("[国标级联] 注销成功, 平台:{}", parentPlatformOld.getServerGBId()); |
| 152 | 152 | }); |
| ... | ... | @@ -253,7 +253,7 @@ public class PlatformServiceImpl implements IPlatformService { |
| 253 | 253 | // 心跳成功 |
| 254 | 254 | // 清空之前的心跳超时计数 |
| 255 | 255 | ParentPlatformCatch platformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); |
| 256 | - if (platformCatch.getKeepAliveReply() > 0) { | |
| 256 | + if (platformCatch != null && platformCatch.getKeepAliveReply() > 0) { | |
| 257 | 257 | platformCatch.setKeepAliveReply(0); |
| 258 | 258 | redisCatchStorage.updatePlatformCatchInfo(platformCatch); |
| 259 | 259 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
| ... | ... | @@ -33,12 +33,12 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo; |
| 33 | 33 | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| 34 | 34 | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; |
| 35 | 35 | import com.genersoft.iot.vmp.utils.DateUtil; |
| 36 | -import com.genersoft.iot.vmp.utils.redis.RedisUtil; | |
| 37 | 36 | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; |
| 38 | 37 | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; |
| 39 | 38 | import org.slf4j.Logger; |
| 40 | 39 | import org.slf4j.LoggerFactory; |
| 41 | 40 | import org.springframework.beans.factory.annotation.Autowired; |
| 41 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 42 | 42 | import org.springframework.stereotype.Service; |
| 43 | 43 | import org.springframework.util.ObjectUtils; |
| 44 | 44 | |
| ... | ... | @@ -100,6 +100,9 @@ public class PlayServiceImpl implements IPlayService { |
| 100 | 100 | @Autowired |
| 101 | 101 | private ZlmHttpHookSubscribe subscribe; |
| 102 | 102 | |
| 103 | + @Autowired | |
| 104 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 105 | + | |
| 103 | 106 | |
| 104 | 107 | @Override |
| 105 | 108 | public void play(MediaServerItem mediaServerItem, String deviceId, String channelId, |
| ... | ... | @@ -886,7 +889,7 @@ public class PlayServiceImpl implements IPlayService { |
| 886 | 889 | throw new ServiceException("streamId不存在"); |
| 887 | 890 | } |
| 888 | 891 | streamInfo.setPause(true); |
| 889 | - RedisUtil.set(key, streamInfo); | |
| 892 | + redisTemplate.opsForValue().set(key, streamInfo); | |
| 890 | 893 | MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId()); |
| 891 | 894 | if (null == mediaServerItem) { |
| 892 | 895 | logger.warn("mediaServer 不存在!"); |
| ... | ... | @@ -910,7 +913,7 @@ public class PlayServiceImpl implements IPlayService { |
| 910 | 913 | throw new ServiceException("streamId不存在"); |
| 911 | 914 | } |
| 912 | 915 | streamInfo.setPause(false); |
| 913 | - RedisUtil.set(key, streamInfo); | |
| 916 | + redisTemplate.opsForValue().set(key, streamInfo); | |
| 914 | 917 | MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId()); |
| 915 | 918 | if (null == mediaServerItem) { |
| 916 | 919 | logger.warn("mediaServer 不存在!"); | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/redisMsg/RedisGbPlayMsgListener.java
| ... | ... | @@ -2,19 +2,17 @@ package com.genersoft.iot.vmp.service.redisMsg; |
| 2 | 2 | |
| 3 | 3 | import com.alibaba.fastjson2.JSON; |
| 4 | 4 | import com.alibaba.fastjson2.JSONObject; |
| 5 | +import com.genersoft.iot.vmp.common.VideoManagerConstants; | |
| 5 | 6 | import com.genersoft.iot.vmp.conf.DynamicTask; |
| 6 | 7 | import com.genersoft.iot.vmp.conf.UserSetting; |
| 7 | 8 | import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem; |
| 8 | -import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe; | |
| 9 | -import com.genersoft.iot.vmp.media.zlm.ZLMMediaListManager; | |
| 10 | 9 | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; |
| 10 | +import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe; | |
| 11 | 11 | import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory; |
| 12 | 12 | import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange; |
| 13 | 13 | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| 14 | 14 | import com.genersoft.iot.vmp.service.IMediaServerService; |
| 15 | 15 | import com.genersoft.iot.vmp.service.bean.*; |
| 16 | -import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | |
| 17 | -import com.genersoft.iot.vmp.utils.redis.RedisUtil; | |
| 18 | 16 | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; |
| 19 | 17 | import org.slf4j.Logger; |
| 20 | 18 | import org.slf4j.LoggerFactory; |
| ... | ... | @@ -22,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
| 22 | 20 | import org.springframework.beans.factory.annotation.Qualifier; |
| 23 | 21 | import org.springframework.data.redis.connection.Message; |
| 24 | 22 | import org.springframework.data.redis.connection.MessageListener; |
| 23 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 25 | 24 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 26 | 25 | import org.springframework.stereotype.Component; |
| 27 | 26 | |
| ... | ... | @@ -68,7 +67,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 68 | 67 | |
| 69 | 68 | |
| 70 | 69 | @Autowired |
| 71 | - private ZLMMediaListManager zlmMediaListManager; | |
| 70 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 72 | 71 | |
| 73 | 72 | @Autowired |
| 74 | 73 | private ZLMRTPServerFactory zlmrtpServerFactory; |
| ... | ... | @@ -76,14 +75,10 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 76 | 75 | @Autowired |
| 77 | 76 | private IMediaServerService mediaServerService; |
| 78 | 77 | |
| 79 | - @Autowired | |
| 80 | - private IRedisCatchStorage redisCatchStorage; | |
| 81 | 78 | |
| 82 | 79 | @Autowired |
| 83 | 80 | private DynamicTask dynamicTask; |
| 84 | 81 | |
| 85 | - @Autowired | |
| 86 | - private ZLMMediaListManager mediaListManager; | |
| 87 | 82 | |
| 88 | 83 | @Autowired |
| 89 | 84 | private ZlmHttpHookSubscribe subscribe; |
| ... | ... | @@ -246,7 +241,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 246 | 241 | WvpRedisMsg response = WvpRedisMsg.getResponseInstance(userSetting.getServerId(), toId, |
| 247 | 242 | WvpRedisMsgCmd.REQUEST_PUSH_STREAM, serial, result); |
| 248 | 243 | JSONObject jsonObject = (JSONObject)JSON.toJSON(response); |
| 249 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 244 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 250 | 245 | } |
| 251 | 246 | |
| 252 | 247 | /** |
| ... | ... | @@ -265,7 +260,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 265 | 260 | WvpRedisMsgCmd.GET_SEND_ITEM, serial, result); |
| 266 | 261 | |
| 267 | 262 | JSONObject jsonObject = (JSONObject)JSON.toJSON(response); |
| 268 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 263 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 269 | 264 | return; |
| 270 | 265 | } |
| 271 | 266 | // 确定流是否在线 |
| ... | ... | @@ -288,7 +283,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 288 | 283 | userSetting.getServerId(), toId, WvpRedisMsgCmd.GET_SEND_ITEM, serial, result |
| 289 | 284 | ); |
| 290 | 285 | JSONObject jsonObject = (JSONObject)JSON.toJSON(response); |
| 291 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 286 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 292 | 287 | }, userSetting.getPlatformPlayTimeout()); |
| 293 | 288 | |
| 294 | 289 | // 添加订阅 |
| ... | ... | @@ -302,7 +297,12 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 302 | 297 | MessageForPushChannel messageForPushChannel = MessageForPushChannel.getInstance(1, content.getApp(), content.getStream(), |
| 303 | 298 | content.getChannelId(), content.getPlatformId(), content.getPlatformName(), content.getServerId(), |
| 304 | 299 | content.getMediaServerId()); |
| 305 | - redisCatchStorage.sendStreamPushRequestedMsg(messageForPushChannel); | |
| 300 | + | |
| 301 | + String key = VideoManagerConstants.VM_MSG_STREAM_PUSH_REQUESTED; | |
| 302 | + logger.info("[redis发送通知] 推流被请求 {}: {}/{}", key, messageForPushChannel.getApp(), messageForPushChannel.getStream()); | |
| 303 | + redisTemplate.convertAndSend(key, JSON.toJSON(messageForPushChannel)); | |
| 304 | + | |
| 305 | +// redisCatchStorage.sendStreamPushRequestedMsg(messageForPushChannel); | |
| 306 | 306 | |
| 307 | 307 | } |
| 308 | 308 | } |
| ... | ... | @@ -327,7 +327,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 327 | 327 | userSetting.getServerId(), toId, WvpRedisMsgCmd.GET_SEND_ITEM, serial, result |
| 328 | 328 | ); |
| 329 | 329 | JSONObject jsonObject = (JSONObject)JSON.toJSON(response); |
| 330 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 330 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | /** |
| ... | ... | @@ -364,7 +364,7 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 364 | 364 | wvpResult.setMsg("timeout"); |
| 365 | 365 | errorCallback.handler(wvpResult); |
| 366 | 366 | }, userSetting.getPlatformPlayTimeout()); |
| 367 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 367 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
| ... | ... | @@ -389,6 +389,6 @@ public class RedisGbPlayMsgListener implements MessageListener { |
| 389 | 389 | callbacksForStartSendRtpStream.remove(key); |
| 390 | 390 | callbacksForError.remove(key); |
| 391 | 391 | }); |
| 392 | - RedisUtil.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 392 | + redisTemplate.convertAndSend(WVP_PUSH_STREAM_KEY, jsonObject); | |
| 393 | 393 | } |
| 394 | 394 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| ... | ... | @@ -23,6 +23,7 @@ import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
| 23 | 23 | import org.slf4j.Logger; |
| 24 | 24 | import org.slf4j.LoggerFactory; |
| 25 | 25 | import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 26 | 27 | import org.springframework.stereotype.Component; |
| 27 | 28 | |
| 28 | 29 | import java.util.*; |
| ... | ... | @@ -39,14 +40,17 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 39 | 40 | @Autowired |
| 40 | 41 | private UserSetting userSetting; |
| 41 | 42 | |
| 43 | + @Autowired | |
| 44 | + private RedisTemplate<Object, Object> redisTemplate; | |
| 45 | + | |
| 42 | 46 | @Override |
| 43 | 47 | public Long getCSEQ() { |
| 44 | 48 | String key = VideoManagerConstants.SIP_CSEQ_PREFIX + userSetting.getServerId(); |
| 45 | 49 | |
| 46 | - long result = RedisUtil.incr(key, 1L); | |
| 47 | - if (result > Integer.MAX_VALUE) { | |
| 48 | - RedisUtil.set(key, 1); | |
| 49 | - result = 1; | |
| 50 | + Long result = redisTemplate.opsForValue().increment(key, 1L); | |
| 51 | + if (result != null && result > Integer.MAX_VALUE) { | |
| 52 | + redisTemplate.opsForValue().set(key, 1); | |
| 53 | + result = 1L; | |
| 50 | 54 | } |
| 51 | 55 | return result; |
| 52 | 56 | } |
| ... | ... | @@ -55,10 +59,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 55 | 59 | public Long getSN(String method) { |
| 56 | 60 | String key = VideoManagerConstants.SIP_SN_PREFIX + userSetting.getServerId() + "_" + method; |
| 57 | 61 | |
| 58 | - long result = RedisUtil.incr(key, 1L); | |
| 59 | - if (result > Integer.MAX_VALUE) { | |
| 60 | - RedisUtil.set(key, 1); | |
| 61 | - result = 1; | |
| 62 | + Long result = redisTemplate.opsForValue().increment(key, 1L); | |
| 63 | + if (result != null && result > Integer.MAX_VALUE) { | |
| 64 | + redisTemplate.opsForValue().set(key, 1); | |
| 65 | + result = 1L; | |
| 62 | 66 | } |
| 63 | 67 | return result; |
| 64 | 68 | } |
| ... | ... | @@ -66,61 +70,58 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 66 | 70 | @Override |
| 67 | 71 | public void resetAllCSEQ() { |
| 68 | 72 | String scanKey = VideoManagerConstants.SIP_CSEQ_PREFIX + userSetting.getServerId() + "_*"; |
| 69 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 73 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 70 | 74 | for (Object o : keys) { |
| 71 | 75 | String key = (String) o; |
| 72 | - RedisUtil.set(key, 1); | |
| 76 | + redisTemplate.opsForValue().set(key, 1); | |
| 73 | 77 | } |
| 74 | 78 | } |
| 75 | 79 | |
| 76 | 80 | @Override |
| 77 | 81 | public void resetAllSN() { |
| 78 | 82 | String scanKey = VideoManagerConstants.SIP_SN_PREFIX + userSetting.getServerId() + "_*"; |
| 79 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 83 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 80 | 84 | for (Object o : keys) { |
| 81 | 85 | String key = (String) o; |
| 82 | - RedisUtil.set(key, 1); | |
| 86 | + redisTemplate.opsForValue().set(key, 1); | |
| 83 | 87 | } |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | /** |
| 87 | 91 | * 开始播放时将流存入redis |
| 88 | - * | |
| 89 | - * @return | |
| 90 | 92 | */ |
| 91 | 93 | @Override |
| 92 | 94 | public boolean startPlay(StreamInfo stream) { |
| 93 | 95 | |
| 94 | - return RedisUtil.set(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), | |
| 96 | + redisTemplate.opsForValue().set(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), | |
| 95 | 97 | stream.getMediaServerId(), stream.getStream(), stream.getDeviceID(), stream.getChannelId()), |
| 96 | 98 | stream); |
| 99 | + return true; | |
| 97 | 100 | } |
| 98 | 101 | |
| 99 | 102 | /** |
| 100 | 103 | * 停止播放时从redis删除 |
| 101 | - * | |
| 102 | - * @return | |
| 103 | 104 | */ |
| 104 | 105 | @Override |
| 105 | 106 | public boolean stopPlay(StreamInfo streamInfo) { |
| 106 | 107 | if (streamInfo == null) { |
| 107 | 108 | return false; |
| 108 | 109 | } |
| 109 | - return RedisUtil.del(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, | |
| 110 | + Boolean result = redisTemplate.delete(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, | |
| 110 | 111 | userSetting.getServerId(), |
| 111 | 112 | streamInfo.getMediaServerId(), |
| 112 | 113 | streamInfo.getStream(), |
| 113 | 114 | streamInfo.getDeviceID(), |
| 114 | 115 | streamInfo.getChannelId())); |
| 116 | + return result != null && result; | |
| 115 | 117 | } |
| 116 | 118 | |
| 117 | 119 | /** |
| 118 | 120 | * 查询播放列表 |
| 119 | - * @return | |
| 120 | 121 | */ |
| 121 | 122 | @Override |
| 122 | 123 | public StreamInfo queryPlay(StreamInfo streamInfo) { |
| 123 | - return (StreamInfo)RedisUtil.get(String.format("%S_%s_%s_%s_%s_%s", | |
| 124 | + return (StreamInfo)redisTemplate.opsForValue().get(String.format("%S_%s_%s_%s_%s_%s", | |
| 124 | 125 | VideoManagerConstants.PLAYER_PREFIX, |
| 125 | 126 | userSetting.getServerId(), |
| 126 | 127 | streamInfo.getMediaServerId(), |
| ... | ... | @@ -130,35 +131,35 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 130 | 131 | } |
| 131 | 132 | @Override |
| 132 | 133 | public StreamInfo queryPlayByStreamId(String streamId) { |
| 133 | - List<Object> playLeys = RedisUtil.scan(String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), streamId)); | |
| 134 | - if (playLeys == null || playLeys.size() == 0) { | |
| 134 | + List<Object> playLeys = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), streamId)); | |
| 135 | + if (playLeys.size() == 0) { | |
| 135 | 136 | return null; |
| 136 | 137 | } |
| 137 | - return (StreamInfo)RedisUtil.get(playLeys.get(0).toString()); | |
| 138 | + return (StreamInfo)redisTemplate.opsForValue().get(playLeys.get(0).toString()); | |
| 138 | 139 | } |
| 139 | 140 | |
| 140 | 141 | @Override |
| 141 | 142 | public StreamInfo queryPlayByDevice(String deviceId, String channelId) { |
| 142 | - List<Object> playLeys = RedisUtil.scan(String.format("%S_%s_*_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX, | |
| 143 | + List<Object> playLeys = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX, | |
| 143 | 144 | userSetting.getServerId(), |
| 144 | 145 | deviceId, |
| 145 | 146 | channelId)); |
| 146 | - if (playLeys == null || playLeys.size() == 0) { | |
| 147 | + if (playLeys.size() == 0) { | |
| 147 | 148 | return null; |
| 148 | 149 | } |
| 149 | - return (StreamInfo)RedisUtil.get(playLeys.get(0).toString()); | |
| 150 | + return (StreamInfo)redisTemplate.opsForValue().get(playLeys.get(0).toString()); | |
| 150 | 151 | } |
| 151 | 152 | |
| 152 | 153 | @Override |
| 153 | 154 | public Map<String, StreamInfo> queryPlayByDeviceId(String deviceId) { |
| 154 | 155 | Map<String, StreamInfo> streamInfos = new HashMap<>(); |
| 155 | - List<Object> players = RedisUtil.scan(String.format("%S_%s_*_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(),deviceId)); | |
| 156 | + List<Object> players = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(),deviceId)); | |
| 156 | 157 | if (players.size() == 0) { |
| 157 | 158 | return streamInfos; |
| 158 | 159 | } |
| 159 | 160 | for (Object player : players) { |
| 160 | 161 | String key = (String) player; |
| 161 | - StreamInfo streamInfo = JsonUtil.redisJsonToObject(key, StreamInfo.class); | |
| 162 | + StreamInfo streamInfo = JsonUtil.redisJsonToObject(redisTemplate, key, StreamInfo.class); | |
| 162 | 163 | if (Objects.isNull(streamInfo)) { |
| 163 | 164 | continue; |
| 164 | 165 | } |
| ... | ... | @@ -170,23 +171,23 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 170 | 171 | |
| 171 | 172 | @Override |
| 172 | 173 | public boolean startPlayback(StreamInfo stream, String callId) { |
| 173 | - return RedisUtil.set(String.format("%S_%s_%s_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, | |
| 174 | + redisTemplate.opsForValue().set(String.format("%S_%s_%s_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, | |
| 174 | 175 | userSetting.getServerId(), stream.getMediaServerId(), stream.getDeviceID(), stream.getChannelId(), stream.getStream(), callId), stream); |
| 176 | + return true; | |
| 175 | 177 | } |
| 176 | 178 | |
| 177 | 179 | @Override |
| 178 | 180 | public boolean startDownload(StreamInfo stream, String callId) { |
| 179 | - boolean result; | |
| 180 | 181 | String key=String.format("%S_%s_%s_%s_%s_%s_%s", VideoManagerConstants.DOWNLOAD_PREFIX, |
| 181 | 182 | userSetting.getServerId(), stream.getMediaServerId(), stream.getDeviceID(), stream.getChannelId(), stream.getStream(), callId); |
| 182 | 183 | if (stream.getProgress() == 1) { |
| 183 | 184 | logger.debug("添加下载缓存==已完成下载=》{}",key); |
| 184 | - result = RedisUtil.set(key, stream); | |
| 185 | + redisTemplate.opsForValue().set(key, stream); | |
| 185 | 186 | }else { |
| 186 | 187 | logger.debug("添加下载缓存==未完成下载=》{}",key); |
| 187 | - result = RedisUtil.set(key, stream, 60*60); | |
| 188 | + redisTemplate.opsForValue().set(key, stream, 60*60); | |
| 188 | 189 | } |
| 189 | - return result; | |
| 190 | + return true; | |
| 190 | 191 | } |
| 191 | 192 | @Override |
| 192 | 193 | public boolean stopDownload(String deviceId, String channelId, String stream, String callId) { |
| ... | ... | @@ -215,10 +216,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 215 | 216 | stream, |
| 216 | 217 | callId |
| 217 | 218 | ); |
| 218 | - List<Object> scan = RedisUtil.scan(key); | |
| 219 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 219 | 220 | if (scan.size() > 0) { |
| 220 | 221 | for (Object keyObj : scan) { |
| 221 | - RedisUtil.del((String) keyObj); | |
| 222 | + redisTemplate.delete(keyObj); | |
| 222 | 223 | } |
| 223 | 224 | } |
| 224 | 225 | return true; |
| ... | ... | @@ -251,10 +252,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 251 | 252 | stream, |
| 252 | 253 | callId |
| 253 | 254 | ); |
| 254 | - List<Object> scan = RedisUtil.scan(key); | |
| 255 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 255 | 256 | if (scan.size() > 0) { |
| 256 | 257 | for (Object keyObj : scan) { |
| 257 | - RedisUtil.del((String) keyObj); | |
| 258 | + redisTemplate.delete(keyObj); | |
| 258 | 259 | } |
| 259 | 260 | } |
| 260 | 261 | return true; |
| ... | ... | @@ -284,9 +285,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 284 | 285 | stream, |
| 285 | 286 | callId |
| 286 | 287 | ); |
| 287 | - List<Object> streamInfoScan = RedisUtil.scan(key); | |
| 288 | + List<Object> streamInfoScan = RedisUtil.scan(redisTemplate, key); | |
| 288 | 289 | if (streamInfoScan.size() > 0) { |
| 289 | - return (StreamInfo) RedisUtil.get((String) streamInfoScan.get(0)); | |
| 290 | + return (StreamInfo) redisTemplate.opsForValue().get(streamInfoScan.get(0)); | |
| 290 | 291 | }else { |
| 291 | 292 | return null; |
| 292 | 293 | } |
| ... | ... | @@ -316,59 +317,59 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 316 | 317 | stream, |
| 317 | 318 | callId |
| 318 | 319 | ); |
| 319 | - List<Object> streamInfoScan = RedisUtil.scan(key); | |
| 320 | + List<Object> streamInfoScan = RedisUtil.scan(redisTemplate, key); | |
| 320 | 321 | return (String) streamInfoScan.get(0); |
| 321 | 322 | } |
| 322 | 323 | |
| 323 | 324 | @Override |
| 324 | 325 | public void updatePlatformCatchInfo(ParentPlatformCatch parentPlatformCatch) { |
| 325 | 326 | String key = VideoManagerConstants.PLATFORM_CATCH_PREFIX + userSetting.getServerId() + "_" + parentPlatformCatch.getId(); |
| 326 | - RedisUtil.set(key, parentPlatformCatch); | |
| 327 | + redisTemplate.opsForValue().set(key, parentPlatformCatch); | |
| 327 | 328 | } |
| 328 | 329 | |
| 329 | 330 | @Override |
| 330 | 331 | public ParentPlatformCatch queryPlatformCatchInfo(String platformGbId) { |
| 331 | - return (ParentPlatformCatch)RedisUtil.get(VideoManagerConstants.PLATFORM_CATCH_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 332 | + return (ParentPlatformCatch)redisTemplate.opsForValue().get(VideoManagerConstants.PLATFORM_CATCH_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 332 | 333 | } |
| 333 | 334 | |
| 334 | 335 | @Override |
| 335 | 336 | public void delPlatformCatchInfo(String platformGbId) { |
| 336 | - RedisUtil.del(VideoManagerConstants.PLATFORM_CATCH_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 337 | + redisTemplate.delete(VideoManagerConstants.PLATFORM_CATCH_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 337 | 338 | } |
| 338 | 339 | |
| 339 | 340 | @Override |
| 340 | 341 | public void delPlatformKeepalive(String platformGbId) { |
| 341 | - RedisUtil.del(VideoManagerConstants.PLATFORM_KEEPALIVE_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 342 | + redisTemplate.delete(VideoManagerConstants.PLATFORM_KEEPALIVE_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 342 | 343 | } |
| 343 | 344 | |
| 344 | 345 | @Override |
| 345 | 346 | public void delPlatformRegister(String platformGbId) { |
| 346 | - RedisUtil.del(VideoManagerConstants.PLATFORM_REGISTER_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 347 | + redisTemplate.delete(VideoManagerConstants.PLATFORM_REGISTER_PREFIX + userSetting.getServerId() + "_" + platformGbId); | |
| 347 | 348 | } |
| 348 | 349 | |
| 349 | 350 | |
| 350 | 351 | @Override |
| 351 | 352 | public void updatePlatformRegisterInfo(String callId, PlatformRegisterInfo platformRegisterInfo) { |
| 352 | 353 | String key = VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId; |
| 353 | - RedisUtil.set(key, platformRegisterInfo, 30); | |
| 354 | + redisTemplate.opsForValue().set(key, platformRegisterInfo, 30); | |
| 354 | 355 | } |
| 355 | 356 | |
| 356 | 357 | |
| 357 | 358 | @Override |
| 358 | 359 | public PlatformRegisterInfo queryPlatformRegisterInfo(String callId) { |
| 359 | - return (PlatformRegisterInfo)RedisUtil.get(VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId); | |
| 360 | + return (PlatformRegisterInfo)redisTemplate.opsForValue().get(VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId); | |
| 360 | 361 | } |
| 361 | 362 | |
| 362 | 363 | @Override |
| 363 | 364 | public void delPlatformRegisterInfo(String callId) { |
| 364 | - RedisUtil.del(VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId); | |
| 365 | + redisTemplate.delete(VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId); | |
| 365 | 366 | } |
| 366 | 367 | |
| 367 | 368 | @Override |
| 368 | 369 | public void cleanPlatformRegisterInfos() { |
| 369 | - List regInfos = RedisUtil.scan(VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + "*"); | |
| 370 | + List regInfos = RedisUtil.scan(redisTemplate, VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + "*"); | |
| 370 | 371 | for (Object key : regInfos) { |
| 371 | - RedisUtil.del(key.toString()); | |
| 372 | + redisTemplate.delete(key.toString()); | |
| 372 | 373 | } |
| 373 | 374 | } |
| 374 | 375 | |
| ... | ... | @@ -382,7 +383,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 382 | 383 | + sendRtpItem.getChannelId() + "_" |
| 383 | 384 | + sendRtpItem.getStreamId() + "_" |
| 384 | 385 | + sendRtpItem.getCallId(); |
| 385 | - RedisUtil.set(key, sendRtpItem); | |
| 386 | + redisTemplate.opsForValue().set(key, sendRtpItem); | |
| 386 | 387 | } |
| 387 | 388 | |
| 388 | 389 | @Override |
| ... | ... | @@ -405,9 +406,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 405 | 406 | + channelId + "_" |
| 406 | 407 | + streamId + "_" |
| 407 | 408 | + callId; |
| 408 | - List<Object> scan = RedisUtil.scan(key); | |
| 409 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 409 | 410 | if (scan.size() > 0) { |
| 410 | - return (SendRtpItem)RedisUtil.get((String)scan.get(0)); | |
| 411 | + return (SendRtpItem)redisTemplate.opsForValue().get(scan.get(0)); | |
| 411 | 412 | }else { |
| 412 | 413 | return null; |
| 413 | 414 | } |
| ... | ... | @@ -427,10 +428,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 427 | 428 | + channelId + "_" |
| 428 | 429 | + streamId + "_" |
| 429 | 430 | + callId; |
| 430 | - List<Object> scan = RedisUtil.scan(key); | |
| 431 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 431 | 432 | List<SendRtpItem> result = new ArrayList<>(); |
| 432 | 433 | for (Object o : scan) { |
| 433 | - result.add((SendRtpItem) RedisUtil.get((String) o)); | |
| 434 | + result.add((SendRtpItem) redisTemplate.opsForValue().get(o)); | |
| 434 | 435 | } |
| 435 | 436 | return result; |
| 436 | 437 | } |
| ... | ... | @@ -449,10 +450,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 449 | 450 | + channelId + "_" |
| 450 | 451 | + stream + "_" |
| 451 | 452 | + callId; |
| 452 | - List<Object> scan = RedisUtil.scan(key); | |
| 453 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 453 | 454 | List<SendRtpItem> result = new ArrayList<>(); |
| 454 | 455 | for (Object o : scan) { |
| 455 | - result.add((SendRtpItem) RedisUtil.get((String) o)); | |
| 456 | + result.add((SendRtpItem) redisTemplate.opsForValue().get(o)); | |
| 456 | 457 | } |
| 457 | 458 | return result; |
| 458 | 459 | } |
| ... | ... | @@ -465,12 +466,12 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 465 | 466 | String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX |
| 466 | 467 | + userSetting.getServerId() + "_*_" |
| 467 | 468 | + platformGbId + "_*" + "_*" + "_*"; |
| 468 | - List<Object> queryResult = RedisUtil.scan(key); | |
| 469 | + List<Object> queryResult = RedisUtil.scan(redisTemplate, key); | |
| 469 | 470 | List<SendRtpItem> result= new ArrayList<>(); |
| 470 | 471 | |
| 471 | 472 | for (Object o : queryResult) { |
| 472 | 473 | String keyItem = (String) o; |
| 473 | - result.add((SendRtpItem) RedisUtil.get(keyItem)); | |
| 474 | + result.add((SendRtpItem) redisTemplate.opsForValue().get(keyItem)); | |
| 474 | 475 | } |
| 475 | 476 | |
| 476 | 477 | return result; |
| ... | ... | @@ -478,8 +479,6 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 478 | 479 | |
| 479 | 480 | /** |
| 480 | 481 | * 删除RTP推送信息缓存 |
| 481 | - * @param platformGbId | |
| 482 | - * @param channelId | |
| 483 | 482 | */ |
| 484 | 483 | @Override |
| 485 | 484 | public void deleteSendRTPServer(String platformGbId, String channelId, String callId, String streamId) { |
| ... | ... | @@ -495,10 +494,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 495 | 494 | + channelId + "_" |
| 496 | 495 | + streamId + "_" |
| 497 | 496 | + callId; |
| 498 | - List<Object> scan = RedisUtil.scan(key); | |
| 497 | + List<Object> scan = RedisUtil.scan(redisTemplate, key); | |
| 499 | 498 | if (scan.size() > 0) { |
| 500 | 499 | for (Object keyStr : scan) { |
| 501 | - RedisUtil.del((String)keyStr); | |
| 500 | + redisTemplate.delete(keyStr); | |
| 502 | 501 | } |
| 503 | 502 | } |
| 504 | 503 | } |
| ... | ... | @@ -507,12 +506,12 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 507 | 506 | public List<SendRtpItem> queryAllSendRTPServer() { |
| 508 | 507 | String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX |
| 509 | 508 | + userSetting.getServerId() + "_*"; |
| 510 | - List<Object> queryResult = RedisUtil.scan(key); | |
| 509 | + List<Object> queryResult = RedisUtil.scan(redisTemplate, key); | |
| 511 | 510 | List<SendRtpItem> result= new ArrayList<>(); |
| 512 | 511 | |
| 513 | 512 | for (Object o : queryResult) { |
| 514 | 513 | String keyItem = (String) o; |
| 515 | - result.add((SendRtpItem) RedisUtil.get(keyItem)); | |
| 514 | + result.add((SendRtpItem) redisTemplate.opsForValue().get(keyItem)); | |
| 516 | 515 | } |
| 517 | 516 | |
| 518 | 517 | return result; |
| ... | ... | @@ -520,47 +519,42 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 520 | 519 | |
| 521 | 520 | /** |
| 522 | 521 | * 查询某个通道是否存在上级点播(RTP推送) |
| 523 | - * @param channelId | |
| 524 | 522 | */ |
| 525 | 523 | @Override |
| 526 | 524 | public boolean isChannelSendingRTP(String channelId) { |
| 527 | 525 | String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX |
| 528 | 526 | + userSetting.getServerId() + "_*_*_" |
| 529 | 527 | + channelId + "*_" + "*_"; |
| 530 | - List<Object> RtpStreams = RedisUtil.scan(key); | |
| 531 | - if (RtpStreams.size() > 0) { | |
| 532 | - return true; | |
| 533 | - } else { | |
| 534 | - return false; | |
| 535 | - } | |
| 528 | + List<Object> RtpStreams = RedisUtil.scan(redisTemplate, key); | |
| 529 | + return RtpStreams.size() > 0; | |
| 536 | 530 | } |
| 537 | 531 | |
| 538 | 532 | @Override |
| 539 | 533 | public void clearCatchByDeviceId(String deviceId) { |
| 540 | - List<Object> playLeys = RedisUtil.scan(String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, | |
| 534 | + List<Object> playLeys = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, | |
| 541 | 535 | userSetting.getServerId(), |
| 542 | 536 | deviceId)); |
| 543 | 537 | if (playLeys.size() > 0) { |
| 544 | 538 | for (Object key : playLeys) { |
| 545 | - RedisUtil.del(key.toString()); | |
| 539 | + redisTemplate.delete(key.toString()); | |
| 546 | 540 | } |
| 547 | 541 | } |
| 548 | 542 | |
| 549 | - List<Object> playBackers = RedisUtil.scan(String.format("%S_%s_*_%s_*_*_*", VideoManagerConstants.PLAY_BLACK_PREFIX, | |
| 543 | + List<Object> playBackers = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_%s_*_*_*", VideoManagerConstants.PLAY_BLACK_PREFIX, | |
| 550 | 544 | userSetting.getServerId(), |
| 551 | 545 | deviceId)); |
| 552 | 546 | if (playBackers.size() > 0) { |
| 553 | 547 | for (Object key : playBackers) { |
| 554 | - RedisUtil.del(key.toString()); | |
| 548 | + redisTemplate.delete(key.toString()); | |
| 555 | 549 | } |
| 556 | 550 | } |
| 557 | 551 | |
| 558 | - List<Object> deviceCache = RedisUtil.scan(String.format("%S%s_%s", VideoManagerConstants.DEVICE_PREFIX, | |
| 552 | + List<Object> deviceCache = RedisUtil.scan(redisTemplate, String.format("%S%s_%s", VideoManagerConstants.DEVICE_PREFIX, | |
| 559 | 553 | userSetting.getServerId(), |
| 560 | 554 | deviceId)); |
| 561 | 555 | if (deviceCache.size() > 0) { |
| 562 | 556 | for (Object key : deviceCache) { |
| 563 | - RedisUtil.del(key.toString()); | |
| 557 | + redisTemplate.delete(key.toString()); | |
| 564 | 558 | } |
| 565 | 559 | } |
| 566 | 560 | } |
| ... | ... | @@ -568,14 +562,14 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 568 | 562 | @Override |
| 569 | 563 | public void updateWVPInfo(JSONObject jsonObject, int time) { |
| 570 | 564 | String key = VideoManagerConstants.WVP_SERVER_PREFIX + userSetting.getServerId(); |
| 571 | - RedisUtil.set(key, jsonObject, time); | |
| 565 | + redisTemplate.opsForValue().set(key, jsonObject, time); | |
| 572 | 566 | } |
| 573 | 567 | |
| 574 | 568 | @Override |
| 575 | 569 | public void sendStreamChangeMsg(String type, JSONObject jsonObject) { |
| 576 | 570 | String key = VideoManagerConstants.WVP_MSG_STREAM_CHANGE_PREFIX + type; |
| 577 | 571 | logger.info("[redis 流变化事件] {}: {}", key, jsonObject.toString()); |
| 578 | - RedisUtil.convertAndSend(key, jsonObject); | |
| 572 | + redisTemplate.convertAndSend(key, jsonObject); | |
| 579 | 573 | } |
| 580 | 574 | |
| 581 | 575 | @Override |
| ... | ... | @@ -586,13 +580,13 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 586 | 580 | if (streamAuthorityInfo != null) { |
| 587 | 581 | onStreamChangedHookParam.setCallId(streamAuthorityInfo.getCallId()); |
| 588 | 582 | } |
| 589 | - RedisUtil.set(key, onStreamChangedHookParam); | |
| 583 | + redisTemplate.opsForValue().set(key, onStreamChangedHookParam); | |
| 590 | 584 | } |
| 591 | 585 | |
| 592 | 586 | @Override |
| 593 | 587 | public void removeStream(String mediaServerId, String type, String app, String streamId) { |
| 594 | 588 | String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_" + type + "_" + app + "_" + streamId + "_" + mediaServerId; |
| 595 | - RedisUtil.del(key); | |
| 589 | + redisTemplate.delete(key); | |
| 596 | 590 | } |
| 597 | 591 | |
| 598 | 592 | @Override |
| ... | ... | @@ -619,9 +613,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 619 | 613 | stream, |
| 620 | 614 | callId |
| 621 | 615 | ); |
| 622 | - List<Object> streamInfoScan = RedisUtil.scan2(key); | |
| 616 | + List<Object> streamInfoScan = RedisUtil.scan(redisTemplate, key); | |
| 623 | 617 | if (streamInfoScan.size() > 0) { |
| 624 | - return (StreamInfo) RedisUtil.get((String) streamInfoScan.get(0)); | |
| 618 | + return (StreamInfo) redisTemplate.opsForValue().get(streamInfoScan.get(0)); | |
| 625 | 619 | }else { |
| 626 | 620 | return null; |
| 627 | 621 | } |
| ... | ... | @@ -630,15 +624,15 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 630 | 624 | @Override |
| 631 | 625 | public ThirdPartyGB queryMemberNoGBId(String queryKey) { |
| 632 | 626 | String key = VideoManagerConstants.WVP_STREAM_GB_ID_PREFIX + queryKey; |
| 633 | - return JsonUtil.redisJsonToObject(key, ThirdPartyGB.class); | |
| 627 | + return JsonUtil.redisJsonToObject(redisTemplate, key, ThirdPartyGB.class); | |
| 634 | 628 | } |
| 635 | 629 | |
| 636 | 630 | @Override |
| 637 | 631 | public void removeStream(String mediaServerId, String type) { |
| 638 | 632 | String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_" + type + "_*_*_" + mediaServerId; |
| 639 | - List<Object> streams = RedisUtil.scan(key); | |
| 633 | + List<Object> streams = RedisUtil.scan(redisTemplate, key); | |
| 640 | 634 | for (Object stream : streams) { |
| 641 | - RedisUtil.del((String) stream); | |
| 635 | + redisTemplate.delete(stream); | |
| 642 | 636 | } |
| 643 | 637 | } |
| 644 | 638 | |
| ... | ... | @@ -646,9 +640,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 646 | 640 | public List<OnStreamChangedHookParam> getStreams(String mediaServerId, String type) { |
| 647 | 641 | List<OnStreamChangedHookParam> result = new ArrayList<>(); |
| 648 | 642 | String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_" + type + "_*_*_" + mediaServerId; |
| 649 | - List<Object> streams = RedisUtil.scan(key); | |
| 643 | + List<Object> streams = RedisUtil.scan(redisTemplate, key); | |
| 650 | 644 | for (Object stream : streams) { |
| 651 | - OnStreamChangedHookParam onStreamChangedHookParam = (OnStreamChangedHookParam)RedisUtil.get((String) stream); | |
| 645 | + OnStreamChangedHookParam onStreamChangedHookParam = (OnStreamChangedHookParam)redisTemplate.opsForValue().get(stream); | |
| 652 | 646 | result.add(onStreamChangedHookParam); |
| 653 | 647 | } |
| 654 | 648 | return result; |
| ... | ... | @@ -657,21 +651,21 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 657 | 651 | @Override |
| 658 | 652 | public void updateDevice(Device device) { |
| 659 | 653 | String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + device.getDeviceId(); |
| 660 | - RedisUtil.set(key, device); | |
| 654 | + redisTemplate.opsForValue().set(key, device); | |
| 661 | 655 | } |
| 662 | 656 | |
| 663 | 657 | @Override |
| 664 | 658 | public void removeDevice(String deviceId) { |
| 665 | 659 | String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; |
| 666 | - RedisUtil.del(key); | |
| 660 | + redisTemplate.delete(key); | |
| 667 | 661 | } |
| 668 | 662 | |
| 669 | 663 | @Override |
| 670 | 664 | public void removeAllDevice() { |
| 671 | 665 | String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; |
| 672 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 666 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 673 | 667 | for (Object key : keys) { |
| 674 | - RedisUtil.del((String) key); | |
| 668 | + redisTemplate.delete(key); | |
| 675 | 669 | } |
| 676 | 670 | } |
| 677 | 671 | |
| ... | ... | @@ -679,12 +673,12 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 679 | 673 | public List<Device> getAllDevices() { |
| 680 | 674 | String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; |
| 681 | 675 | List<Device> result = new ArrayList<>(); |
| 682 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 676 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 683 | 677 | for (Object o : keys) { |
| 684 | 678 | String key = (String) o; |
| 685 | - Device device = JsonUtil.redisJsonToObject(key, Device.class); | |
| 679 | + Device device = JsonUtil.redisJsonToObject(redisTemplate, key, Device.class); | |
| 686 | 680 | if (Objects.nonNull(device)) { // 只取没有存过得 |
| 687 | - result.add(JsonUtil.redisJsonToObject(key, Device.class)); | |
| 681 | + result.add(JsonUtil.redisJsonToObject(redisTemplate, key, Device.class)); | |
| 688 | 682 | } |
| 689 | 683 | } |
| 690 | 684 | |
| ... | ... | @@ -694,31 +688,31 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 694 | 688 | @Override |
| 695 | 689 | public Device getDevice(String deviceId) { |
| 696 | 690 | String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; |
| 697 | - return JsonUtil.redisJsonToObject(key, Device.class); | |
| 691 | + return JsonUtil.redisJsonToObject(redisTemplate, key, Device.class); | |
| 698 | 692 | } |
| 699 | 693 | |
| 700 | 694 | @Override |
| 701 | 695 | public void updateGpsMsgInfo(GPSMsgInfo gpsMsgInfo) { |
| 702 | 696 | String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gpsMsgInfo.getId(); |
| 703 | - RedisUtil.set(key, gpsMsgInfo, 60); // 默认GPS消息保存1分钟 | |
| 697 | + redisTemplate.opsForValue().set(key, gpsMsgInfo, 60); // 默认GPS消息保存1分钟 | |
| 704 | 698 | } |
| 705 | 699 | |
| 706 | 700 | @Override |
| 707 | 701 | public GPSMsgInfo getGpsMsgInfo(String gbId) { |
| 708 | 702 | String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gbId; |
| 709 | - return JsonUtil.redisJsonToObject(key, GPSMsgInfo.class); | |
| 703 | + return JsonUtil.redisJsonToObject(redisTemplate, key, GPSMsgInfo.class); | |
| 710 | 704 | } |
| 711 | 705 | |
| 712 | 706 | @Override |
| 713 | 707 | public List<GPSMsgInfo> getAllGpsMsgInfo() { |
| 714 | 708 | String scanKey = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_*"; |
| 715 | 709 | List<GPSMsgInfo> result = new ArrayList<>(); |
| 716 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 710 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 717 | 711 | for (Object o : keys) { |
| 718 | 712 | String key = (String) o; |
| 719 | - GPSMsgInfo gpsMsgInfo = JsonUtil.redisJsonToObject(key, GPSMsgInfo.class); | |
| 713 | + GPSMsgInfo gpsMsgInfo = JsonUtil.redisJsonToObject(redisTemplate, key, GPSMsgInfo.class); | |
| 720 | 714 | if (Objects.nonNull(gpsMsgInfo) && !gpsMsgInfo.isStored()) { // 只取没有存过得 |
| 721 | - result.add(JsonUtil.redisJsonToObject(key, GPSMsgInfo.class)); | |
| 715 | + result.add(JsonUtil.redisJsonToObject(redisTemplate, key, GPSMsgInfo.class)); | |
| 722 | 716 | } |
| 723 | 717 | } |
| 724 | 718 | |
| ... | ... | @@ -728,19 +722,19 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 728 | 722 | @Override |
| 729 | 723 | public void updateStreamAuthorityInfo(String app, String stream, StreamAuthorityInfo streamAuthorityInfo) { |
| 730 | 724 | String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream; |
| 731 | - RedisUtil.set(key, streamAuthorityInfo); | |
| 725 | + redisTemplate.opsForValue().set(key, streamAuthorityInfo); | |
| 732 | 726 | } |
| 733 | 727 | |
| 734 | 728 | @Override |
| 735 | 729 | public void removeStreamAuthorityInfo(String app, String stream) { |
| 736 | 730 | String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream ; |
| 737 | - RedisUtil.del(key); | |
| 731 | + redisTemplate.delete(key); | |
| 738 | 732 | } |
| 739 | 733 | |
| 740 | 734 | @Override |
| 741 | 735 | public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) { |
| 742 | 736 | String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream ; |
| 743 | - return JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class); | |
| 737 | + return JsonUtil.redisJsonToObject(redisTemplate, key, StreamAuthorityInfo.class); | |
| 744 | 738 | |
| 745 | 739 | } |
| 746 | 740 | |
| ... | ... | @@ -748,10 +742,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 748 | 742 | public List<StreamAuthorityInfo> getAllStreamAuthorityInfo() { |
| 749 | 743 | String scanKey = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_*_*" ; |
| 750 | 744 | List<StreamAuthorityInfo> result = new ArrayList<>(); |
| 751 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 745 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 752 | 746 | for (Object o : keys) { |
| 753 | 747 | String key = (String) o; |
| 754 | - result.add(JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class)); | |
| 748 | + result.add(JsonUtil.redisJsonToObject(redisTemplate, key, StreamAuthorityInfo.class)); | |
| 755 | 749 | } |
| 756 | 750 | return result; |
| 757 | 751 | } |
| ... | ... | @@ -762,10 +756,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 762 | 756 | String scanKey = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_*_" + app + "_" + streamId + "_" + mediaServerId; |
| 763 | 757 | |
| 764 | 758 | OnStreamChangedHookParam result = null; |
| 765 | - List<Object> keys = RedisUtil.scan(scanKey); | |
| 759 | + List<Object> keys = RedisUtil.scan(redisTemplate, scanKey); | |
| 766 | 760 | if (keys.size() > 0) { |
| 767 | 761 | String key = (String) keys.get(0); |
| 768 | - result = JsonUtil.redisJsonToObject(key, OnStreamChangedHookParam.class); | |
| 762 | + result = JsonUtil.redisJsonToObject(redisTemplate, key, OnStreamChangedHookParam.class); | |
| 769 | 763 | } |
| 770 | 764 | |
| 771 | 765 | return result; |
| ... | ... | @@ -776,12 +770,13 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 776 | 770 | String key = VideoManagerConstants.SYSTEM_INFO_CPU_PREFIX + userSetting.getServerId(); |
| 777 | 771 | Map<String, String> infoMap = new HashMap<>(); |
| 778 | 772 | infoMap.put("time", DateUtil.getNow()); |
| 779 | - infoMap.put("data", cpuInfo + ""); | |
| 780 | - RedisUtil.lSet(key, infoMap); | |
| 773 | + infoMap.put("data", String.valueOf(cpuInfo)); | |
| 774 | + redisTemplate.opsForList().rightPush(key, infoMap); | |
| 781 | 775 | // 每秒一个,最多只存30个 |
| 782 | - if (RedisUtil.lGetListSize(key) >= 30) { | |
| 783 | - for (int i = 0; i < RedisUtil.lGetListSize(key) - 30; i++) { | |
| 784 | - RedisUtil.lLeftPop(key); | |
| 776 | + Long size = redisTemplate.opsForList().size(key); | |
| 777 | + if (size != null && size >= 30) { | |
| 778 | + for (int i = 0; i < size - 30; i++) { | |
| 779 | + redisTemplate.opsForList().leftPop(key); | |
| 785 | 780 | } |
| 786 | 781 | } |
| 787 | 782 | } |
| ... | ... | @@ -791,12 +786,13 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 791 | 786 | String key = VideoManagerConstants.SYSTEM_INFO_MEM_PREFIX + userSetting.getServerId(); |
| 792 | 787 | Map<String, String> infoMap = new HashMap<>(); |
| 793 | 788 | infoMap.put("time", DateUtil.getNow()); |
| 794 | - infoMap.put("data", memInfo + ""); | |
| 795 | - RedisUtil.lSet(key, infoMap); | |
| 789 | + infoMap.put("data", String.valueOf(memInfo)); | |
| 790 | + redisTemplate.opsForList().rightPush(key, infoMap); | |
| 796 | 791 | // 每秒一个,最多只存30个 |
| 797 | - if (RedisUtil.lGetListSize(key) >= 30) { | |
| 798 | - for (int i = 0; i < RedisUtil.lGetListSize(key) - 30; i++) { | |
| 799 | - RedisUtil.lLeftPop(key); | |
| 792 | + Long size = redisTemplate.opsForList().size(key); | |
| 793 | + if (size != null && size >= 30) { | |
| 794 | + for (int i = 0; i < size - 30; i++) { | |
| 795 | + redisTemplate.opsForList().leftPop(key); | |
| 800 | 796 | } |
| 801 | 797 | } |
| 802 | 798 | } |
| ... | ... | @@ -809,11 +805,12 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 809 | 805 | for (String netKey : networkInterfaces.keySet()) { |
| 810 | 806 | infoMap.put(netKey, networkInterfaces.get(netKey)); |
| 811 | 807 | } |
| 812 | - RedisUtil.lSet(key, infoMap); | |
| 808 | + redisTemplate.opsForList().rightPush(key, infoMap); | |
| 813 | 809 | // 每秒一个,最多只存30个 |
| 814 | - if (RedisUtil.lGetListSize(key) >= 30) { | |
| 815 | - for (int i = 0; i < RedisUtil.lGetListSize(key) - 30; i++) { | |
| 816 | - RedisUtil.lLeftPop(key); | |
| 810 | + Long size = redisTemplate.opsForList().size(key); | |
| 811 | + if (size != null && size >= 30) { | |
| 812 | + for (int i = 0; i < size - 30; i++) { | |
| 813 | + redisTemplate.opsForList().leftPop(key); | |
| 817 | 814 | } |
| 818 | 815 | } |
| 819 | 816 | } |
| ... | ... | @@ -822,7 +819,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 822 | 819 | public void addDiskInfo(List<Map<String, Object>> diskInfo) { |
| 823 | 820 | |
| 824 | 821 | String key = VideoManagerConstants.SYSTEM_INFO_DISK_PREFIX + userSetting.getServerId(); |
| 825 | - RedisUtil.set(key, diskInfo); | |
| 822 | + redisTemplate.opsForValue().set(key, diskInfo); | |
| 826 | 823 | } |
| 827 | 824 | |
| 828 | 825 | @Override |
| ... | ... | @@ -832,11 +829,11 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 832 | 829 | String netKey = VideoManagerConstants.SYSTEM_INFO_NET_PREFIX + userSetting.getServerId(); |
| 833 | 830 | String diskKey = VideoManagerConstants.SYSTEM_INFO_DISK_PREFIX + userSetting.getServerId(); |
| 834 | 831 | SystemAllInfo systemAllInfo = new SystemAllInfo(); |
| 835 | - systemAllInfo.setCpu(RedisUtil.lGet(cpuKey, 0, -1)); | |
| 836 | - systemAllInfo.setMem(RedisUtil.lGet(memKey, 0, -1)); | |
| 837 | - systemAllInfo.setNet(RedisUtil.lGet(netKey, 0, -1)); | |
| 832 | + systemAllInfo.setCpu(redisTemplate.opsForList().range(cpuKey, 0, -1)); | |
| 833 | + systemAllInfo.setMem(redisTemplate.opsForList().range(memKey, 0, -1)); | |
| 834 | + systemAllInfo.setNet(redisTemplate.opsForList().range(netKey, 0, -1)); | |
| 838 | 835 | |
| 839 | - systemAllInfo.setDisk(RedisUtil.get(diskKey)); | |
| 836 | + systemAllInfo.setDisk(redisTemplate.opsForValue().get(diskKey)); | |
| 840 | 837 | systemAllInfo.setNetTotal(SystemInfoUtils.getNetworkTotal()); |
| 841 | 838 | return systemAllInfo; |
| 842 | 839 | } |
| ... | ... | @@ -845,14 +842,14 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 845 | 842 | public void sendMobilePositionMsg(JSONObject jsonObject) { |
| 846 | 843 | String key = VideoManagerConstants.VM_MSG_SUBSCRIBE_MOBILE_POSITION; |
| 847 | 844 | logger.info("[redis发送通知] 移动位置 {}: {}", key, jsonObject.toString()); |
| 848 | - RedisUtil.convertAndSend(key, jsonObject); | |
| 845 | + redisTemplate.convertAndSend(key, jsonObject); | |
| 849 | 846 | } |
| 850 | 847 | |
| 851 | 848 | @Override |
| 852 | 849 | public void sendStreamPushRequestedMsg(MessageForPushChannel msg) { |
| 853 | 850 | String key = VideoManagerConstants.VM_MSG_STREAM_PUSH_REQUESTED; |
| 854 | 851 | logger.info("[redis发送通知] 推流被请求 {}: {}/{}", key, msg.getApp(), msg.getStream()); |
| 855 | - RedisUtil.convertAndSend(key, (JSONObject)JSON.toJSON(msg)); | |
| 852 | + redisTemplate.convertAndSend(key, JSON.toJSON(msg)); | |
| 856 | 853 | } |
| 857 | 854 | |
| 858 | 855 | @Override |
| ... | ... | @@ -860,7 +857,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 860 | 857 | // 此消息用于对接第三方服务下级来的消息内容 |
| 861 | 858 | String key = VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM; |
| 862 | 859 | logger.info("[redis发送通知] 报警{}: {}", key, JSON.toJSON(msg)); |
| 863 | - RedisUtil.convertAndSend(key, (JSONObject)JSON.toJSON(msg)); | |
| 860 | + redisTemplate.convertAndSend(key, JSON.toJSON(msg)); | |
| 864 | 861 | } |
| 865 | 862 | |
| 866 | 863 | @Override |
| ... | ... | @@ -875,19 +872,19 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 875 | 872 | logger.info("[redis通知]获取所有推流设备的状态"); |
| 876 | 873 | JSONObject jsonObject = new JSONObject(); |
| 877 | 874 | jsonObject.put(key, key); |
| 878 | - RedisUtil.convertAndSend(key, jsonObject); | |
| 875 | + redisTemplate.convertAndSend(key, jsonObject); | |
| 879 | 876 | } |
| 880 | 877 | |
| 881 | 878 | @Override |
| 882 | 879 | public int getPushStreamCount(String id) { |
| 883 | 880 | String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_PUSH_*_*_" + id; |
| 884 | - return RedisUtil.scan(key).size(); | |
| 881 | + return RedisUtil.scan(redisTemplate, key).size(); | |
| 885 | 882 | } |
| 886 | 883 | |
| 887 | 884 | @Override |
| 888 | 885 | public int getProxyStreamCount(String id) { |
| 889 | 886 | String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_PULL_*_*_" + id; |
| 890 | - return RedisUtil.scan(key).size(); | |
| 887 | + return RedisUtil.scan(redisTemplate, key).size(); | |
| 891 | 888 | } |
| 892 | 889 | |
| 893 | 890 | @Override |
| ... | ... | @@ -896,13 +893,13 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 896 | 893 | String playBackKey = VideoManagerConstants.PLAY_BLACK_PREFIX + "_" + userSetting.getServerId() + "_" + id + "_*"; |
| 897 | 894 | String downloadKey = VideoManagerConstants.DOWNLOAD_PREFIX + "_" + userSetting.getServerId() + "_" + id + "_*"; |
| 898 | 895 | |
| 899 | - return RedisUtil.scan(playKey).size() + RedisUtil.scan(playBackKey).size() + RedisUtil.scan(downloadKey).size(); | |
| 896 | + return RedisUtil.scan(redisTemplate, playKey).size() + RedisUtil.scan(redisTemplate, playBackKey).size() + RedisUtil.scan(redisTemplate, downloadKey).size(); | |
| 900 | 897 | } |
| 901 | 898 | |
| 902 | 899 | @Override |
| 903 | 900 | public int getGbSendCount(String id) { |
| 904 | 901 | String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX |
| 905 | 902 | + userSetting.getServerId() + "_*_" + id + "_*"; |
| 906 | - return RedisUtil.scan(key).size(); | |
| 903 | + return RedisUtil.scan(redisTemplate, key).size(); | |
| 907 | 904 | } |
| 908 | 905 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java
| 1 | 1 | package com.genersoft.iot.vmp.utils; |
| 2 | 2 | |
| 3 | -import com.alibaba.fastjson2.JSON; | |
| 4 | -import com.alibaba.fastjson2.JSONObject; | |
| 5 | -import com.genersoft.iot.vmp.utils.redis.RedisUtil; | |
| 3 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 6 | 4 | |
| 7 | 5 | import java.util.Objects; |
| 8 | 6 | |
| ... | ... | @@ -26,8 +24,8 @@ public final class JsonUtil { |
| 26 | 24 | * @param <T> |
| 27 | 25 | * @return result type |
| 28 | 26 | */ |
| 29 | - public static <T> T redisJsonToObject(String key, Class<T> clazz) { | |
| 30 | - Object jsonObject = RedisUtil.get(key); | |
| 27 | + public static <T> T redisJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, Class<T> clazz) { | |
| 28 | + Object jsonObject = redisTemplate.opsForValue().get(key); | |
| 31 | 29 | if (Objects.isNull(jsonObject)) { |
| 32 | 30 | return null; |
| 33 | 31 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
| 1 | 1 | package com.genersoft.iot.vmp.utils.redis; |
| 2 | 2 | |
| 3 | -import com.alibaba.fastjson2.JSONObject; | |
| 4 | -import com.genersoft.iot.vmp.utils.SpringBeanFactory; | |
| 5 | -import org.springframework.data.redis.core.*; | |
| 6 | -import org.springframework.util.CollectionUtils; | |
| 3 | +import org.springframework.data.redis.core.Cursor; | |
| 4 | +import org.springframework.data.redis.core.RedisCallback; | |
| 5 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 6 | +import org.springframework.data.redis.core.ScanOptions; | |
| 7 | 7 | |
| 8 | -import java.util.*; | |
| 9 | -import java.util.concurrent.TimeUnit; | |
| 8 | +import java.util.ArrayList; | |
| 9 | +import java.util.HashSet; | |
| 10 | +import java.util.List; | |
| 11 | +import java.util.Set; | |
| 10 | 12 | |
| 11 | -/** | |
| 13 | +/** | |
| 12 | 14 | * Redis工具类 |
| 15 | + * | |
| 13 | 16 | * @author swwheihei |
| 14 | 17 | * @date 2020年5月6日 下午8:27:29 |
| 15 | 18 | */ |
| 16 | 19 | @SuppressWarnings(value = {"rawtypes", "unchecked"}) |
| 17 | 20 | public class RedisUtil { |
| 18 | 21 | |
| 19 | - private static RedisTemplate redisTemplate; | |
| 20 | - | |
| 21 | - static { | |
| 22 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 23 | - } | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * 指定缓存失效时间 | |
| 27 | - * @param key 键 | |
| 28 | - * @param time 时间(秒) | |
| 29 | - * @return true / false | |
| 30 | - */ | |
| 31 | - public static boolean expire(String key, long time) { | |
| 32 | - if (redisTemplate == null) { | |
| 33 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 34 | - } | |
| 35 | - try { | |
| 36 | - if (time > 0) { | |
| 37 | - redisTemplate.expire(key, time, TimeUnit.SECONDS); | |
| 38 | - } | |
| 39 | - return true; | |
| 40 | - } catch (Exception e) { | |
| 41 | - e.printStackTrace(); | |
| 42 | - return false; | |
| 43 | - } | |
| 44 | - } | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * 根据 key 获取过期时间 | |
| 48 | - * @param key 键 | |
| 49 | - */ | |
| 50 | - public static long getExpire(String key) { | |
| 51 | - if (redisTemplate == null) { | |
| 52 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 53 | - } | |
| 54 | - return redisTemplate.getExpire(key, TimeUnit.SECONDS); | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * 判断 key 是否存在 | |
| 59 | - * @param key 键 | |
| 60 | - * @return true / false | |
| 61 | - */ | |
| 62 | - public static boolean hasKey(String key) { | |
| 63 | - if (redisTemplate == null) { | |
| 64 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 65 | - } | |
| 66 | - try { | |
| 67 | - return redisTemplate.hasKey(key); | |
| 68 | - } catch (Exception e) { | |
| 69 | - e.printStackTrace(); | |
| 70 | - return false; | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * 删除缓存 | |
| 76 | - * @SuppressWarnings("unchecked") 忽略类型转换警告 | |
| 77 | - * @param key 键(一个或者多个) | |
| 78 | - */ | |
| 79 | - public static boolean del(String... key) { | |
| 80 | - if (redisTemplate == null) { | |
| 81 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 82 | - } | |
| 83 | - try { | |
| 84 | - if (key != null && key.length > 0) { | |
| 85 | - if (key.length == 1) { | |
| 86 | - redisTemplate.delete(key[0]); | |
| 87 | - } else { | |
| 88 | -// 传入一个 Collection<String> 集合 | |
| 89 | - redisTemplate.delete(CollectionUtils.arrayToList(key)); | |
| 90 | - } | |
| 91 | - } | |
| 92 | - return true; | |
| 93 | - } catch (Exception e) { | |
| 94 | - e.printStackTrace(); | |
| 95 | - return false; | |
| 96 | - } | |
| 97 | - } | |
| 98 | - | |
| 99 | -// ============================== String ============================== | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * 普通缓存获取 | |
| 103 | - * @param key 键 | |
| 104 | - * @return 值 | |
| 105 | - */ | |
| 106 | - public static Object get(String key) { | |
| 107 | - if (redisTemplate == null) { | |
| 108 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 109 | - } | |
| 110 | - return key == null ? null : redisTemplate.opsForValue().get(key); | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * 普通缓存放入 | |
| 115 | - * @param key 键 | |
| 116 | - * @param value 值 | |
| 117 | - * @return true / false | |
| 118 | - */ | |
| 119 | - public static boolean set(String key, Object value) { | |
| 120 | - if (redisTemplate == null) { | |
| 121 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 122 | - } | |
| 123 | - try { | |
| 124 | - redisTemplate.opsForValue().set(key, value); | |
| 125 | - return true; | |
| 126 | - } catch (Exception e) { | |
| 127 | - e.printStackTrace(); | |
| 128 | - return false; | |
| 129 | - } | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | - * 普通缓存放入并设置时间 | |
| 134 | - * @param key 键 | |
| 135 | - * @param value 值 | |
| 136 | - * @param time 时间(秒),如果 time < 0 则设置无限时间 | |
| 137 | - * @return true / false | |
| 138 | - */ | |
| 139 | - public static boolean set(String key, Object value, long time) { | |
| 140 | - if (redisTemplate == null) { | |
| 141 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 142 | - } | |
| 143 | - try { | |
| 144 | - if (time > 0) { | |
| 145 | - redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); | |
| 146 | - } else { | |
| 147 | - set(key, value); | |
| 148 | - } | |
| 149 | - return true; | |
| 150 | - } catch (Exception e) { | |
| 151 | - e.printStackTrace(); | |
| 152 | - return false; | |
| 153 | - } | |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * 递增 | |
| 158 | - * @param key 键 | |
| 159 | - * @param delta 递增大小 | |
| 160 | - * @return | |
| 161 | - */ | |
| 162 | - public static long incr(String key, long delta) { | |
| 163 | - if (redisTemplate == null) { | |
| 164 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 165 | - } | |
| 166 | - if (delta < 0) { | |
| 167 | - throw new RuntimeException("递增因子必须大于 0"); | |
| 168 | - } | |
| 169 | - return redisTemplate.opsForValue().increment(key, delta); | |
| 170 | - } | |
| 171 | - | |
| 172 | - /** | |
| 173 | - * 递减 | |
| 174 | - * @param key 键 | |
| 175 | - * @param delta 递减大小 | |
| 176 | - * @return | |
| 177 | - */ | |
| 178 | - public static long decr(String key, long delta) { | |
| 179 | - if (redisTemplate == null) { | |
| 180 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 181 | - } | |
| 182 | - if (delta < 0) { | |
| 183 | - throw new RuntimeException("递减因子必须大于 0"); | |
| 184 | - } | |
| 185 | - return redisTemplate.opsForValue().increment(key, delta); | |
| 186 | - } | |
| 187 | - | |
| 188 | -// ============================== Map ============================== | |
| 189 | - | |
| 190 | - /** | |
| 191 | - * HashGet | |
| 192 | - * @param key 键(no null) | |
| 193 | - * @param item 项(no null) | |
| 194 | - * @return 值 | |
| 195 | - */ | |
| 196 | - public static Object hget(String key, String item) { | |
| 197 | - if (redisTemplate == null) { | |
| 198 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 199 | - } | |
| 200 | - return redisTemplate.opsForHash().get(key, item); | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * 获取 key 对应的 map | |
| 205 | - * @param key 键(no null) | |
| 206 | - * @return 对应的多个键值 | |
| 207 | - */ | |
| 208 | - public static Map<Object, Object> hmget(String key) { | |
| 209 | - if (redisTemplate == null) { | |
| 210 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 211 | - } | |
| 212 | - return redisTemplate.opsForHash().entries(key); | |
| 213 | - } | |
| 214 | - | |
| 215 | - /** | |
| 216 | - * HashSet | |
| 217 | - * @param key 键 | |
| 218 | - * @param map 值 | |
| 219 | - * @return true / false | |
| 220 | - */ | |
| 221 | - public static boolean hmset(String key, Map<Object, Object> map) { | |
| 222 | - if (redisTemplate == null) { | |
| 223 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 224 | - } | |
| 225 | - try { | |
| 226 | - redisTemplate.opsForHash().putAll(key, map); | |
| 227 | - return true; | |
| 228 | - } catch (Exception e) { | |
| 229 | - e.printStackTrace(); | |
| 230 | - return false; | |
| 231 | - } | |
| 232 | - } | |
| 233 | - | |
| 234 | - /** | |
| 235 | - * HashSet 并设置时间 | |
| 236 | - * @param key 键 | |
| 237 | - * @param map 值 | |
| 238 | - * @param time 时间 | |
| 239 | - * @return true / false | |
| 240 | - */ | |
| 241 | - public static boolean hmset(String key, Map<?, ?> map, long time) { | |
| 242 | - if (redisTemplate == null) { | |
| 243 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 244 | - } | |
| 245 | - try { | |
| 246 | - redisTemplate.opsForHash().putAll(key, map); | |
| 247 | - if (time > 0) { | |
| 248 | - expire(key, time); | |
| 249 | - } | |
| 250 | - return true; | |
| 251 | - } catch (Exception e) { | |
| 252 | - e.printStackTrace(); | |
| 253 | - return false; | |
| 254 | - } | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * 向一张 Hash表 中放入数据,如不存在则创建 | |
| 259 | - * @param key 键 | |
| 260 | - * @param item 项 | |
| 261 | - * @param value 值 | |
| 262 | - * @return true / false | |
| 263 | - */ | |
| 264 | - public static boolean hset(String key, String item, Object value) { | |
| 265 | - if (redisTemplate == null) { | |
| 266 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 267 | - } | |
| 268 | - try { | |
| 269 | - redisTemplate.opsForHash().put(key, item, value); | |
| 270 | - return true; | |
| 271 | - } catch (Exception e) { | |
| 272 | - e.printStackTrace(); | |
| 273 | - return false; | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * 向一张 Hash表 中放入数据,并设置时间,如不存在则创建 | |
| 279 | - * @param key 键 | |
| 280 | - * @param item 项 | |
| 281 | - * @param value 值 | |
| 282 | - * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖) | |
| 283 | - * @return true / false | |
| 284 | - */ | |
| 285 | - public static boolean hset(String key, String item, Object value, long time) { | |
| 286 | - if (redisTemplate == null) { | |
| 287 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 288 | - } | |
| 289 | - try { | |
| 290 | - redisTemplate.opsForHash().put(key, item, value); | |
| 291 | - if (time > 0) { | |
| 292 | - expire(key, time); | |
| 293 | - } | |
| 294 | - return true; | |
| 295 | - } catch (Exception e) { | |
| 296 | - e.printStackTrace(); | |
| 297 | - return false; | |
| 298 | - } | |
| 299 | - } | |
| 300 | - | |
| 301 | - /** | |
| 302 | - * 删除 Hash表 中的值 | |
| 303 | - * @param key 键 | |
| 304 | - * @param item 项(可以多个,no null) | |
| 305 | - */ | |
| 306 | - public static void hdel(String key, Object... item) { | |
| 307 | - if (redisTemplate == null) { | |
| 308 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 309 | - } | |
| 310 | - redisTemplate.opsForHash().delete(key, item); | |
| 311 | - } | |
| 312 | - | |
| 313 | - /** | |
| 314 | - * 判断 Hash表 中是否有该键的值 | |
| 315 | - * @param key 键(no null) | |
| 316 | - * @param item 值(no null) | |
| 317 | - * @return true / false | |
| 318 | - */ | |
| 319 | - public static boolean hHasKey(String key, String item) { | |
| 320 | - if (redisTemplate == null) { | |
| 321 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 322 | - } | |
| 323 | - return redisTemplate.opsForHash().hasKey(key, item); | |
| 324 | - } | |
| 325 | - | |
| 326 | - /** | |
| 327 | - * Hash递增,如果不存在则创建一个,并把新增的值返回 | |
| 328 | - * @param key 键 | |
| 329 | - * @param item 项 | |
| 330 | - * @param by 递增大小 > 0 | |
| 331 | - * @return | |
| 332 | - */ | |
| 333 | - public static Double hincr(String key, String item, Double by) { | |
| 334 | - if (redisTemplate == null) { | |
| 335 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 336 | - } | |
| 337 | - return redisTemplate.opsForHash().increment(key, item, by); | |
| 338 | - } | |
| 339 | - | |
| 340 | - /** | |
| 341 | - * Hash递减 | |
| 342 | - * @param key 键 | |
| 343 | - * @param item 项 | |
| 344 | - * @param by 递减大小 | |
| 345 | - * @return | |
| 346 | - */ | |
| 347 | - public static Double hdecr(String key, String item, Double by) { | |
| 348 | - if (redisTemplate == null) { | |
| 349 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 350 | - } | |
| 351 | - return redisTemplate.opsForHash().increment(key, item, -by); | |
| 352 | - } | |
| 353 | - | |
| 354 | -// ============================== Set ============================== | |
| 355 | - | |
| 356 | - /** | |
| 357 | - * 根据 key 获取 set 中的所有值 | |
| 358 | - * @param key 键 | |
| 359 | - * @return 值 | |
| 360 | - */ | |
| 361 | - public static Set<Object> sGet(String key) { | |
| 362 | - if (redisTemplate == null) { | |
| 363 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 364 | - } | |
| 365 | - try { | |
| 366 | - return redisTemplate.opsForSet().members(key); | |
| 367 | - } catch (Exception e) { | |
| 368 | - e.printStackTrace(); | |
| 369 | - return null; | |
| 370 | - } | |
| 371 | - } | |
| 372 | - | |
| 373 | - /** | |
| 374 | - * 从键为 key 的 set 中,根据 value 查询是否存在 | |
| 375 | - * @param key 键 | |
| 376 | - * @param value 值 | |
| 377 | - * @return true / false | |
| 378 | - */ | |
| 379 | - public static boolean sHasKey(String key, Object value) { | |
| 380 | - if (redisTemplate == null) { | |
| 381 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 382 | - } | |
| 383 | - try { | |
| 384 | - return redisTemplate.opsForSet().isMember(key, value); | |
| 385 | - } catch (Exception e) { | |
| 386 | - e.printStackTrace(); | |
| 387 | - return false; | |
| 388 | - } | |
| 389 | - } | |
| 390 | - | |
| 391 | - /** | |
| 392 | - * 将数据放入 set缓存 | |
| 393 | - * @param key 键值 | |
| 394 | - * @param values 值(可以多个) | |
| 395 | - * @return 成功个数 | |
| 396 | - */ | |
| 397 | - public static long sSet(String key, Object... values) { | |
| 398 | - if (redisTemplate == null) { | |
| 399 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 400 | - } | |
| 401 | - try { | |
| 402 | - return redisTemplate.opsForSet().add(key, values); | |
| 403 | - } catch (Exception e) { | |
| 404 | - e.printStackTrace(); | |
| 405 | - return 0; | |
| 406 | - } | |
| 407 | - } | |
| 408 | - | |
| 409 | - /** | |
| 410 | - * 将数据放入 set缓存,并设置时间 | |
| 411 | - * @param key 键 | |
| 412 | - * @param time 时间 | |
| 413 | - * @param values 值(可以多个) | |
| 414 | - * @return 成功放入个数 | |
| 415 | - */ | |
| 416 | - public static long sSet(String key, long time, Object... values) { | |
| 417 | - if (redisTemplate == null) { | |
| 418 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 419 | - } | |
| 420 | - try { | |
| 421 | - long count = redisTemplate.opsForSet().add(key, values); | |
| 422 | - if (time > 0) { | |
| 423 | - expire(key, time); | |
| 424 | - } | |
| 425 | - return count; | |
| 426 | - } catch (Exception e) { | |
| 427 | - e.printStackTrace(); | |
| 428 | - return 0; | |
| 429 | - } | |
| 430 | - } | |
| 431 | - | |
| 432 | - /** | |
| 433 | - * 获取 set缓存的长度 | |
| 434 | - * @param key 键 | |
| 435 | - * @return 长度 | |
| 436 | - */ | |
| 437 | - public static long sGetSetSize(String key) { | |
| 438 | - if (redisTemplate == null) { | |
| 439 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 440 | - } | |
| 441 | - try { | |
| 442 | - return redisTemplate.opsForSet().size(key); | |
| 443 | - } catch (Exception e) { | |
| 444 | - e.printStackTrace(); | |
| 445 | - return 0; | |
| 446 | - } | |
| 447 | - } | |
| 448 | - | |
| 449 | - /** | |
| 450 | - * 移除 set缓存中,值为 value 的 | |
| 451 | - * @param key 键 | |
| 452 | - * @param values 值 | |
| 453 | - * @return 成功移除个数 | |
| 454 | - */ | |
| 455 | - public static long setRemove(String key, Object... values) { | |
| 456 | - if (redisTemplate == null) { | |
| 457 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 458 | - } | |
| 459 | - try { | |
| 460 | - return redisTemplate.opsForSet().remove(key, values); | |
| 461 | - } catch (Exception e) { | |
| 462 | - e.printStackTrace(); | |
| 463 | - return 0; | |
| 464 | - } | |
| 465 | - } | |
| 466 | -// ============================== ZSet ============================== | |
| 467 | - | |
| 468 | - /** | |
| 469 | - * 添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能; zadd | |
| 470 | - * | |
| 471 | - * @param key | |
| 472 | - * @param value | |
| 473 | - * @param score | |
| 474 | - */ | |
| 475 | - public static void zAdd(Object key, Object value, double score) { | |
| 476 | - if (redisTemplate == null) { | |
| 477 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 478 | - } | |
| 479 | - redisTemplate.opsForZSet().add(key, value, score); | |
| 480 | - } | |
| 481 | - | |
| 482 | - /** | |
| 483 | - * 删除元素 zrem | |
| 484 | - * | |
| 485 | - * @param key | |
| 486 | - * @param value | |
| 487 | - */ | |
| 488 | - public static void zRemove(Object key, Object value) { | |
| 489 | - if (redisTemplate == null) { | |
| 490 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 491 | - } | |
| 492 | - redisTemplate.opsForZSet().remove(key, value); | |
| 493 | - } | |
| 494 | - | |
| 495 | - /** | |
| 496 | - * score的增加or减少 zincrby | |
| 497 | - * | |
| 498 | - * @param key | |
| 499 | - * @param value | |
| 500 | - * @param delta -1 表示减 1 表示加1 | |
| 501 | - */ | |
| 502 | - public static Double zIncrScore(Object key, Object value, double delta) { | |
| 503 | - if (redisTemplate == null) { | |
| 504 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 505 | - } | |
| 506 | - return redisTemplate.opsForZSet().incrementScore(key, value, delta); | |
| 507 | - } | |
| 508 | - | |
| 509 | - /** | |
| 510 | - * 查询value对应的score zscore | |
| 511 | - * | |
| 512 | - * @param key | |
| 513 | - * @param value | |
| 514 | - * @return | |
| 515 | - */ | |
| 516 | - public static Double zScore(Object key, Object value) { | |
| 517 | - if (redisTemplate == null) { | |
| 518 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 519 | - } | |
| 520 | - return redisTemplate.opsForZSet().score(key, value); | |
| 521 | - } | |
| 522 | - | |
| 523 | - /** | |
| 524 | - * 判断value在zset中的排名 zrank | |
| 525 | - * | |
| 526 | - * @param key | |
| 527 | - * @param value | |
| 528 | - * @return | |
| 529 | - */ | |
| 530 | - public static Long zRank(Object key, Object value) { | |
| 531 | - if (redisTemplate == null) { | |
| 532 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 533 | - } | |
| 534 | - return redisTemplate.opsForZSet().rank(key, value); | |
| 535 | - } | |
| 536 | - | |
| 537 | - /** | |
| 538 | - * 返回集合的长度 | |
| 539 | - * | |
| 540 | - * @param key | |
| 541 | - * @return | |
| 542 | - */ | |
| 543 | - public static Long zSize(Object key) { | |
| 544 | - if (redisTemplate == null) { | |
| 545 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 546 | - } | |
| 547 | - return redisTemplate.opsForZSet().zCard(key); | |
| 548 | - } | |
| 549 | - | |
| 550 | - /** | |
| 551 | - * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容 zrange | |
| 552 | - * | |
| 553 | - * 返回有序的集合,score小的在前面 | |
| 554 | - * | |
| 555 | - * @param key | |
| 556 | - * @param start | |
| 557 | - * @param end | |
| 558 | - * @return | |
| 559 | - */ | |
| 560 | - public static Set<Object> zRange(Object key, int start, int end) { | |
| 561 | - if (redisTemplate == null) { | |
| 562 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 563 | - } | |
| 564 | - return redisTemplate.opsForZSet().range(key, start, end); | |
| 565 | - } | |
| 566 | - /** | |
| 567 | - * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容 | |
| 568 | - * | |
| 569 | - * @param key | |
| 570 | - * @param start | |
| 571 | - * @param end | |
| 572 | - * @return | |
| 573 | - */ | |
| 574 | - public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | |
| 575 | - if (redisTemplate == null) { | |
| 576 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 577 | - } | |
| 578 | - return redisTemplate.opsForZSet().rangeWithScores(key, start, end); | |
| 579 | - } | |
| 580 | - /** | |
| 581 | - * 查询集合中指定顺序的值 zrevrange | |
| 582 | - * | |
| 583 | - * 返回有序的集合中,score大的在前面 | |
| 584 | - * | |
| 585 | - * @param key | |
| 586 | - * @param start | |
| 587 | - * @param end | |
| 588 | - * @return | |
| 589 | - */ | |
| 590 | - public static Set<String> zRevRange(Object key, int start, int end) { | |
| 591 | - if (redisTemplate == null) { | |
| 592 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 593 | - } | |
| 594 | - return redisTemplate.opsForZSet().reverseRange(key, start, end); | |
| 595 | - } | |
| 596 | - /** | |
| 597 | - * 根据score的值,来获取满足条件的集合 zrangebyscore | |
| 598 | - * | |
| 599 | - * @param key | |
| 600 | - * @param min | |
| 601 | - * @param max | |
| 602 | - * @return | |
| 603 | - */ | |
| 604 | - public static Set<String> zSortRange(Object key, int min, int max) { | |
| 605 | - if (redisTemplate == null) { | |
| 606 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 607 | - } | |
| 608 | - return redisTemplate.opsForZSet().rangeByScore(key, min, max); | |
| 609 | - } | |
| 610 | - | |
| 611 | - | |
| 612 | -// ============================== List ============================== | |
| 613 | - | |
| 614 | - /** | |
| 615 | - * 获取 list缓存的内容 | |
| 616 | - * @param key 键 | |
| 617 | - * @param start 开始 | |
| 618 | - * @param end 结束(0 到 -1 代表所有值) | |
| 619 | - * @return | |
| 620 | - */ | |
| 621 | - public static List<Object> lGet(String key, long start, long end) { | |
| 622 | - if (redisTemplate == null) { | |
| 623 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 624 | - } | |
| 625 | - try { | |
| 626 | - return redisTemplate.opsForList().range(key, start, end); | |
| 627 | - } catch (Exception e) { | |
| 628 | - e.printStackTrace(); | |
| 629 | - return null; | |
| 630 | - } | |
| 631 | - } | |
| 632 | - | |
| 633 | - /** | |
| 634 | - * 获取 list缓存的长度 | |
| 635 | - * @param key 键 | |
| 636 | - * @return 长度 | |
| 637 | - */ | |
| 638 | - public static long lGetListSize(String key) { | |
| 639 | - if (redisTemplate == null) { | |
| 640 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 641 | - } | |
| 642 | - try { | |
| 643 | - return redisTemplate.opsForList().size(key); | |
| 644 | - } catch (Exception e) { | |
| 645 | - e.printStackTrace(); | |
| 646 | - return 0; | |
| 647 | - } | |
| 648 | - } | |
| 649 | - | |
| 650 | - /** | |
| 651 | - * 根据索引 index 获取键为 key 的 list 中的元素 | |
| 652 | - * @param key 键 | |
| 653 | - * @param index 索引 | |
| 654 | - * 当 index >= 0 时 {0:表头, 1:第二个元素} | |
| 655 | - * 当 index < 0 时 {-1:表尾, -2:倒数第二个元素} | |
| 656 | - * @return 值 | |
| 657 | - */ | |
| 658 | - public static Object lGetIndex(String key, long index) { | |
| 659 | - if (redisTemplate == null) { | |
| 660 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 661 | - } | |
| 662 | - try { | |
| 663 | - return redisTemplate.opsForList().index(key, index); | |
| 664 | - } catch (Exception e) { | |
| 665 | - e.printStackTrace(); | |
| 666 | - return null; | |
| 667 | - } | |
| 668 | - } | |
| 669 | - | |
| 670 | - /** | |
| 671 | - * 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list | |
| 672 | - * @param key 键 | |
| 673 | - * @param value 值 | |
| 674 | - * @return true / false | |
| 675 | - */ | |
| 676 | - public static boolean lSet(String key, Object value) { | |
| 677 | - if (redisTemplate == null) { | |
| 678 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 679 | - } | |
| 680 | - try { | |
| 681 | - redisTemplate.opsForList().rightPush(key, value); | |
| 682 | - return true; | |
| 683 | - } catch (Exception e) { | |
| 684 | - e.printStackTrace(); | |
| 685 | - return false; | |
| 686 | - } | |
| 687 | - } | |
| 688 | - | |
| 689 | - /** | |
| 690 | - * 将值 value 插入键为 key 的 list 中,并设置时间 | |
| 691 | - * @param key 键 | |
| 692 | - * @param value 值 | |
| 693 | - * @param time 时间 | |
| 694 | - * @return true / false | |
| 695 | - */ | |
| 696 | - public static boolean lSet(String key, Object value, long time) { | |
| 697 | - if (redisTemplate == null) { | |
| 698 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 699 | - } | |
| 700 | - try { | |
| 701 | - redisTemplate.opsForList().rightPush(key, value); | |
| 702 | - if (time > 0) { | |
| 703 | - expire(key, time); | |
| 704 | - } | |
| 705 | - return true; | |
| 706 | - } catch (Exception e) { | |
| 707 | - e.printStackTrace(); | |
| 708 | - return false; | |
| 709 | - } | |
| 710 | - } | |
| 711 | - | |
| 712 | - /** | |
| 713 | - * 将 values 插入键为 key 的 list 中 | |
| 714 | - * @param key 键 | |
| 715 | - * @param values 值 | |
| 716 | - * @return true / false | |
| 717 | - */ | |
| 718 | - public static boolean lSetList(String key, List<Object> values) { | |
| 719 | - if (redisTemplate == null) { | |
| 720 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 721 | - } | |
| 722 | - try { | |
| 723 | - redisTemplate.opsForList().rightPushAll(key, values); | |
| 724 | - return true; | |
| 725 | - } catch (Exception e) { | |
| 726 | - e.printStackTrace(); | |
| 727 | - return false; | |
| 728 | - } | |
| 729 | - } | |
| 730 | - | |
| 731 | - /** | |
| 732 | - * 将 values 插入键为 key 的 list 中,并设置时间 | |
| 733 | - * @param key 键 | |
| 734 | - * @param values 值 | |
| 735 | - * @param time 时间 | |
| 736 | - * @return true / false | |
| 737 | - */ | |
| 738 | - public static boolean lSetList(String key, List<Object> values, long time) { | |
| 739 | - if (redisTemplate == null) { | |
| 740 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 741 | - } | |
| 742 | - try { | |
| 743 | - redisTemplate.opsForList().rightPushAll(key, values); | |
| 744 | - if (time > 0) { | |
| 745 | - expire(key, time); | |
| 746 | - } | |
| 747 | - return true; | |
| 748 | - } catch (Exception e) { | |
| 749 | - e.printStackTrace(); | |
| 750 | - return false; | |
| 751 | - } | |
| 752 | - } | |
| 753 | - | |
| 754 | - /** | |
| 755 | - * 根据索引 index 修改键为 key 的值 | |
| 756 | - * @param key 键 | |
| 757 | - * @param index 索引 | |
| 758 | - * @param value 值 | |
| 759 | - * @return true / false | |
| 760 | - */ | |
| 761 | - public static boolean lUpdateIndex(String key, long index, Object value) { | |
| 762 | - if (redisTemplate == null) { | |
| 763 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 764 | - } | |
| 765 | - try { | |
| 766 | - redisTemplate.opsForList().set(key, index, value); | |
| 767 | - return true; | |
| 768 | - } catch (Exception e) { | |
| 769 | - e.printStackTrace(); | |
| 770 | - return false; | |
| 771 | - } | |
| 772 | - } | |
| 773 | - | |
| 774 | - /** | |
| 775 | - * 在键为 key 的 list 中删除值为 value 的元素 | |
| 776 | - * @param key 键 | |
| 777 | - * @param count 如果 count == 0 则删除 list 中所有值为 value 的元素 | |
| 778 | - * 如果 count > 0 则删除 list 中最左边那个值为 value 的元素 | |
| 779 | - * 如果 count < 0 则删除 list 中最右边那个值为 value 的元素 | |
| 780 | - * @param value | |
| 781 | - * @return | |
| 782 | - */ | |
| 783 | - public static long lRemove(String key, long count, Object value) { | |
| 784 | - if (redisTemplate == null) { | |
| 785 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 786 | - } | |
| 787 | - try { | |
| 788 | - return redisTemplate.opsForList().remove(key, count, value); | |
| 789 | - } catch (Exception e) { | |
| 790 | - e.printStackTrace(); | |
| 791 | - return 0; | |
| 792 | - } | |
| 793 | - } | |
| 794 | - | |
| 795 | - /** | |
| 796 | - * 在键为 key 的 list中移除第一个元素 | |
| 797 | - * @param key 键 | |
| 798 | - * @return | |
| 799 | - */ | |
| 800 | - public static Object lLeftPop(String key) { | |
| 801 | - if (redisTemplate == null) { | |
| 802 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 803 | - } | |
| 804 | - return redisTemplate.opsForList().leftPop(key); | |
| 805 | - } | |
| 806 | - | |
| 807 | - /** | |
| 808 | - * 在键为 key 的 list中移除、最后一个元素 | |
| 809 | - * @param key 键 | |
| 810 | - * @return | |
| 811 | - */ | |
| 812 | - public static Object lrightPop(String key) { | |
| 813 | - if (redisTemplate == null) { | |
| 814 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 815 | - } | |
| 816 | - return redisTemplate.opsForList().rightPop(key); | |
| 817 | - } | |
| 818 | - | |
| 819 | - /** | |
| 820 | - * 模糊查询 | |
| 821 | - * @param key 键 | |
| 822 | - * @return true / false | |
| 823 | - */ | |
| 824 | - public static List<Object> keys(String key) { | |
| 825 | - if (redisTemplate == null) { | |
| 826 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 827 | - } | |
| 828 | - try { | |
| 829 | - Set<String> set = redisTemplate.keys(key); | |
| 830 | - return new ArrayList<>(set); | |
| 831 | - } catch (Exception e) { | |
| 832 | - e.printStackTrace(); | |
| 833 | - return null; | |
| 834 | - } | |
| 835 | - } | |
| 836 | - | |
| 837 | - | |
| 838 | 22 | /** |
| 839 | 23 | * 模糊查询 |
| 24 | + * | |
| 840 | 25 | * @param query 查询参数 |
| 841 | 26 | * @return |
| 842 | 27 | */ |
| 843 | -// public static List<Object> scan(String query) { | |
| 844 | -// List<Object> result = new ArrayList<>(); | |
| 845 | -// try { | |
| 846 | -// Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field", | |
| 847 | -// ScanOptions.scanOptions().match(query).count(1000).build()); | |
| 848 | -// while (cursor.hasNext()) { | |
| 849 | -// Map.Entry<Object,Object> entry = cursor.next(); | |
| 850 | -// result.add(entry.getKey()); | |
| 851 | -// Object key = entry.getKey(); | |
| 852 | -// Object valueSet = entry.getValue(); | |
| 853 | -// } | |
| 854 | -// //关闭cursor | |
| 855 | -// cursor.close(); | |
| 856 | -// } catch (Exception e) { | |
| 857 | -// e.printStackTrace(); | |
| 858 | -// } | |
| 859 | -// return result; | |
| 860 | -// } | |
| 28 | + public static List<Object> scan(RedisTemplate redisTemplate, String query) { | |
| 861 | 29 | |
| 862 | - /** | |
| 863 | - * 模糊查询 | |
| 864 | - * @param query 查询参数 | |
| 865 | - * @return | |
| 866 | - */ | |
| 867 | - public static List<Object> scan(String query) { | |
| 868 | - if (redisTemplate == null) { | |
| 869 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 870 | - } | |
| 871 | 30 | Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { |
| 872 | 31 | ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); |
| 873 | 32 | Cursor<byte[]> scan = connection.scan(scanOptions); |
| ... | ... | @@ -881,19 +40,7 @@ public class RedisUtil { |
| 881 | 40 | |
| 882 | 41 | return new ArrayList<>(resultKeys); |
| 883 | 42 | } |
| 884 | - public static List<Object> scan2(String query) { | |
| 885 | - if (redisTemplate == null) { | |
| 886 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 887 | - } | |
| 888 | - Set<String> keys = redisTemplate.keys(query); | |
| 889 | - return new ArrayList<>(keys); | |
| 890 | - } | |
| 891 | - // ============================== 消息发送与订阅 ============================== | |
| 892 | - public static void convertAndSend(String channel, JSONObject msg) { | |
| 893 | - if (redisTemplate == null) { | |
| 894 | - redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 895 | - } | |
| 896 | - redisTemplate.convertAndSend(channel, msg); | |
| 897 | - } | |
| 898 | - | |
| 899 | 43 | } |
| 44 | + | |
| 45 | + | |
| 46 | + | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil2.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.utils.redis; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson2.JSONObject; | |
| 4 | +import com.genersoft.iot.vmp.utils.SpringBeanFactory; | |
| 5 | +import org.springframework.data.redis.core.*; | |
| 6 | +import org.springframework.util.CollectionUtils; | |
| 7 | + | |
| 8 | +import java.util.*; | |
| 9 | +import java.util.concurrent.TimeUnit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Redis工具类 | |
| 13 | + * @author swwheihei | |
| 14 | + * @date 2020年5月6日 下午8:27:29 | |
| 15 | + */ | |
| 16 | +@SuppressWarnings(value = {"rawtypes", "unchecked"}) | |
| 17 | +public class RedisUtil2 { | |
| 18 | + | |
| 19 | + private static RedisTemplate redisTemplate; | |
| 20 | + | |
| 21 | + static { | |
| 22 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 23 | + } | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * 指定缓存失效时间 | |
| 27 | + * @param key 键 | |
| 28 | + * @param time 时间(秒) | |
| 29 | + * @return true / false | |
| 30 | + */ | |
| 31 | + public static boolean expire(String key, long time) { | |
| 32 | + if (redisTemplate == null) { | |
| 33 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 34 | + } | |
| 35 | + try { | |
| 36 | + if (time > 0) { | |
| 37 | + redisTemplate.expire(key, time, TimeUnit.SECONDS); | |
| 38 | + } | |
| 39 | + return true; | |
| 40 | + } catch (Exception e) { | |
| 41 | + e.printStackTrace(); | |
| 42 | + return false; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 根据 key 获取过期时间 | |
| 48 | + * @param key 键 | |
| 49 | + */ | |
| 50 | + public static long getExpire(String key) { | |
| 51 | + if (redisTemplate == null) { | |
| 52 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 53 | + } | |
| 54 | + return redisTemplate.getExpire(key, TimeUnit.SECONDS); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * 判断 key 是否存在 | |
| 59 | + * @param key 键 | |
| 60 | + * @return true / false | |
| 61 | + */ | |
| 62 | + public static boolean hasKey(String key) { | |
| 63 | + if (redisTemplate == null) { | |
| 64 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 65 | + } | |
| 66 | + try { | |
| 67 | + return redisTemplate.hasKey(key); | |
| 68 | + } catch (Exception e) { | |
| 69 | + e.printStackTrace(); | |
| 70 | + return false; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * 删除缓存 | |
| 76 | + * @SuppressWarnings("unchecked") 忽略类型转换警告 | |
| 77 | + * @param key 键(一个或者多个) | |
| 78 | + */ | |
| 79 | + public static boolean del(String... key) { | |
| 80 | + if (redisTemplate == null) { | |
| 81 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 82 | + } | |
| 83 | + try { | |
| 84 | + if (key != null && key.length > 0) { | |
| 85 | + if (key.length == 1) { | |
| 86 | + redisTemplate.delete(key[0]); | |
| 87 | + } else { | |
| 88 | +// 传入一个 Collection<String> 集合 | |
| 89 | + redisTemplate.delete(CollectionUtils.arrayToList(key)); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + return true; | |
| 93 | + } catch (Exception e) { | |
| 94 | + e.printStackTrace(); | |
| 95 | + return false; | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | +// ============================== String ============================== | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * 普通缓存获取 | |
| 103 | + * @param key 键 | |
| 104 | + * @return 值 | |
| 105 | + */ | |
| 106 | + public static Object get(String key) { | |
| 107 | + if (redisTemplate == null) { | |
| 108 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 109 | + } | |
| 110 | + return key == null ? null : redisTemplate.opsForValue().get(key); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * 普通缓存放入 | |
| 115 | + * @param key 键 | |
| 116 | + * @param value 值 | |
| 117 | + * @return true / false | |
| 118 | + */ | |
| 119 | + public static boolean set(String key, Object value) { | |
| 120 | + if (redisTemplate == null) { | |
| 121 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 122 | + } | |
| 123 | + try { | |
| 124 | + redisTemplate.opsForValue().set(key, value); | |
| 125 | + return true; | |
| 126 | + } catch (Exception e) { | |
| 127 | + e.printStackTrace(); | |
| 128 | + return false; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * 普通缓存放入并设置时间 | |
| 134 | + * @param key 键 | |
| 135 | + * @param value 值 | |
| 136 | + * @param time 时间(秒),如果 time < 0 则设置无限时间 | |
| 137 | + * @return true / false | |
| 138 | + */ | |
| 139 | + public static boolean set(String key, Object value, long time) { | |
| 140 | + if (redisTemplate == null) { | |
| 141 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 142 | + } | |
| 143 | + try { | |
| 144 | + if (time > 0) { | |
| 145 | + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); | |
| 146 | + } else { | |
| 147 | + set(key, value); | |
| 148 | + } | |
| 149 | + return true; | |
| 150 | + } catch (Exception e) { | |
| 151 | + e.printStackTrace(); | |
| 152 | + return false; | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * 递增 | |
| 158 | + * @param key 键 | |
| 159 | + * @param delta 递增大小 | |
| 160 | + * @return | |
| 161 | + */ | |
| 162 | + public static long incr(String key, long delta) { | |
| 163 | + if (redisTemplate == null) { | |
| 164 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 165 | + } | |
| 166 | + if (delta < 0) { | |
| 167 | + throw new RuntimeException("递增因子必须大于 0"); | |
| 168 | + } | |
| 169 | + return redisTemplate.opsForValue().increment(key, delta); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * 递减 | |
| 174 | + * @param key 键 | |
| 175 | + * @param delta 递减大小 | |
| 176 | + * @return | |
| 177 | + */ | |
| 178 | + public static long decr(String key, long delta) { | |
| 179 | + if (redisTemplate == null) { | |
| 180 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 181 | + } | |
| 182 | + if (delta < 0) { | |
| 183 | + throw new RuntimeException("递减因子必须大于 0"); | |
| 184 | + } | |
| 185 | + return redisTemplate.opsForValue().increment(key, delta); | |
| 186 | + } | |
| 187 | + | |
| 188 | +// ============================== Map ============================== | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * HashGet | |
| 192 | + * @param key 键(no null) | |
| 193 | + * @param item 项(no null) | |
| 194 | + * @return 值 | |
| 195 | + */ | |
| 196 | + public static Object hget(String key, String item) { | |
| 197 | + if (redisTemplate == null) { | |
| 198 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 199 | + } | |
| 200 | + return redisTemplate.opsForHash().get(key, item); | |
| 201 | + } | |
| 202 | + | |
| 203 | + /** | |
| 204 | + * 获取 key 对应的 map | |
| 205 | + * @param key 键(no null) | |
| 206 | + * @return 对应的多个键值 | |
| 207 | + */ | |
| 208 | + public static Map<Object, Object> hmget(String key) { | |
| 209 | + if (redisTemplate == null) { | |
| 210 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 211 | + } | |
| 212 | + return redisTemplate.opsForHash().entries(key); | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * HashSet | |
| 217 | + * @param key 键 | |
| 218 | + * @param map 值 | |
| 219 | + * @return true / false | |
| 220 | + */ | |
| 221 | + public static boolean hmset(String key, Map<Object, Object> map) { | |
| 222 | + if (redisTemplate == null) { | |
| 223 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 224 | + } | |
| 225 | + try { | |
| 226 | + redisTemplate.opsForHash().putAll(key, map); | |
| 227 | + return true; | |
| 228 | + } catch (Exception e) { | |
| 229 | + e.printStackTrace(); | |
| 230 | + return false; | |
| 231 | + } | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * HashSet 并设置时间 | |
| 236 | + * @param key 键 | |
| 237 | + * @param map 值 | |
| 238 | + * @param time 时间 | |
| 239 | + * @return true / false | |
| 240 | + */ | |
| 241 | + public static boolean hmset(String key, Map<?, ?> map, long time) { | |
| 242 | + if (redisTemplate == null) { | |
| 243 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 244 | + } | |
| 245 | + try { | |
| 246 | + redisTemplate.opsForHash().putAll(key, map); | |
| 247 | + if (time > 0) { | |
| 248 | + expire(key, time); | |
| 249 | + } | |
| 250 | + return true; | |
| 251 | + } catch (Exception e) { | |
| 252 | + e.printStackTrace(); | |
| 253 | + return false; | |
| 254 | + } | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * 向一张 Hash表 中放入数据,如不存在则创建 | |
| 259 | + * @param key 键 | |
| 260 | + * @param item 项 | |
| 261 | + * @param value 值 | |
| 262 | + * @return true / false | |
| 263 | + */ | |
| 264 | + public static boolean hset(String key, String item, Object value) { | |
| 265 | + if (redisTemplate == null) { | |
| 266 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 267 | + } | |
| 268 | + try { | |
| 269 | + redisTemplate.opsForHash().put(key, item, value); | |
| 270 | + return true; | |
| 271 | + } catch (Exception e) { | |
| 272 | + e.printStackTrace(); | |
| 273 | + return false; | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * 向一张 Hash表 中放入数据,并设置时间,如不存在则创建 | |
| 279 | + * @param key 键 | |
| 280 | + * @param item 项 | |
| 281 | + * @param value 值 | |
| 282 | + * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖) | |
| 283 | + * @return true / false | |
| 284 | + */ | |
| 285 | + public static boolean hset(String key, String item, Object value, long time) { | |
| 286 | + if (redisTemplate == null) { | |
| 287 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 288 | + } | |
| 289 | + try { | |
| 290 | + redisTemplate.opsForHash().put(key, item, value); | |
| 291 | + if (time > 0) { | |
| 292 | + expire(key, time); | |
| 293 | + } | |
| 294 | + return true; | |
| 295 | + } catch (Exception e) { | |
| 296 | + e.printStackTrace(); | |
| 297 | + return false; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + /** | |
| 302 | + * 删除 Hash表 中的值 | |
| 303 | + * @param key 键 | |
| 304 | + * @param item 项(可以多个,no null) | |
| 305 | + */ | |
| 306 | + public static void hdel(String key, Object... item) { | |
| 307 | + if (redisTemplate == null) { | |
| 308 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 309 | + } | |
| 310 | + redisTemplate.opsForHash().delete(key, item); | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * 判断 Hash表 中是否有该键的值 | |
| 315 | + * @param key 键(no null) | |
| 316 | + * @param item 值(no null) | |
| 317 | + * @return true / false | |
| 318 | + */ | |
| 319 | + public static boolean hHasKey(String key, String item) { | |
| 320 | + if (redisTemplate == null) { | |
| 321 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 322 | + } | |
| 323 | + return redisTemplate.opsForHash().hasKey(key, item); | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * Hash递增,如果不存在则创建一个,并把新增的值返回 | |
| 328 | + * @param key 键 | |
| 329 | + * @param item 项 | |
| 330 | + * @param by 递增大小 > 0 | |
| 331 | + * @return | |
| 332 | + */ | |
| 333 | + public static Double hincr(String key, String item, Double by) { | |
| 334 | + if (redisTemplate == null) { | |
| 335 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 336 | + } | |
| 337 | + return redisTemplate.opsForHash().increment(key, item, by); | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Hash递减 | |
| 342 | + * @param key 键 | |
| 343 | + * @param item 项 | |
| 344 | + * @param by 递减大小 | |
| 345 | + * @return | |
| 346 | + */ | |
| 347 | + public static Double hdecr(String key, String item, Double by) { | |
| 348 | + if (redisTemplate == null) { | |
| 349 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 350 | + } | |
| 351 | + return redisTemplate.opsForHash().increment(key, item, -by); | |
| 352 | + } | |
| 353 | + | |
| 354 | +// ============================== Set ============================== | |
| 355 | + | |
| 356 | + /** | |
| 357 | + * 根据 key 获取 set 中的所有值 | |
| 358 | + * @param key 键 | |
| 359 | + * @return 值 | |
| 360 | + */ | |
| 361 | + public static Set<Object> sGet(String key) { | |
| 362 | + if (redisTemplate == null) { | |
| 363 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 364 | + } | |
| 365 | + try { | |
| 366 | + return redisTemplate.opsForSet().members(key); | |
| 367 | + } catch (Exception e) { | |
| 368 | + e.printStackTrace(); | |
| 369 | + return null; | |
| 370 | + } | |
| 371 | + } | |
| 372 | + | |
| 373 | + /** | |
| 374 | + * 从键为 key 的 set 中,根据 value 查询是否存在 | |
| 375 | + * @param key 键 | |
| 376 | + * @param value 值 | |
| 377 | + * @return true / false | |
| 378 | + */ | |
| 379 | + public static boolean sHasKey(String key, Object value) { | |
| 380 | + if (redisTemplate == null) { | |
| 381 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 382 | + } | |
| 383 | + try { | |
| 384 | + return redisTemplate.opsForSet().isMember(key, value); | |
| 385 | + } catch (Exception e) { | |
| 386 | + e.printStackTrace(); | |
| 387 | + return false; | |
| 388 | + } | |
| 389 | + } | |
| 390 | + | |
| 391 | + /** | |
| 392 | + * 将数据放入 set缓存 | |
| 393 | + * @param key 键值 | |
| 394 | + * @param values 值(可以多个) | |
| 395 | + * @return 成功个数 | |
| 396 | + */ | |
| 397 | + public static long sSet(String key, Object... values) { | |
| 398 | + if (redisTemplate == null) { | |
| 399 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 400 | + } | |
| 401 | + try { | |
| 402 | + return redisTemplate.opsForSet().add(key, values); | |
| 403 | + } catch (Exception e) { | |
| 404 | + e.printStackTrace(); | |
| 405 | + return 0; | |
| 406 | + } | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * 将数据放入 set缓存,并设置时间 | |
| 411 | + * @param key 键 | |
| 412 | + * @param time 时间 | |
| 413 | + * @param values 值(可以多个) | |
| 414 | + * @return 成功放入个数 | |
| 415 | + */ | |
| 416 | + public static long sSet(String key, long time, Object... values) { | |
| 417 | + if (redisTemplate == null) { | |
| 418 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 419 | + } | |
| 420 | + try { | |
| 421 | + long count = redisTemplate.opsForSet().add(key, values); | |
| 422 | + if (time > 0) { | |
| 423 | + expire(key, time); | |
| 424 | + } | |
| 425 | + return count; | |
| 426 | + } catch (Exception e) { | |
| 427 | + e.printStackTrace(); | |
| 428 | + return 0; | |
| 429 | + } | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * 获取 set缓存的长度 | |
| 434 | + * @param key 键 | |
| 435 | + * @return 长度 | |
| 436 | + */ | |
| 437 | + public static long sGetSetSize(String key) { | |
| 438 | + if (redisTemplate == null) { | |
| 439 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 440 | + } | |
| 441 | + try { | |
| 442 | + return redisTemplate.opsForSet().size(key); | |
| 443 | + } catch (Exception e) { | |
| 444 | + e.printStackTrace(); | |
| 445 | + return 0; | |
| 446 | + } | |
| 447 | + } | |
| 448 | + | |
| 449 | + /** | |
| 450 | + * 移除 set缓存中,值为 value 的 | |
| 451 | + * @param key 键 | |
| 452 | + * @param values 值 | |
| 453 | + * @return 成功移除个数 | |
| 454 | + */ | |
| 455 | + public static long setRemove(String key, Object... values) { | |
| 456 | + if (redisTemplate == null) { | |
| 457 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 458 | + } | |
| 459 | + try { | |
| 460 | + return redisTemplate.opsForSet().remove(key, values); | |
| 461 | + } catch (Exception e) { | |
| 462 | + e.printStackTrace(); | |
| 463 | + return 0; | |
| 464 | + } | |
| 465 | + } | |
| 466 | +// ============================== ZSet ============================== | |
| 467 | + | |
| 468 | + /** | |
| 469 | + * 添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能; zadd | |
| 470 | + * | |
| 471 | + * @param key | |
| 472 | + * @param value | |
| 473 | + * @param score | |
| 474 | + */ | |
| 475 | + public static void zAdd(Object key, Object value, double score) { | |
| 476 | + if (redisTemplate == null) { | |
| 477 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 478 | + } | |
| 479 | + redisTemplate.opsForZSet().add(key, value, score); | |
| 480 | + } | |
| 481 | + | |
| 482 | + /** | |
| 483 | + * 删除元素 zrem | |
| 484 | + * | |
| 485 | + * @param key | |
| 486 | + * @param value | |
| 487 | + */ | |
| 488 | + public static void zRemove(Object key, Object value) { | |
| 489 | + if (redisTemplate == null) { | |
| 490 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 491 | + } | |
| 492 | + redisTemplate.opsForZSet().remove(key, value); | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 496 | + * score的增加or减少 zincrby | |
| 497 | + * | |
| 498 | + * @param key | |
| 499 | + * @param value | |
| 500 | + * @param delta -1 表示减 1 表示加1 | |
| 501 | + */ | |
| 502 | + public static Double zIncrScore(Object key, Object value, double delta) { | |
| 503 | + if (redisTemplate == null) { | |
| 504 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 505 | + } | |
| 506 | + return redisTemplate.opsForZSet().incrementScore(key, value, delta); | |
| 507 | + } | |
| 508 | + | |
| 509 | + /** | |
| 510 | + * 查询value对应的score zscore | |
| 511 | + * | |
| 512 | + * @param key | |
| 513 | + * @param value | |
| 514 | + * @return | |
| 515 | + */ | |
| 516 | + public static Double zScore(Object key, Object value) { | |
| 517 | + if (redisTemplate == null) { | |
| 518 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 519 | + } | |
| 520 | + return redisTemplate.opsForZSet().score(key, value); | |
| 521 | + } | |
| 522 | + | |
| 523 | + /** | |
| 524 | + * 判断value在zset中的排名 zrank | |
| 525 | + * | |
| 526 | + * @param key | |
| 527 | + * @param value | |
| 528 | + * @return | |
| 529 | + */ | |
| 530 | + public static Long zRank(Object key, Object value) { | |
| 531 | + if (redisTemplate == null) { | |
| 532 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 533 | + } | |
| 534 | + return redisTemplate.opsForZSet().rank(key, value); | |
| 535 | + } | |
| 536 | + | |
| 537 | + /** | |
| 538 | + * 返回集合的长度 | |
| 539 | + * | |
| 540 | + * @param key | |
| 541 | + * @return | |
| 542 | + */ | |
| 543 | + public static Long zSize(Object key) { | |
| 544 | + if (redisTemplate == null) { | |
| 545 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 546 | + } | |
| 547 | + return redisTemplate.opsForZSet().zCard(key); | |
| 548 | + } | |
| 549 | + | |
| 550 | + /** | |
| 551 | + * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容 zrange | |
| 552 | + * | |
| 553 | + * 返回有序的集合,score小的在前面 | |
| 554 | + * | |
| 555 | + * @param key | |
| 556 | + * @param start | |
| 557 | + * @param end | |
| 558 | + * @return | |
| 559 | + */ | |
| 560 | + public static Set<Object> zRange(Object key, int start, int end) { | |
| 561 | + if (redisTemplate == null) { | |
| 562 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 563 | + } | |
| 564 | + return redisTemplate.opsForZSet().range(key, start, end); | |
| 565 | + } | |
| 566 | + /** | |
| 567 | + * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容 | |
| 568 | + * | |
| 569 | + * @param key | |
| 570 | + * @param start | |
| 571 | + * @param end | |
| 572 | + * @return | |
| 573 | + */ | |
| 574 | + public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | |
| 575 | + if (redisTemplate == null) { | |
| 576 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 577 | + } | |
| 578 | + return redisTemplate.opsForZSet().rangeWithScores(key, start, end); | |
| 579 | + } | |
| 580 | + /** | |
| 581 | + * 查询集合中指定顺序的值 zrevrange | |
| 582 | + * | |
| 583 | + * 返回有序的集合中,score大的在前面 | |
| 584 | + * | |
| 585 | + * @param key | |
| 586 | + * @param start | |
| 587 | + * @param end | |
| 588 | + * @return | |
| 589 | + */ | |
| 590 | + public static Set<String> zRevRange(Object key, int start, int end) { | |
| 591 | + if (redisTemplate == null) { | |
| 592 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 593 | + } | |
| 594 | + return redisTemplate.opsForZSet().reverseRange(key, start, end); | |
| 595 | + } | |
| 596 | + /** | |
| 597 | + * 根据score的值,来获取满足条件的集合 zrangebyscore | |
| 598 | + * | |
| 599 | + * @param key | |
| 600 | + * @param min | |
| 601 | + * @param max | |
| 602 | + * @return | |
| 603 | + */ | |
| 604 | + public static Set<String> zSortRange(Object key, int min, int max) { | |
| 605 | + if (redisTemplate == null) { | |
| 606 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 607 | + } | |
| 608 | + return redisTemplate.opsForZSet().rangeByScore(key, min, max); | |
| 609 | + } | |
| 610 | + | |
| 611 | + | |
| 612 | +// ============================== List ============================== | |
| 613 | + | |
| 614 | + /** | |
| 615 | + * 获取 list缓存的内容 | |
| 616 | + * @param key 键 | |
| 617 | + * @param start 开始 | |
| 618 | + * @param end 结束(0 到 -1 代表所有值) | |
| 619 | + * @return | |
| 620 | + */ | |
| 621 | + public static List<Object> lGet(String key, long start, long end) { | |
| 622 | + if (redisTemplate == null) { | |
| 623 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 624 | + } | |
| 625 | + try { | |
| 626 | + return redisTemplate.opsForList().range(key, start, end); | |
| 627 | + } catch (Exception e) { | |
| 628 | + e.printStackTrace(); | |
| 629 | + return null; | |
| 630 | + } | |
| 631 | + } | |
| 632 | + | |
| 633 | + /** | |
| 634 | + * 获取 list缓存的长度 | |
| 635 | + * @param key 键 | |
| 636 | + * @return 长度 | |
| 637 | + */ | |
| 638 | + public static long lGetListSize(String key) { | |
| 639 | + if (redisTemplate == null) { | |
| 640 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 641 | + } | |
| 642 | + try { | |
| 643 | + return redisTemplate.opsForList().size(key); | |
| 644 | + } catch (Exception e) { | |
| 645 | + e.printStackTrace(); | |
| 646 | + return 0; | |
| 647 | + } | |
| 648 | + } | |
| 649 | + | |
| 650 | + /** | |
| 651 | + * 根据索引 index 获取键为 key 的 list 中的元素 | |
| 652 | + * @param key 键 | |
| 653 | + * @param index 索引 | |
| 654 | + * 当 index >= 0 时 {0:表头, 1:第二个元素} | |
| 655 | + * 当 index < 0 时 {-1:表尾, -2:倒数第二个元素} | |
| 656 | + * @return 值 | |
| 657 | + */ | |
| 658 | + public static Object lGetIndex(String key, long index) { | |
| 659 | + if (redisTemplate == null) { | |
| 660 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 661 | + } | |
| 662 | + try { | |
| 663 | + return redisTemplate.opsForList().index(key, index); | |
| 664 | + } catch (Exception e) { | |
| 665 | + e.printStackTrace(); | |
| 666 | + return null; | |
| 667 | + } | |
| 668 | + } | |
| 669 | + | |
| 670 | + /** | |
| 671 | + * 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list | |
| 672 | + * @param key 键 | |
| 673 | + * @param value 值 | |
| 674 | + * @return true / false | |
| 675 | + */ | |
| 676 | + public static boolean lSet(String key, Object value) { | |
| 677 | + if (redisTemplate == null) { | |
| 678 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 679 | + } | |
| 680 | + try { | |
| 681 | + redisTemplate.opsForList().rightPush(key, value); | |
| 682 | + return true; | |
| 683 | + } catch (Exception e) { | |
| 684 | + e.printStackTrace(); | |
| 685 | + return false; | |
| 686 | + } | |
| 687 | + } | |
| 688 | + | |
| 689 | + /** | |
| 690 | + * 将值 value 插入键为 key 的 list 中,并设置时间 | |
| 691 | + * @param key 键 | |
| 692 | + * @param value 值 | |
| 693 | + * @param time 时间 | |
| 694 | + * @return true / false | |
| 695 | + */ | |
| 696 | + public static boolean lSet(String key, Object value, long time) { | |
| 697 | + if (redisTemplate == null) { | |
| 698 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 699 | + } | |
| 700 | + try { | |
| 701 | + redisTemplate.opsForList().rightPush(key, value); | |
| 702 | + if (time > 0) { | |
| 703 | + expire(key, time); | |
| 704 | + } | |
| 705 | + return true; | |
| 706 | + } catch (Exception e) { | |
| 707 | + e.printStackTrace(); | |
| 708 | + return false; | |
| 709 | + } | |
| 710 | + } | |
| 711 | + | |
| 712 | + /** | |
| 713 | + * 将 values 插入键为 key 的 list 中 | |
| 714 | + * @param key 键 | |
| 715 | + * @param values 值 | |
| 716 | + * @return true / false | |
| 717 | + */ | |
| 718 | + public static boolean lSetList(String key, List<Object> values) { | |
| 719 | + if (redisTemplate == null) { | |
| 720 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 721 | + } | |
| 722 | + try { | |
| 723 | + redisTemplate.opsForList().rightPushAll(key, values); | |
| 724 | + return true; | |
| 725 | + } catch (Exception e) { | |
| 726 | + e.printStackTrace(); | |
| 727 | + return false; | |
| 728 | + } | |
| 729 | + } | |
| 730 | + | |
| 731 | + /** | |
| 732 | + * 将 values 插入键为 key 的 list 中,并设置时间 | |
| 733 | + * @param key 键 | |
| 734 | + * @param values 值 | |
| 735 | + * @param time 时间 | |
| 736 | + * @return true / false | |
| 737 | + */ | |
| 738 | + public static boolean lSetList(String key, List<Object> values, long time) { | |
| 739 | + if (redisTemplate == null) { | |
| 740 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 741 | + } | |
| 742 | + try { | |
| 743 | + redisTemplate.opsForList().rightPushAll(key, values); | |
| 744 | + if (time > 0) { | |
| 745 | + expire(key, time); | |
| 746 | + } | |
| 747 | + return true; | |
| 748 | + } catch (Exception e) { | |
| 749 | + e.printStackTrace(); | |
| 750 | + return false; | |
| 751 | + } | |
| 752 | + } | |
| 753 | + | |
| 754 | + /** | |
| 755 | + * 根据索引 index 修改键为 key 的值 | |
| 756 | + * @param key 键 | |
| 757 | + * @param index 索引 | |
| 758 | + * @param value 值 | |
| 759 | + * @return true / false | |
| 760 | + */ | |
| 761 | + public static boolean lUpdateIndex(String key, long index, Object value) { | |
| 762 | + if (redisTemplate == null) { | |
| 763 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 764 | + } | |
| 765 | + try { | |
| 766 | + redisTemplate.opsForList().set(key, index, value); | |
| 767 | + return true; | |
| 768 | + } catch (Exception e) { | |
| 769 | + e.printStackTrace(); | |
| 770 | + return false; | |
| 771 | + } | |
| 772 | + } | |
| 773 | + | |
| 774 | + /** | |
| 775 | + * 在键为 key 的 list 中删除值为 value 的元素 | |
| 776 | + * @param key 键 | |
| 777 | + * @param count 如果 count == 0 则删除 list 中所有值为 value 的元素 | |
| 778 | + * 如果 count > 0 则删除 list 中最左边那个值为 value 的元素 | |
| 779 | + * 如果 count < 0 则删除 list 中最右边那个值为 value 的元素 | |
| 780 | + * @param value | |
| 781 | + * @return | |
| 782 | + */ | |
| 783 | + public static long lRemove(String key, long count, Object value) { | |
| 784 | + if (redisTemplate == null) { | |
| 785 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 786 | + } | |
| 787 | + try { | |
| 788 | + return redisTemplate.opsForList().remove(key, count, value); | |
| 789 | + } catch (Exception e) { | |
| 790 | + e.printStackTrace(); | |
| 791 | + return 0; | |
| 792 | + } | |
| 793 | + } | |
| 794 | + | |
| 795 | + /** | |
| 796 | + * 在键为 key 的 list中移除第一个元素 | |
| 797 | + * @param key 键 | |
| 798 | + * @return | |
| 799 | + */ | |
| 800 | + public static Object lLeftPop(String key) { | |
| 801 | + if (redisTemplate == null) { | |
| 802 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 803 | + } | |
| 804 | + return redisTemplate.opsForList().leftPop(key); | |
| 805 | + } | |
| 806 | + | |
| 807 | + /** | |
| 808 | + * 在键为 key 的 list中移除、最后一个元素 | |
| 809 | + * @param key 键 | |
| 810 | + * @return | |
| 811 | + */ | |
| 812 | + public static Object lrightPop(String key) { | |
| 813 | + if (redisTemplate == null) { | |
| 814 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 815 | + } | |
| 816 | + return redisTemplate.opsForList().rightPop(key); | |
| 817 | + } | |
| 818 | + | |
| 819 | + /** | |
| 820 | + * 模糊查询 | |
| 821 | + * @param key 键 | |
| 822 | + * @return true / false | |
| 823 | + */ | |
| 824 | + public static List<Object> keys(String key) { | |
| 825 | + if (redisTemplate == null) { | |
| 826 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 827 | + } | |
| 828 | + try { | |
| 829 | + Set<String> set = redisTemplate.keys(key); | |
| 830 | + return new ArrayList<>(set); | |
| 831 | + } catch (Exception e) { | |
| 832 | + e.printStackTrace(); | |
| 833 | + return null; | |
| 834 | + } | |
| 835 | + } | |
| 836 | + | |
| 837 | + | |
| 838 | + /** | |
| 839 | + * 模糊查询 | |
| 840 | + * @param query 查询参数 | |
| 841 | + * @return | |
| 842 | + */ | |
| 843 | +// public static List<Object> scan(String query) { | |
| 844 | +// List<Object> result = new ArrayList<>(); | |
| 845 | +// try { | |
| 846 | +// Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field", | |
| 847 | +// ScanOptions.scanOptions().match(query).count(1000).build()); | |
| 848 | +// while (cursor.hasNext()) { | |
| 849 | +// Map.Entry<Object,Object> entry = cursor.next(); | |
| 850 | +// result.add(entry.getKey()); | |
| 851 | +// Object key = entry.getKey(); | |
| 852 | +// Object valueSet = entry.getValue(); | |
| 853 | +// } | |
| 854 | +// //关闭cursor | |
| 855 | +// cursor.close(); | |
| 856 | +// } catch (Exception e) { | |
| 857 | +// e.printStackTrace(); | |
| 858 | +// } | |
| 859 | +// return result; | |
| 860 | +// } | |
| 861 | + | |
| 862 | + /** | |
| 863 | + * 模糊查询 | |
| 864 | + * @param query 查询参数 | |
| 865 | + * @return | |
| 866 | + */ | |
| 867 | + public static List<Object> scan(String query) { | |
| 868 | + if (redisTemplate == null) { | |
| 869 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 870 | + } | |
| 871 | + Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { | |
| 872 | + ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); | |
| 873 | + Cursor<byte[]> scan = connection.scan(scanOptions); | |
| 874 | + Set<String> keys = new HashSet<>(); | |
| 875 | + while (scan.hasNext()) { | |
| 876 | + byte[] next = scan.next(); | |
| 877 | + keys.add(new String(next)); | |
| 878 | + } | |
| 879 | + return keys; | |
| 880 | + }); | |
| 881 | + | |
| 882 | + return new ArrayList<>(resultKeys); | |
| 883 | + } | |
| 884 | + public static List<Object> scan2(String query) { | |
| 885 | + if (redisTemplate == null) { | |
| 886 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 887 | + } | |
| 888 | + Set<String> keys = redisTemplate.keys(query); | |
| 889 | + return new ArrayList<>(keys); | |
| 890 | + } | |
| 891 | + // ============================== 消息发送与订阅 ============================== | |
| 892 | + public static void convertAndSend(String channel, JSONObject msg) { | |
| 893 | + if (redisTemplate == null) { | |
| 894 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 895 | + } | |
| 896 | + redisTemplate.convertAndSend(channel, msg); | |
| 897 | + } | |
| 898 | + | |
| 899 | +} | ... | ... |
src/main/java/com/genersoft/iot/vmp/vmanager/bean/StreamContent.java
| ... | ... | @@ -116,6 +116,9 @@ public class StreamContent { |
| 116 | 116 | if (streamInfo.getFmp4() != null) { |
| 117 | 117 | this.fmp4 = streamInfo.getFmp4().getUrl(); |
| 118 | 118 | } |
| 119 | + if (streamInfo.getHttps_fmp4() != null) { | |
| 120 | + this.https_fmp4 = streamInfo.getHttps_fmp4().getUrl(); | |
| 121 | + } | |
| 119 | 122 | if (streamInfo.getWs_fmp4() != null) { |
| 120 | 123 | this.ws_fmp4 = streamInfo.getWs_fmp4().getUrl(); |
| 121 | 124 | } | ... | ... |