diff --git a/app/Cache/GroupCache.php b/app/Cache/GroupCache.php new file mode 100644 index 0000000..6f08ee7 --- /dev/null +++ b/app/Cache/GroupCache.php @@ -0,0 +1,46 @@ +first(); + + if (!$group) return []; + + $group = $group->toArray(); + + $this->set(strval($group_id), json_encode($group), 60 * 60 * 1); + + return $group; + } + + /** + * 获取或更新群组缓存 + * + * @param int $group_id 群组ID + * @return array + */ + public function getOrSetCache(int $group_id): array + { + $cache = $this->get(strval($group_id)); + return $cache ? json_decode($cache, true) : $this->updateCache($group_id); + } +} diff --git a/app/Cache/VoteCache.php b/app/Cache/VoteCache.php index 4bba9d2..7db0993 100644 --- a/app/Cache/VoteCache.php +++ b/app/Cache/VoteCache.php @@ -9,17 +9,17 @@ use App\Traits\StaticInstance; class VoteCache extends StringRedis { - protected $name = 'vote-cache'; - use StaticInstance; + protected $name = 'vote-cache'; + /** * 更新投票缓存 * * @param int $vote_id 投票ID * @return array */ - public function updateVoteCache(int $vote_id): array + public function updateCache(int $vote_id): array { $vote_users = TalkRecordsVoteAnswer::where('vote_id', $vote_id)->pluck('user_id')->toArray(); $vote_users = array_unique($vote_users); @@ -34,9 +34,9 @@ class VoteCache extends StringRedis * @param int $vote_id 投票ID * @return array */ - public function getOrSetVoteCache(int $vote_id): array + public function getOrSetCache(int $vote_id): array { $cache = $this->get(strval($vote_id)); - return $cache ? json_decode($cache, true) : $this->updateVoteCache($vote_id); + return $cache ? json_decode($cache, true) : $this->updateCache($vote_id); } } diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 059ef4f..9ec1fea 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -9,6 +9,7 @@ use App\Model\Talk\TalkRecordsVote; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; use Hyperf\DbConnection\Db; +use Hyperf\Utils\Arr; use Psr\Container\ContainerInterface; use Hyperf\Guzzle\ClientFactory; use function _HumbugBox39a196d4601e\RingCentral\Psr7\build_query; @@ -40,24 +41,35 @@ class TestCommand extends HyperfCommand { //VoteStatisticsCache::getInstance()->updateVoteCache(15); - $api = config('juhe_api.ip'); - $options = []; - $client = di()->get(ClientFactory::class)->create($options); - $params = [ - 'ip' => '47.105.180.123', - 'key' => $api['key'], + + $array = [ + 'name' => 'yuandong', + 'sex' => 1, + 'age' => 18 ]; - $address = ''; - $response = $client->get($api['api'] . '?' . http_build_query($params)); - if ($response->getStatusCode() == 200) { - $result = json_decode($response->getBody()->getContents(), true); - if ($result['resultcode'] == 200) { - unset($result['result']['Isp']); - $address = join(' ', $result['result']); - } - } - var_dump($address); + + var_dump(Arr::only($array,['name','age','tt'])); + + //$api = config('juhe_api.ip'); + //$options = []; + //$client = di()->get(ClientFactory::class)->create($options); + //$params = [ + // 'ip' => '47.105.180.123', + // 'key' => $api['key'], + //]; + // + //$address = ''; + //$response = $client->get($api['api'] . '?' . http_build_query($params)); + //if ($response->getStatusCode() == 200) { + // $result = json_decode($response->getBody()->getContents(), true); + // if ($result['resultcode'] == 200) { + // unset($result['result']['Isp']); + // $address = join(' ', $result['result']); + // } + //} + // + //var_dump($address); } } diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index e2c5dc1..be8a454 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -19,7 +19,6 @@ use App\Middleware\JWTAuthMiddleware; use App\Model\Article\ArticleAnnex; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsFile; -use App\Model\Group\Group; use Hyperf\HttpServer\Contract\ResponseInterface; use League\Flysystem\Filesystem; diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index 9d1c33f..c17750b 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -54,7 +54,7 @@ class ArticleService extends BaseService { $items = ArticleTag::where('user_id', $user_id)->orderBy('id', 'desc')->get(['id', 'tag_name', Db::raw('0 as count')])->toArray(); foreach ($items as $k => $item) { - $items[$k]['count'] = (int)Article::where('user_id', $user_id)->whereRaw("FIND_IN_SET({$item['id']},tags_id)")->count(); + $items[$k]['count'] = Article::where('user_id', $user_id)->whereRaw("FIND_IN_SET({$item['id']},tags_id)")->count(); } return $items; diff --git a/app/Service/ContactApplyService.php b/app/Service/ContactApplyService.php index d305f44..a3c6b31 100644 --- a/app/Service/ContactApplyService.php +++ b/app/Service/ContactApplyService.php @@ -121,7 +121,7 @@ class ContactApplyService if (!$result) return false; - // 做聊天记录的推送 + // todo 做聊天记录的推送 return true; } diff --git a/app/Service/EmoticonService.php b/app/Service/EmoticonService.php index 36e2d51..64a1a85 100644 --- a/app/Service/EmoticonService.php +++ b/app/Service/EmoticonService.php @@ -7,7 +7,6 @@ use App\Constants\TalkModeConstant; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsFile; use App\Model\EmoticonItem; -use App\Model\Group\Group; use App\Model\UsersEmoticon; use App\Service\Group\GroupMemberService; diff --git a/app/Service/Group/GroupNoticeService.php b/app/Service/Group/GroupNoticeService.php index f290696..ef45ec5 100644 --- a/app/Service/Group/GroupNoticeService.php +++ b/app/Service/Group/GroupNoticeService.php @@ -3,7 +3,6 @@ namespace App\Service\Group; use Exception; -use App\Model\Group\Group; use App\Model\Group\GroupNotice; use App\Service\BaseService; diff --git a/app/Service/Message/FormatMessageService.php b/app/Service/Message/FormatMessageService.php index d124bd6..5a7319f 100644 --- a/app/Service/Message/FormatMessageService.php +++ b/app/Service/Message/FormatMessageService.php @@ -163,7 +163,7 @@ class FormatMessageService $votes[$row['id']]['answer_option'] = $options; $rows[$k]['vote'] = [ 'statistics' => VoteStatisticsCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), - 'vote_users' => VoteCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), + 'vote_users' => VoteCache::getInstance()->getOrSetCache($votes[$row['id']]['id']), 'detail' => $votes[$row['id']], ]; break; diff --git a/app/Service/Message/SubscribeHandleService.php b/app/Service/Message/SubscribeHandleService.php index bb650ff..549d9de 100644 --- a/app/Service/Message/SubscribeHandleService.php +++ b/app/Service/Message/SubscribeHandleService.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace App\Service\Message; +use App\Cache\GroupCache; use App\Cache\SocketRoom; use App\Constants\TalkEventConstant; use App\Constants\TalkModeConstant; @@ -12,6 +13,7 @@ use App\Model\User; use App\Model\UsersFriendApply; use App\Service\SocketClientService; use App\Service\UserService; +use Hyperf\Utils\Arr; class SubscribeHandleService { @@ -94,7 +96,7 @@ class SubscribeHandleService $fds = array_merge($fds, $this->clientService->findUserFds(intval($uid))); } - $groupInfo = Group::where('id', $receiver_id)->first(['group_name', 'avatar']); + $groupInfo = GroupCache::getInstance()->getOrSetCache($receiver_id); } // 客户端ID去重 @@ -124,8 +126,8 @@ class SubscribeHandleService 'receiver_id' => $receiver_id, 'talk_type' => $talk_type, 'data' => array_merge($message, [ - 'group_name' => $groupInfo ? $groupInfo->group_name : '', - 'group_avatar' => $groupInfo ? $groupInfo->avatar : '' + 'group_name' => $groupInfo ? $groupInfo['group_name'] : '', + 'group_avatar' => $groupInfo ? $groupInfo['avatar'] : '' ]) ]; diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index 3e8fc4a..13ebf58 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -241,7 +241,7 @@ class TalkMessageService } // 更新投票缓存 - VoteCache::getInstance()->updateVoteCache($record->vote_id); + VoteCache::getInstance()->updateCache($record->vote_id); $cache = VoteStatisticsCache::getInstance()->updateVoteCache($record->vote_id); // todo 推送消息 diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index f032aa4..ce05726 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -9,7 +9,6 @@ use App\Event\TalkEvent; use App\Service\Group\GroupMemberService; use App\Service\Message\FormatMessageService; use Exception; -use App\Model\Group\Group; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsFile; diff --git a/app/Support/UserRelation.php b/app/Support/UserRelation.php index 1414845..19ab0f1 100644 --- a/app/Support/UserRelation.php +++ b/app/Support/UserRelation.php @@ -3,7 +3,6 @@ namespace App\Support; use App\Constants\TalkModeConstant; -use App\Model\Group\Group; use App\Service\Group\GroupMemberService; use App\Service\UserFriendService;