优化部分redis scan查询

pull/1653/head
648540858 2024-10-21 17:42:26 +08:00
parent 9506bef6cb
commit 189e56c839
5 changed files with 43 additions and 64 deletions

View File

@ -18,18 +18,7 @@ public class VideoManagerConstants {
public static final String DEVICE_PREFIX = "VMP_DEVICE_"; public static final String DEVICE_PREFIX = "VMP_DEVICE_";
// 设备同步完成
public static final String DEVICE_SYNC_PREFIX = "VMP_DEVICE_SYNC_";
public static final String CACHEKEY_PREFIX = "VMP_CHANNEL_";
public static final String KEEPLIVEKEY_PREFIX = "VMP_KEEPALIVE_";
// TODO 此处多了一个_暂不修改
public static final String INVITE_PREFIX = "VMP_INVITE_INFO"; public static final String INVITE_PREFIX = "VMP_INVITE_INFO";
public static final String PLAYER_PREFIX = "VMP_INVITE_PLAY_";
public static final String PLAY_BLACK_PREFIX = "VMP_INVITE_PLAYBACK_";
public static final String DOWNLOAD_PREFIX = "VMP_INVITE_DOWNLOAD_";
public static final String PLATFORM_KEEPALIVE_PREFIX = "VMP_PLATFORM_KEEPALIVE_"; public static final String PLATFORM_KEEPALIVE_PREFIX = "VMP_PLATFORM_KEEPALIVE_";
@ -40,30 +29,18 @@ public class VideoManagerConstants {
public static final String PLATFORM_REGISTER_INFO_PREFIX = "VMP_PLATFORM_REGISTER_INFO_"; public static final String PLATFORM_REGISTER_INFO_PREFIX = "VMP_PLATFORM_REGISTER_INFO_";
public static final String SEND_RTP_INFO = "VMP_SEND_RTP_INFO:"; public static final String SEND_RTP_INFO = "VMP_SEND_RTP_INFO:";
public static final String SEND_RTP_INFO_CALLID = "VMP_SEND_RTP_INFO:CALL_ID"; public static final String SEND_RTP_INFO_CALLID = "VMP_SEND_RTP_INFO:CALL_ID:";
public static final String SEND_RTP_INFO_STREAM = "VMP_SEND_RTP_INFO:STREAM"; public static final String SEND_RTP_INFO_STREAM = "VMP_SEND_RTP_INFO:STREAM:";
public static final String SEND_RTP_INFO_CHANNEL = "VMP_SEND_RTP_INFO:CHANNEL"; public static final String SEND_RTP_INFO_CHANNEL = "VMP_SEND_RTP_INFO:CHANNEL:";
public static final String EVENT_ONLINE_REGISTER = "1";
public static final String EVENT_ONLINE_MESSAGE = "3";
public static final String EVENT_OUTLINE_UNREGISTER = "1";
public static final String EVENT_OUTLINE_TIMEOUT = "2";
public static final String MEDIA_SSRC_USED_PREFIX = "VMP_MEDIA_USED_SSRC_";
public static final String SIP_INVITE_SESSION = "VMP_SIP_INVITE_SESSION:"; public static final String SIP_INVITE_SESSION = "VMP_SIP_INVITE_SESSION:";
public static final String SIP_INVITE_SESSION_CALL_ID = SIP_INVITE_SESSION + "CALL_ID" + ":"; public static final String SIP_INVITE_SESSION_CALL_ID = SIP_INVITE_SESSION + "CALL_ID:";
public static final String SIP_INVITE_SESSION_STREAM = SIP_INVITE_SESSION + "STREAM" + ":"; public static final String SIP_INVITE_SESSION_STREAM = SIP_INVITE_SESSION + "STREAM:";
public static final String MEDIA_STREAM_AUTHORITY = "VMP_MEDIA_STREAM_AUTHORITY_"; public static final String MEDIA_STREAM_AUTHORITY = "VMP_MEDIA_STREAM_AUTHORITY_";
public static final String SIP_CSEQ_PREFIX = "VMP_SIP_CSEQ_"; public static final String SIP_CSEQ_PREFIX = "VMP_SIP_CSEQ_";
public static final String SIP_SN_PREFIX = "VMP_SIP_SN_";
public static final String SIP_SUBSCRIBE_PREFIX = "VMP_SIP_SUBSCRIBE_"; public static final String SIP_SUBSCRIBE_PREFIX = "VMP_SIP_SUBSCRIBE_";
public static final String SYSTEM_INFO_CPU_PREFIX = "VMP_SYSTEM_INFO_CPU_"; public static final String SYSTEM_INFO_CPU_PREFIX = "VMP_SYSTEM_INFO_CPU_";
@ -79,7 +56,6 @@ public class VideoManagerConstants {
public static final String PUSH_STREAM_LIST = "VMP_PUSH_STREAM_LIST_"; public static final String PUSH_STREAM_LIST = "VMP_PUSH_STREAM_LIST_";
public static final String WAITE_SEND_PUSH_STREAM = "VMP_WAITE_SEND_PUSH_STREAM:"; public static final String WAITE_SEND_PUSH_STREAM = "VMP_WAITE_SEND_PUSH_STREAM:";
public static final String START_SEND_PUSH_STREAM = "VMP_START_SEND_PUSH_STREAM:"; public static final String START_SEND_PUSH_STREAM = "VMP_START_SEND_PUSH_STREAM:";
public static final String PUSH_STREAM_ONLINE = "VMP_PUSH_STREAM_ONLINE:";

View File

@ -79,10 +79,8 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
InviteInfo inviteInfoForUpdate; InviteInfo inviteInfoForUpdate;
if (InviteSessionStatus.ready == inviteInfo.getStatus()) { if (InviteSessionStatus.ready == inviteInfo.getStatus()) {
if (inviteInfo.getDeviceId() == null if (inviteInfo.getDeviceId() == null || inviteInfo.getChannelId() == null
|| inviteInfo.getChannelId() == null || inviteInfo.getType() == null || inviteInfo.getStream() == null
|| inviteInfo.getType() == null
|| inviteInfo.getStream() == null
) { ) {
return; return;
} }
@ -118,7 +116,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
} }
String key = VideoManagerConstants.INVITE_PREFIX + String key = VideoManagerConstants.INVITE_PREFIX +
":" + inviteInfoForUpdate.getType() + ":" + inviteInfoForUpdate.getType() +
":" + inviteInfoForUpdate.getDeviceId() +
":" + inviteInfoForUpdate.getChannelId() + ":" + inviteInfoForUpdate.getChannelId() +
":" + inviteInfoForUpdate.getStream()+ ":" + inviteInfoForUpdate.getStream()+
":" + inviteInfoForUpdate.getSsrcInfo().getSsrc(); ":" + inviteInfoForUpdate.getSsrcInfo().getSsrc();
@ -139,7 +136,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
removeInviteInfo(inviteInfoInDb); removeInviteInfo(inviteInfoInDb);
String key = VideoManagerConstants.INVITE_PREFIX + String key = VideoManagerConstants.INVITE_PREFIX +
":" + inviteInfo.getType() + ":" + inviteInfo.getType() +
":" + inviteInfo.getDeviceId() +
":" + inviteInfo.getChannelId() + ":" + inviteInfo.getChannelId() +
":" + stream + ":" + stream +
":" + inviteInfo.getSsrcInfo().getSsrc(); ":" + inviteInfo.getSsrcInfo().getSsrc();
@ -160,7 +156,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
public InviteInfo getInviteInfo(InviteSessionType type, Integer channelId, String stream) { public InviteInfo getInviteInfo(InviteSessionType type, Integer channelId, String stream) {
String key = VideoManagerConstants.INVITE_PREFIX + String key = VideoManagerConstants.INVITE_PREFIX +
":" + (type != null ? type : "*") + ":" + (type != null ? type : "*") +
":*" +
":" + (channelId != null ? channelId : "*") + ":" + (channelId != null ? channelId : "*") +
":" + (stream != null ? stream : "*") ":" + (stream != null ? stream : "*")
+ ":*"; + ":*";
@ -179,7 +174,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
public List<InviteInfo> getAllInviteInfo(InviteSessionType type, Integer channelId, String stream) { public List<InviteInfo> getAllInviteInfo(InviteSessionType type, Integer channelId, String stream) {
String key = VideoManagerConstants.INVITE_PREFIX + String key = VideoManagerConstants.INVITE_PREFIX +
":" + (type != null ? type : "*") + ":" + (type != null ? type : "*") +
":*" +
":" + (channelId != null ? channelId : "*") + ":" + (channelId != null ? channelId : "*") +
":" + (stream != null ? stream : "*") ":" + (stream != null ? stream : "*")
+ ":*"; + ":*";
@ -208,7 +202,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
public void removeInviteInfo(InviteSessionType type, Integer channelId, String stream) { public void removeInviteInfo(InviteSessionType type, Integer channelId, String stream) {
String scanKey = VideoManagerConstants.INVITE_PREFIX + String scanKey = VideoManagerConstants.INVITE_PREFIX +
":" + (type != null ? type : "*") + ":" + (type != null ? type : "*") +
":*" +
":" + (channelId != null ? channelId : "*") + ":" + (channelId != null ? channelId : "*") +
":" + (stream != null ? stream : "*") + ":" + (stream != null ? stream : "*") +
":*"; ":*";
@ -267,7 +260,7 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
@Override @Override
public int getStreamInfoCount(String mediaServerId) { public int getStreamInfoCount(String mediaServerId) {
int count = 0; int count = 0;
String key = VideoManagerConstants.INVITE_PREFIX + ":*:*:*:*:*"; String key = VideoManagerConstants.INVITE_PREFIX + ":*:*:*:*";
List<Object> scanResult = RedisUtil.scan(redisTemplate, key); List<Object> scanResult = RedisUtil.scan(redisTemplate, key);
if (scanResult.isEmpty()) { if (scanResult.isEmpty()) {
return 0; return 0;
@ -316,7 +309,7 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
@Override @Override
public InviteInfo getInviteInfoBySSRC(String ssrc) { public InviteInfo getInviteInfoBySSRC(String ssrc) {
String key = VideoManagerConstants.INVITE_PREFIX + ":*:*:*:*:" + ssrc; String key = VideoManagerConstants.INVITE_PREFIX + ":*:*:*:" + ssrc;
List<Object> scanResult = RedisUtil.scan(redisTemplate, key); List<Object> scanResult = RedisUtil.scan(redisTemplate, key);
if (scanResult.size() != 1) { if (scanResult.size() != 1) {
return null; return null;
@ -334,7 +327,6 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
removeInviteInfo(inviteInfoInDb); removeInviteInfo(inviteInfoInDb);
String key = VideoManagerConstants.INVITE_PREFIX + String key = VideoManagerConstants.INVITE_PREFIX +
":" + inviteInfo.getType() + ":" + inviteInfo.getType() +
":" + inviteInfo.getDeviceId() +
":" + inviteInfo.getChannelId() + ":" + inviteInfo.getChannelId() +
":" + inviteInfo.getStream() + ":" + inviteInfo.getStream() +
":" + ssrc; ":" + ssrc;

View File

@ -65,14 +65,14 @@ public class SendRtpServerServiceImpl implements ISendRtpServerService {
@Override @Override
public void update(SendRtpInfo sendRtpItem) { public void update(SendRtpInfo sendRtpItem) {
redisTemplate.opsForValue().set(VideoManagerConstants.SEND_RTP_INFO_CALLID + sendRtpItem.getCallId(), sendRtpItem); redisTemplate.opsForValue().set(VideoManagerConstants.SEND_RTP_INFO_CALLID + sendRtpItem.getCallId(), sendRtpItem);
redisTemplate.opsForValue().set(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpItem.getStream() + ":" + sendRtpItem.getTargetId(), sendRtpItem); redisTemplate.opsForHash().put(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpItem.getStream(), sendRtpItem.getTargetId(), sendRtpItem);
redisTemplate.opsForValue().set(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpItem.getChannelId() + ":" + sendRtpItem.getTargetId(), sendRtpItem); redisTemplate.opsForHash().put(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpItem.getChannelId(), sendRtpItem.getTargetId(), sendRtpItem);
} }
@Override @Override
public SendRtpInfo queryByChannelId(Integer channelId, String targetId) { public SendRtpInfo queryByChannelId(Integer channelId, String targetId) {
String key = VideoManagerConstants.SEND_RTP_INFO_CHANNEL + channelId + ":" + targetId; String key = VideoManagerConstants.SEND_RTP_INFO_CHANNEL + channelId;
return JsonUtil.redisJsonToObject(redisTemplate, key, SendRtpInfo.class); return JsonUtil.redisHashJsonToObject(redisTemplate, key, targetId, SendRtpInfo.class);
} }
@Override @Override
@ -83,19 +83,17 @@ public class SendRtpServerServiceImpl implements ISendRtpServerService {
@Override @Override
public SendRtpInfo queryByStream(String stream, String targetId) { public SendRtpInfo queryByStream(String stream, String targetId) {
String key = VideoManagerConstants.SEND_RTP_INFO_STREAM + stream + ":" + targetId; String key = VideoManagerConstants.SEND_RTP_INFO_STREAM + stream;
return JsonUtil.redisJsonToObject(redisTemplate, key, SendRtpInfo.class); return JsonUtil.redisHashJsonToObject(redisTemplate, key, targetId, SendRtpInfo.class);
} }
@Override @Override
public List<SendRtpInfo> queryByStream(String stream) { public List<SendRtpInfo> queryByStream(String stream) {
String key = VideoManagerConstants.SEND_RTP_INFO_STREAM + stream + ":*"; String key = VideoManagerConstants.SEND_RTP_INFO_STREAM + stream;
List<Object> queryResult = RedisUtil.scan(redisTemplate, key); List<Object> values = redisTemplate.opsForHash().values(key);
List<SendRtpInfo> result= new ArrayList<>(); List<SendRtpInfo> result= new ArrayList<>();
for (Object o : values) {
for (Object o : queryResult) { result.add((SendRtpInfo) o);
String keyItem = (String) o;
result.add((SendRtpInfo) redisTemplate.opsForValue().get(keyItem));
} }
return result; return result;
@ -110,8 +108,16 @@ public class SendRtpServerServiceImpl implements ISendRtpServerService {
return; return;
} }
redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_CALLID + sendRtpInfo.getCallId()); redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_CALLID + sendRtpInfo.getCallId());
redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpInfo.getStream() + ":" + sendRtpInfo.getTargetId()); if (redisTemplate.opsForHash().size(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpInfo.getStream()) == 0) {
redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpInfo.getChannelId() + ":" + sendRtpInfo.getTargetId()); redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpInfo.getStream());
}else {
redisTemplate.opsForHash().delete(VideoManagerConstants.SEND_RTP_INFO_STREAM + sendRtpInfo.getStream(), sendRtpInfo.getTargetId());
}
if (redisTemplate.opsForHash().size(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpInfo.getChannelId()) == 0) {
redisTemplate.delete(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpInfo.getChannelId());
}else {
redisTemplate.opsForHash().delete(VideoManagerConstants.SEND_RTP_INFO_CHANNEL + sendRtpInfo.getChannelId(), sendRtpInfo.getTargetId());
}
} }
@Override @Override
public void deleteByCallId(String callId) { public void deleteByCallId(String callId) {
@ -149,22 +155,18 @@ public class SendRtpServerServiceImpl implements ISendRtpServerService {
@Override @Override
public List<SendRtpInfo> queryByChannelId(int channelId) { public List<SendRtpInfo> queryByChannelId(int channelId) {
String key = VideoManagerConstants.SEND_RTP_INFO_CHANNEL + channelId + ":*"; String key = VideoManagerConstants.SEND_RTP_INFO_CHANNEL + channelId;
List<Object> queryResult = RedisUtil.scan(redisTemplate, key); List<Object> values = redisTemplate.opsForHash().values(key);
List<SendRtpInfo> result= new ArrayList<>(); List<SendRtpInfo> result= new ArrayList<>();
for (Object o : values) {
for (Object o : queryResult) { result.add((SendRtpInfo) o);
String keyItem = (String) o;
result.add((SendRtpInfo) redisTemplate.opsForValue().get(keyItem));
} }
return result; return result;
} }
@Override @Override
public List<SendRtpInfo> queryAll() { public List<SendRtpInfo> queryAll() {
String key = VideoManagerConstants.SEND_RTP_INFO_CALLID String key = VideoManagerConstants.SEND_RTP_INFO_CALLID + ":*";
+ userSetting.getServerId() + ":*";
List<Object> queryResult = RedisUtil.scan(redisTemplate, key); List<Object> queryResult = RedisUtil.scan(redisTemplate, key);
List<SendRtpInfo> result= new ArrayList<>(); List<SendRtpInfo> result= new ArrayList<>();

View File

@ -98,6 +98,7 @@ public interface IRedisCatchStorage {
void updateGpsMsgInfo(GPSMsgInfo gpsMsgInfo); void updateGpsMsgInfo(GPSMsgInfo gpsMsgInfo);
GPSMsgInfo getGpsMsgInfo(String gbId); GPSMsgInfo getGpsMsgInfo(String gbId);
List<GPSMsgInfo> getAllGpsMsgInfo(); List<GPSMsgInfo> getAllGpsMsgInfo();
MediaInfo getStreamInfo(String app, String streamId, String mediaServerId); MediaInfo getStreamInfo(String app, String streamId, String mediaServerId);

View File

@ -31,4 +31,12 @@ public final class JsonUtil {
} }
return clazz.cast(jsonObject); return clazz.cast(jsonObject);
} }
public static <T> T redisHashJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, String objKey, Class<T> clazz) {
Object jsonObject = redisTemplate.opsForHash().get(key, objKey);
if (Objects.isNull(jsonObject)) {
return null;
}
return clazz.cast(jsonObject);
}
} }