Files
hyperf_service/app/Cache/Redis/Common/CityCache.php
2024-10-31 16:52:32 +08:00

56 lines
1.3 KiB
PHP

<?php
namespace App\Cache\Redis\Common;
use App\Cache\Redis\RedisCache;
use App\Model\SystemCityConfig;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CityCache
{
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @var SystemCityConfig
*/
#[Inject]
protected SystemCityConfig $systemCityConfigModel;
/**
* 获取数据
* @param int $deep 0 = 省份 1 = 市 2 = 区 other = all
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function getCityList(int $deep = -1): array
{
$type = match ($deep) {
0 => 'province',
1 => 'city',
2 => 'district',
default => 'all',
};
$key = CommonRedisKey::getSystemRegionList($type);
if ($this->redis->exists($key)) return json_decode($this->redis->get($key), true);
if ($type == 'all') {
$data = $this->systemCityConfigModel->getAll();
} else {
$data = $this->systemCityConfigModel->getListByDeep($deep);
}
$this->redis->set($key, json_encode($data));
return $data;
}
}