优化代码

main
gzydong 2021-07-28 23:42:21 +08:00
parent 40103cd99c
commit 1fea98efb8
13 changed files with 88 additions and 33 deletions

46
app/Cache/GroupCache.php Normal file
View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Cache;
use App\Cache\Repository\StringRedis;
use App\Model\Group\Group;
use App\Traits\StaticInstance;
class GroupCache extends StringRedis
{
use StaticInstance;
protected $name = 'group-cache';
/**
* 更新缓存
*
* @param int $group_id
* @return array
*/
public function updateCache(int $group_id)
{
$group = Group::where('id', $group_id)->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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -121,7 +121,7 @@ class ContactApplyService
if (!$result) return false;
// 做聊天记录的推送
// todo 做聊天记录的推送
return true;
}

View File

@ -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;

View File

@ -3,7 +3,6 @@
namespace App\Service\Group;
use Exception;
use App\Model\Group\Group;
use App\Model\Group\GroupNotice;
use App\Service\BaseService;

View File

@ -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;

View File

@ -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'] : ''
])
];

View File

@ -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 推送消息

View File

@ -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;

View File

@ -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;