| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace Modules\Api\Services;
- use App\Constants\RedisKeyConstant;
- use App\Models\ConfigModel;
- use Illuminate\Support\Facades\Cache;
- class ConfigService
- {
- /**
- * 获取缓存配置
- *
- * @return mixed
- */
- public function getConfig()
- {
- $config = Cache::remember(RedisKeyConstant::CONFIG, 60 * 60 * 24, function () {
- return ConfigModel::query()->get(['name', 'key', 'value'])->keyBy('key')->toArray();
- });
- return $config;
- }
- /**
- * 获取单个缓存
- *
- * @param string $key
- * @return mixed
- */
- public function getValue(string $key)
- {
- $keyList = $this->getConfig();
- if (isset($keyList[$key])) {
- return $keyList[$key]['value'];
- } else {
- $cacheKey = 'config_'. $key;
- $value = Cache::get($cacheKey);
- if (empty($value)) {
- $confInfo = ConfigModel::query()->where('key', $key)->first('value');
- Cache::put($key, $confInfo['value']);
- return $confInfo['value'];
- }
- return $value;
- }
- }
- }
|