master_zzxx
grimm 2024-06-14 16:03:08 +08:00
parent a11b061959
commit b7e05622ca
1 changed files with 34 additions and 22 deletions

View File

@ -39,41 +39,53 @@ public class MessageCache {
RedisUtil.getInstence().hset(key,String.valueOf(index),gson.toJson(chatInfo));
}
public static List<ChatProto.ChatInfo> getCrossMsg(long messageId, String group) {
String key = RedisKey.CHAT_CROSS_INFO_CACHE + group;
Map<Object, Object> rawMap = RedisUtil.getInstence().hmget(key);
if (rawMap == null || rawMap.isEmpty()) {
return Collections.emptyList();
}
public static List<ChatProto.ChatInfo> getCrossMsg(long messageId,String group){
String key = RedisKey.CHAT_CROSS_INFO_CACHE+group;
Map<Integer,ChatProto.ChatInfo> map = new HashMap<>();
Map<Object, Object> 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<ChatProto.ChatInfo> 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;
}
}