diff --git a/serverlogic/src/main/java/com/ljsd/jieling/chat/messge/MessageCache.java b/serverlogic/src/main/java/com/ljsd/jieling/chat/messge/MessageCache.java index 5ce3a4a14..b986fc791 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/chat/messge/MessageCache.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/chat/messge/MessageCache.java @@ -39,41 +39,53 @@ public class MessageCache { RedisUtil.getInstence().hset(key,String.valueOf(index),gson.toJson(chatInfo)); } - public static List getCrossMsg(long messageId, String group) { - String key = RedisKey.CHAT_CROSS_INFO_CACHE + group; - Map rawMap = RedisUtil.getInstence().hmget(key); - - if (rawMap == null || rawMap.isEmpty()) { - return Collections.emptyList(); - } + public static List getCrossMsg(long messageId,String group){ + String key = RedisKey.CHAT_CROSS_INFO_CACHE+group; + Map map = new HashMap<>(); + Map collect = RedisUtil.getInstence().hmget(key); + collect.entrySet().forEach(o -> map.put(Integer.parseInt(o.getKey().toString()), + gson.fromJson(o.getValue().toString(),ChatProto.ChatInfo.class))); List objs = new ArrayList<>(); Object obj = RedisUtil.getInstence().get(RedisKey.CHAT_CROSS_MSG_ID + group); - - if (obj == null) { + if(obj==null){ return objs; } int writeIndex = Integer.parseInt(String.valueOf(obj)); - int nextIndex = writeIndex % capuatiy; // Assuming capacity is a static value or defined elsewhere + Integer nextIndex = writeIndex%capuatiy; + + if(nextIndex==-1){ + nextIndex = capuatiy - 1; + } + if(collect.size()<=0){ + return objs; + } boolean needCompareTime = (messageId == 0); - while (rawMap.containsKey(nextIndex)) { - String rawValue = String.valueOf(rawMap.get(nextIndex)); - ChatProto.ChatInfo chatInfo = gson.fromJson(rawValue, ChatProto.ChatInfo.class); + while(map.containsKey(nextIndex)){ - if (!needCompareTime && messageId >= chatInfo.getMessageId()) { + ChatProto.ChatInfo o = map.get(nextIndex); + if(!needCompareTime){ + //比较消息id + if(messageId>=o.getMessageId()){ + break; + } + + } + objs.add(0, o); + if(objs.size()>perCaught){ break; } - - objs.add(0, chatInfo); - - if (objs.size() > perCaught) { - break; + nextIndex--; + if(nextIndex == -1){ + nextIndex = capuatiy - 1; } - - nextIndex = (nextIndex - 1 + capuatiy) % capuatiy; } - return objs.size() > perCaught ? objs.subList(0, perCaught) : objs; + if(!needCompareTime && objs.size()>perCaught){ + return objs.subList(0,perCaught); + } + + return objs; } }