缓存问题
This commit is contained in:
@@ -7,7 +7,6 @@ use app\chukebao\model\FriendSettings;
|
|||||||
use library\ResponseHelper;
|
use library\ResponseHelper;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\facade\Env;
|
use think\facade\Env;
|
||||||
use think\facade\Cache;
|
|
||||||
use app\common\service\AuthService;
|
use app\common\service\AuthService;
|
||||||
|
|
||||||
class MessageController extends BaseController
|
class MessageController extends BaseController
|
||||||
@@ -31,48 +30,22 @@ class MessageController extends BaseController
|
|||||||
return ResponseHelper::error('请先登录');
|
return ResponseHelper::error('请先登录');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存键
|
// 直接查询好友ID列表
|
||||||
$cacheKeyFriends = 'message_list_friends_' . $accountId;
|
|
||||||
$cacheKeyChatrooms = 'message_list_chatrooms_' . $accountId;
|
|
||||||
$cacheKeyMessages = 'message_list_messages_' . $accountId;
|
|
||||||
|
|
||||||
// 缓存时间:好友和群聊信息5分钟,消息列表30秒
|
|
||||||
$cacheTimeFriends = 300; // 5分钟
|
|
||||||
$cacheTimeChatrooms = 300; // 5分钟
|
|
||||||
$cacheTimeMessages = 30; // 30秒
|
|
||||||
|
|
||||||
// 从缓存获取好友ID列表
|
|
||||||
$friendIds = Cache::get($cacheKeyFriends . '_ids');
|
|
||||||
if ($friendIds === false) {
|
|
||||||
$ids = Db::table('s2_wechat_friend')
|
$ids = Db::table('s2_wechat_friend')
|
||||||
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
||||||
->column('id');
|
->column('id');
|
||||||
$friendIds = empty($ids) ? [0] : $ids; // 避免 IN 查询为空
|
$friendIds = empty($ids) ? [0] : $ids; // 避免 IN 查询为空
|
||||||
Cache::set($cacheKeyFriends . '_ids', $friendIds, $cacheTimeFriends);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从缓存获取好友信息
|
// 直接查询好友信息
|
||||||
$friends = Cache::get($cacheKeyFriends);
|
|
||||||
if ($friends === false) {
|
|
||||||
$friends = Db::table('s2_wechat_friend')
|
$friends = Db::table('s2_wechat_friend')
|
||||||
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
||||||
->column('id,nickname,avatar,conRemark,labels,groupId,wechatAccountId,wechatId,extendFields,phone,region,isTop');
|
->column('id,nickname,avatar,conRemark,labels,groupId,wechatAccountId,wechatId,extendFields,phone,region,isTop');
|
||||||
Cache::set($cacheKeyFriends, $friends, $cacheTimeFriends);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从缓存获取群聊信息
|
// 直接查询群聊信息
|
||||||
$chatrooms = Cache::get($cacheKeyChatrooms);
|
|
||||||
if ($chatrooms === false) {
|
|
||||||
$chatrooms = Db::table('s2_wechat_chatroom')
|
$chatrooms = Db::table('s2_wechat_chatroom')
|
||||||
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
||||||
->column('id,nickname,chatroomAvatar,chatroomId,isTop');
|
->column('id,nickname,chatroomAvatar,chatroomId,isTop');
|
||||||
Cache::set($cacheKeyChatrooms, $chatrooms, $cacheTimeChatrooms);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 优化:分别查询群聊和好友的最新消息,使用缓存
|
|
||||||
// 从缓存获取最新消息列表(短时间缓存,保证消息实时性)
|
|
||||||
$allMessages = Cache::get($cacheKeyMessages);
|
|
||||||
if ($allMessages === false) {
|
|
||||||
// 获取群聊ID列表
|
// 获取群聊ID列表
|
||||||
$chatroomIds = array_keys($chatrooms);
|
$chatroomIds = array_keys($chatrooms);
|
||||||
if (empty($chatroomIds)) {
|
if (empty($chatroomIds)) {
|
||||||
@@ -127,10 +100,6 @@ class MessageController extends BaseController
|
|||||||
return $b['wechatTime'] <=> $a['wechatTime'];
|
return $b['wechatTime'] <=> $a['wechatTime'];
|
||||||
});
|
});
|
||||||
|
|
||||||
// 存入缓存
|
|
||||||
Cache::set($cacheKeyMessages, $allMessages, $cacheTimeMessages);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 计算总数
|
// 计算总数
|
||||||
$totalCount = count($allMessages);
|
$totalCount = count($allMessages);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user