im 获取消息数量,增加缓存
This commit is contained in:
parent
30dc0e976e
commit
d3e1e2906c
@ -37,12 +37,14 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@ -57,28 +59,23 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMapper, SnsUserMessage> implements SnsUserMessageService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SnsUserMessageServiceImpl.class);
|
||||
@Value("${mall.socket.url}")
|
||||
private String MALLSUITE_IM_SOCKETURL;
|
||||
|
||||
@Autowired
|
||||
private AccountService accountService;
|
||||
|
||||
@Autowired
|
||||
private SnsUserMessageMapper snsUserMessageMapper;
|
||||
|
||||
@Autowired
|
||||
private ShopService shopService;
|
||||
|
||||
@Autowired
|
||||
private AccountBaseConfigService accountBaseConfigService;
|
||||
|
||||
@Autowired
|
||||
private SnsUserFriendService snsUserFriendService;
|
||||
|
||||
@Autowired
|
||||
private ImService imService;
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(SnsUserMessageServiceImpl.class);
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Override
|
||||
public Map getMsgCount() {
|
||||
@ -89,26 +86,35 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
|
||||
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.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);
|
||||
|
||||
rs.put("num", num);
|
||||
|
||||
// 构造最后一个用户聊天网址
|
||||
if (num > 0) {
|
||||
QueryWrapper<SnsUserMessage> query = new QueryWrapper<>();
|
||||
query.orderByAsc("message_is_read").orderByDesc("message_time").eq("user_id", user_id).eq("message_is_read", 0).eq("message_kind", 2);
|
||||
QueryWrapper<SnsUserMessage> messageQuery = new QueryWrapper<>();
|
||||
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);
|
||||
if (CheckUtil.isNotEmpty(recently_flag)) {
|
||||
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);
|
||||
|
||||
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']);
|
||||
}
|
||||
}
|
||||
|
||||
// 将结果缓存到Redis,缓存10秒
|
||||
redisTemplate.opsForValue().set(cacheKey, rs, 45, TimeUnit.SECONDS);
|
||||
|
||||
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("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)) {
|
||||
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);
|
||||
edit(snsUserMessage, messageQueryWrapper);
|
||||
} 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)) {
|
||||
Map user_other_info = user_rows.get(0);
|
||||
|
||||
@ -802,10 +812,10 @@ public class SnsUserMessageServiceImpl extends BaseServiceImpl<SnsUserMessageMap
|
||||
other.setMessage_time(now.getTime());
|
||||
|
||||
if (!saveOrUpdate(data)) {
|
||||
throw new ApiException(I18nUtil._("保存发件箱失败!data=") + data.toString());
|
||||
throw new ApiException(I18nUtil._("保存发件箱失败!data=") + data);
|
||||
}
|
||||
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_other_id", other.getMessage_id());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user