2021-07-18 23:41:25 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Cache;
|
|
|
|
|
|
|
|
use App\Cache\Repository\StringRedis;
|
|
|
|
use App\Model\Talk\TalkRecordsVoteAnswer;
|
|
|
|
use App\Traits\StaticInstance;
|
|
|
|
|
|
|
|
class VoteCache extends StringRedis
|
|
|
|
{
|
|
|
|
use StaticInstance;
|
|
|
|
|
2021-07-28 23:42:21 +08:00
|
|
|
protected $name = 'vote-cache';
|
|
|
|
|
2021-07-18 23:41:25 +08:00
|
|
|
/**
|
|
|
|
* 更新投票缓存
|
|
|
|
*
|
|
|
|
* @param int $vote_id 投票ID
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-07-28 23:42:21 +08:00
|
|
|
public function updateCache(int $vote_id): array
|
2021-07-18 23:41:25 +08:00
|
|
|
{
|
|
|
|
$vote_users = TalkRecordsVoteAnswer::where('vote_id', $vote_id)->pluck('user_id')->toArray();
|
|
|
|
$vote_users = array_unique($vote_users);
|
|
|
|
$this->set(strval($vote_id), json_encode($vote_users), 60 * 60 * 24);
|
|
|
|
|
|
|
|
return $vote_users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取或更新投票缓存
|
|
|
|
*
|
|
|
|
* @param int $vote_id 投票ID
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-07-28 23:42:21 +08:00
|
|
|
public function getOrSetCache(int $vote_id): array
|
2021-07-18 23:41:25 +08:00
|
|
|
{
|
|
|
|
$cache = $this->get(strval($vote_id));
|
2021-07-28 23:42:21 +08:00
|
|
|
return $cache ? json_decode($cache, true) : $this->updateCache($vote_id);
|
2021-07-18 23:41:25 +08:00
|
|
|
}
|
|
|
|
}
|