im 获取消息数量,增加缓存

This commit is contained in:
Jack 2025-07-30 00:14:02 +08:00
parent 30dc0e976e
commit d3e1e2906c

View File

@ -37,12 +37,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -57,28 +59,23 @@ import java.util.stream.Collectors;
@Service @Service
public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMapper, SnsUserMessage> implements SnsUserMessageService { public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMapper, SnsUserMessage> implements SnsUserMessageService {
private static final Logger logger = LoggerFactory.getLogger(SnsUserMessageServiceImpl.class);
@Value("${mall.socket.url}") @Value("${mall.socket.url}")
private String MALLSUITE_IM_SOCKETURL; private String MALLSUITE_IM_SOCKETURL;
@Autowired @Autowired
private AccountService accountService; private AccountService accountService;
@Autowired @Autowired
private SnsUserMessageMapper snsUserMessageMapper; private SnsUserMessageMapper snsUserMessageMapper;
@Autowired @Autowired
private ShopService shopService; private ShopService shopService;
@Autowired @Autowired
private AccountBaseConfigService accountBaseConfigService; private AccountBaseConfigService accountBaseConfigService;
@Autowired @Autowired
private SnsUserFriendService snsUserFriendService; private SnsUserFriendService snsUserFriendService;
@Autowired @Autowired
private ImService imService; private ImService imService;
@Autowired
private static Logger logger = LoggerFactory.getLogger(SnsUserMessageServiceImpl.class); private RedisTemplate<String, Object> redisTemplate;
@Override @Override
public Map getMsgCount() { public Map getMsgCount() {
@ -89,26 +86,35 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
return rs; return rs;
} }
Integer user_id = user.getId(); Integer userId = user.getId();
String cacheKey = "sns:user:message:unread:" + userId;
// 尝试从Redis缓存中获取数据
Map cachedResult = (Map) redisTemplate.opsForValue().get(cacheKey);
if (cachedResult != null) {
return cachedResult;
}
QueryWrapper<SnsUserMessage> queryWrapper = new QueryWrapper<>(); QueryWrapper<SnsUserMessage> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user_id).eq("message_kind", 2).eq("message_is_read", 0); queryWrapper.eq("user_id", userId).eq("message_kind", 2).eq("message_is_read", 0);
long num = count(queryWrapper); long num = count(queryWrapper);
rs.put("num", num); rs.put("num", num);
// 构造最后一个用户聊天网址 // 构造最后一个用户聊天网址
if (num > 0) { if (num > 0) {
QueryWrapper<SnsUserMessage> query = new QueryWrapper<>(); QueryWrapper<SnsUserMessage> messageQuery = new QueryWrapper<>();
query.orderByAsc("message_is_read").orderByDesc("message_time").eq("user_id", user_id).eq("message_is_read", 0).eq("message_kind", 2); messageQuery.orderByAsc("message_is_read").orderByDesc("message_time")
.eq("user_id", userId).eq("message_is_read", 0).eq("message_kind", 2);
Integer recently_flag = getParameter("recently_flag", Integer.class); Integer recently_flag = getParameter("recently_flag", Integer.class);
if (CheckUtil.isNotEmpty(recently_flag)) { if (CheckUtil.isNotEmpty(recently_flag)) {
long time = new Date().getTime(); long time = new Date().getTime();
query.gt("message_time", (time - 60 * 5)); messageQuery.gt("message_time", (time - 60 * 5));
} }
SnsUserMessage userMessage = findOne(queryWrapper); // 修复bug使用正确的查询条件messageQuery而不是queryWrapper
SnsUserMessage userMessage = findOne(messageQuery);
Map msg_row = Convert.toMap(String.class, Object.class, userMessage); Map msg_row = Convert.toMap(String.class, Object.class, userMessage);
if (ObjectUtil.isNotNull(msg_row)) { if (ObjectUtil.isNotNull(msg_row)) {
@ -116,6 +122,10 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
msg_row.put("puid", ""); // todo Zero_Model::getPlantformUid(msg_row['user_other_id']); msg_row.put("puid", ""); // todo Zero_Model::getPlantformUid(msg_row['user_other_id']);
} }
} }
// 将结果缓存到Redis缓存10秒
redisTemplate.opsForValue().set(cacheKey, rs, 45, TimeUnit.SECONDS);
return rs; return rs;
} }
@ -456,7 +466,7 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
user_other_info.put("puid", getPlantformUid(user_other_id)); user_other_info.put("puid", getPlantformUid(user_other_id));
user_other_info.put("suid", service_user_id); user_other_info.put("suid", service_user_id);
Long chat_item_id = getParameter("chat_item_id", 0l); Long chat_item_id = getParameter("chat_item_id", 0L);
if (CheckUtil.isNotEmpty(chat_item_id)) { if (CheckUtil.isNotEmpty(chat_item_id)) {
data.put("chat_item_row", shopService.getProductItemOne(chat_item_id)); data.put("chat_item_row", shopService.getProductItemOne(chat_item_id));
} }
@ -468,7 +478,7 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
snsUserMessage.setMessage_is_read(1); snsUserMessage.setMessage_is_read(1);
edit(snsUserMessage, messageQueryWrapper); edit(snsUserMessage, messageQueryWrapper);
} else { } else {
List<Map> user_rows = accountService.getUser(Arrays.asList(user_other_id)); List<Map> user_rows = accountService.getUser(Collections.singletonList(user_other_id));
if (CollUtil.isNotEmpty(user_rows)) { if (CollUtil.isNotEmpty(user_rows)) {
Map user_other_info = user_rows.get(0); Map user_other_info = user_rows.get(0);
@ -802,10 +812,10 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
other.setMessage_time(now.getTime()); other.setMessage_time(now.getTime());
if (!saveOrUpdate(data)) { if (!saveOrUpdate(data)) {
throw new ApiException(I18nUtil._("保存发件箱失败data=") + data.toString()); throw new ApiException(I18nUtil._("保存发件箱失败data=") + data);
} }
if (!saveOrUpdate(other)) { if (!saveOrUpdate(other)) {
throw new ApiException(I18nUtil._("保存收件箱失败data=") + other.toString()); throw new ApiException(I18nUtil._("保存收件箱失败data=") + other);
} }
result.put("message_id", data.getMessage_id()); result.put("message_id", data.getMessage_id());
result.put("message_other_id", other.getMessage_id()); result.put("message_other_id", other.getMessage_id());