-
redis存放聊天消息设计
在获取聊天记录的业务中,首先,消息的具体内容是用hash结构,存放在redis中的
key是CHAT_MESSAGE_CONTENT_CACHE+{groupId},
hashKey 是messageId,
value是具体的消息内容。如下图所示
在查询历史记录时,则只会根据查询条件,到数据库查询所有满足条件的messageId,然后根据messageId,一个个获取消息的具体内容。获取操作是,先从redis中获取,如果没有,再查询数据库,查询到结果后就再放到redis中。
@Override
public List<ChatMsg> getMessageHistory(Integer count, String groupId, Long endTime) {
List<ChatMsg> chatMsgHistoryList = new ArrayList();
List<String> idList = Lists.newArrayList();
QueryWrapper<ChatMsg> queryWrapper = new QueryWrapper();
queryWrapper.select("message_id")
// .last("limit " + count)