添加投票缓存

main
gzydong 2021-07-19 23:58:14 +08:00
parent d7d6947be9
commit c9728f1a20
5 changed files with 78 additions and 13 deletions

View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Cache;
use App\Cache\Repository\StringRedis;
use App\Model\Talk\TalkRecordsVote;
use App\Model\Talk\TalkRecordsVoteAnswer;
use App\Traits\StaticInstance;
class VoteStatisticsCache extends StringRedis
{
protected $name = 'vote-statistic-cache';
use StaticInstance;
/**
* 更新投票统计缓存
*
* @param int $vote_id 投票ID
* @return array
*/
public function updateVoteCache(int $vote_id): array
{
$vote = TalkRecordsVote::where('id', $vote_id)->first(['answer_num', 'answered_num', 'answer_option']);
if (!$vote) return [];
$answers = TalkRecordsVoteAnswer::where('vote_id', $vote_id)->pluck('option')->toArray();
$options = array_map(function () {
return 0;
}, $vote->answer_option);
foreach ($answers as $answer) {
$options[$answer]++;
}
$statistics = [
'count' => count($answers),
'options' => $options
];
$this->set(strval($vote_id), json_encode($statistics), 60 * 60 * 24);
return $statistics;
}
/**
* 获取或更新投票统计缓存
*
* @param int $vote_id 投票ID
* @return array
*/
public function getOrSetVoteCache(int $vote_id): array
{
$cache = $this->get(strval($vote_id));
return $cache ? json_decode($cache, true) : $this->updateVoteCache($vote_id);
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Command; namespace App\Command;
use App\Cache\VoteStatisticsCache;
use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVote;
use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command; use Hyperf\Command\Annotation\Command;
@ -35,6 +36,6 @@ class TestCommand extends HyperfCommand
public function handle() public function handle()
{ {
echo chr(65); VoteStatisticsCache::getInstance()->updateVoteCache(15);
} }
} }

View File

@ -226,11 +226,11 @@ class TalkMessageController extends CController
return $this->response->fail('投票失败,请稍后再试!'); return $this->response->fail('投票失败,请稍后再试!');
} }
$isTrue = $this->talkMessageService->handleVote($this->uid(), $params); [$isTrue, $cache] = $this->talkMessageService->handleVote($this->uid(), $params);
if (!$isTrue) return $this->response->fail('投票失败,请稍后再试!'); if (!$isTrue) return $this->response->fail('投票失败,请稍后再试!');
return $this->response->success(); return $this->response->success($cache);
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace App\Service\Message; namespace App\Service\Message;
use App\Cache\VoteCache; use App\Cache\VoteCache;
use App\Cache\VoteStatisticsCache;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsCode;
use App\Model\Talk\TalkRecordsFile; use App\Model\Talk\TalkRecordsFile;
@ -152,10 +153,10 @@ class FormatMessageService
} }
$votes[$row['id']]['answer_option'] = $options; $votes[$row['id']]['answer_option'] = $options;
$rows[$k]['vote'] = [
$rows[$k]['vote'] = [ 'statistics' => VoteStatisticsCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']),
'vote_users' => VoteCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), 'vote_users' => VoteCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']),
'detail' => $votes[$row['id']] 'detail' => $votes[$row['id']],
]; ];
break; break;

View File

@ -4,6 +4,7 @@ namespace App\Service;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\VoteCache; use App\Cache\VoteCache;
use App\Cache\VoteStatisticsCache;
use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageEvent;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
@ -176,7 +177,7 @@ class TalkMessageService
* @param array $params * @param array $params
* @return bool * @return bool
*/ */
public function handleVote(int $user_id, array $params): bool public function handleVote(int $user_id, array $params): array
{ {
$record = TalkRecords::join('talk_records_vote as vote', 'vote.record_id', '=', 'talk_records.id') $record = TalkRecords::join('talk_records_vote as vote', 'vote.record_id', '=', 'talk_records.id')
->where('talk_records.id', $params['record_id']) ->where('talk_records.id', $params['record_id'])
@ -191,15 +192,15 @@ class TalkMessageService
if (!$record) return false; if (!$record) return false;
if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) { if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) {
return false; return [false, []];
} }
if (!UserRelation::isFriendOrGroupMember($user_id, $record->receiver_id, $record->talk_type)) { if (!UserRelation::isFriendOrGroupMember($user_id, $record->receiver_id, $record->talk_type)) {
return false; return [false, []];
} }
if (TalkRecordsVoteAnswer::where('vote_id', $record->vote_id)->where('user_id', $user_id)->exists()) { if (TalkRecordsVoteAnswer::where('vote_id', $record->vote_id)->where('user_id', $user_id)->exists()) {
return false; return [false, []];
} }
$options = $params['options']; $options = $params['options'];
@ -207,7 +208,7 @@ class TalkMessageService
sort($options); sort($options);
foreach ($options as $value) { foreach ($options as $value) {
if (!isset($record->answer_option[$value])) return false; if (!isset($record->answer_option[$value])) return [false, []];
} }
// 单选模式取第一个 // 单选模式取第一个
@ -233,11 +234,15 @@ class TalkMessageService
} }
}); });
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return [false, []];
} }
// 更新投票缓存
VoteCache::getInstance()->updateVoteCache($record->vote_id); VoteCache::getInstance()->updateVoteCache($record->vote_id);
$cache = VoteStatisticsCache::getInstance()->updateVoteCache($record->vote_id);
return true; // todo 推送消息
return [true, $cache];
} }
} }