162 lines
4.5 KiB
PHP
162 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis\Api;
|
|
|
|
use App\Cache\Redis\Common\CommonRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Common\SiteCode;
|
|
use App\Constants\RedisCode;
|
|
use App\Model\Site;
|
|
use App\Service\ServiceTrait\Common\OssTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class SiteCache
|
|
{
|
|
use OssTrait;
|
|
|
|
/**
|
|
* @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,RedisCode::SYSTEM_DB) && $this->redis->exists($LngLatKey,RedisCode::SYSTEM_DB)) 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();
|
|
//
|
|
// $siteListJsonDecode = json_decode($this->redis->get($siteKey,RedisCode::SYSTEM_DB), true);
|
|
// if (empty($siteListJsonDecode)) return null;
|
|
//
|
|
// $siteList = array_column($siteListJsonDecode, null, 'id');
|
|
//
|
|
// unset($siteListJsonDecode);
|
|
// return $siteList[$siteId] ?? null;
|
|
// }
|
|
|
|
private function setSiteInfo(int $siteId): void
|
|
{
|
|
$siteKey = CommonRedisKey::siteInfoKey($siteId);
|
|
|
|
$info = $this->siteModel->getInfoById($siteId);
|
|
|
|
if (empty($info)) return;
|
|
|
|
if ($info->status == SiteCode::SITE_DISABLE || $info->is_del == SiteCode::SITE_DEL) return;
|
|
|
|
$this->redis->hMset($siteKey, [
|
|
'site_id' => $info->id,
|
|
'name' => $info->name,
|
|
'kitchen_id' => $info->kitchen_id,
|
|
'city_id' => $info->city_id,
|
|
'address' => $info->address,
|
|
'lng' => $info->lng,
|
|
'lat' => $info->lat,
|
|
'url' => $this->getOssObjectById($info->image_id)
|
|
],RedisCode::SYSTEM_DB);
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @return array|false|\Redis|null
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getSiteInfo(int $siteId): false|array|\Redis|null
|
|
{
|
|
$siteKey = CommonRedisKey::siteInfoKey($siteId);
|
|
|
|
if (!$this->redis->exists($siteKey,RedisCode::SYSTEM_DB)) $this->setSiteInfo($siteId);
|
|
|
|
return $this->redis->hGetAll($siteKey,RedisCode::SYSTEM_DB) ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @param string $key
|
|
* @param string $value
|
|
* @return bool|\Redis
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function updateSiteInfo(int $siteId,string $key,string $value)
|
|
{
|
|
$siteKey = CommonRedisKey::siteInfoKey($siteId);
|
|
|
|
if (!$this->redis->exists($siteKey,RedisCode::SYSTEM_DB)) $this->setSiteInfo($siteId);
|
|
|
|
return $this->redis->hMset($siteKey,[
|
|
$key => $value
|
|
],RedisCode::SYSTEM_DB) ?? false;
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function updateBatchValueInfo(int $siteId)
|
|
{
|
|
$siteKey = CommonRedisKey::siteInfoKey($siteId);
|
|
|
|
if ($this->redis->exists($siteKey,RedisCode::SYSTEM_DB)) $this->delSiteInfo($siteId);
|
|
|
|
$this->setSiteInfo($siteId);
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @return bool|int
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function delSiteInfo(int $siteId): bool|int
|
|
{
|
|
$siteKey = CommonRedisKey::siteInfoKey($siteId);
|
|
|
|
if (!$this->redis->exists($siteKey,RedisCode::SYSTEM_DB)) return true;
|
|
|
|
return $this->redis->delete($siteKey,RedisCode::SYSTEM_DB);
|
|
}
|
|
|
|
} |