67 lines
1.6 KiB
PHP
67 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis\Api;
|
|
|
|
use App\Cache\Redis\Common\CommonRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\RedisCode;
|
|
use App\Model\Site;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class SiteCache
|
|
{
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var Site
|
|
*/
|
|
#[Inject]
|
|
protected Site $siteModel;
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function setSiteRedis()
|
|
{
|
|
$siteKey = CommonRedisKey::siteListKey();
|
|
$LngLatKey = CommonRedisKey::siteLngLatKey();
|
|
|
|
if ($this->redis->exists($siteKey) && $this->redis->exists($LngLatKey)) return;
|
|
|
|
$siteList = $this->siteModel->getAllSiteList();
|
|
|
|
$this->redis->delete($siteKey);
|
|
$this->redis->delete($LngLatKey);
|
|
|
|
foreach ($siteList as $site) {
|
|
$this->redis->geoAdd($LngLatKey, $site->lng,$site->lat,$site->id,RedisCode::SYSTEM_DB);
|
|
}
|
|
|
|
$this->redis->set($siteKey,json_encode(array_column($siteList, null, 'id'), JSON_UNESCAPED_UNICODE),RedisCode::SYSTEM_DB);
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @return array|null
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getSiteInfo(int $siteId): array|null
|
|
{
|
|
$this->setSiteRedis();
|
|
|
|
$siteKey = CommonRedisKey::siteListKey();
|
|
|
|
$siteList = json_decode($this->redis->get($siteKey), true);
|
|
|
|
return $siteList[$siteId] ?? null;
|
|
}
|
|
} |