userIsFocusOtherUserWithoutCache($sourceUid, $targetUid); } else { $isFocus = make(CacheInterface::class)->rememberForever($cacheKey, function () use ($sourceUid, $targetUid) { return $this->userIsFocusOtherUserWithoutCache($sourceUid, $targetUid); }); } return $isFocus; } /** * A用户是否关注了B用户(没缓存). * @param $sourceUid * @param $targetUid * @return bool */ public function userIsFocusOtherUserWithoutCache($sourceUid, $targetUid) { $isFocus = false; // 默认未关注. $mongoKeyId = md5('favorite_' . (string)$sourceUid . '_' . (string)$targetUid); $favoriteData = [ '_id' => $mongoKeyId, ]; $count = make(UserRelationModel::class)->db() ->countDocuments($favoriteData); $isFocus = boolval($count); return $isFocus; } /** * 用户是否点赞过某一个视频(缓存). * @param $userCode * @param $videoId * @param bool $cache * @return bool */ public function userIsLikeVideo($userCode, $videoId, $cache = true) { $cacheKey = 'video_like:'. (string)$userCode . '_' . (string)$videoId; if (!$cache) { $isLike = $this->userIsLikeVideoWithoutCache($userCode, $videoId); } else { $isLike = make(CacheInterface::class)->rememberForever($cacheKey, function () use ($userCode, $videoId) { return $this->userIsLikeVideoWithoutCache($userCode, $videoId); }); } return $isLike; } /** * 用户是否点赞过某一个视频(没缓存). * @param $userCode * @param $videoId * @return bool */ public function userIsLikeVideoWithoutCache($userCode, $videoId) { $isLike = false; // 默认未点赞. $mongoKeyId = substr(md5($userCode . '_' . strval($videoId)), 8, 16); $data = [ '_id' => $mongoKeyId, ]; $count = make(VideoLikeModel::class)->db() ->countDocuments($data); $isLike = boolval($count); return $isLike; } }