ConfigService.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Modules\Api\Services;
  3. use App\Constants\RedisKeyConstant;
  4. use App\Models\ConfigModel;
  5. use Illuminate\Support\Facades\Cache;
  6. class ConfigService
  7. {
  8. /**
  9. * 获取缓存配置
  10. *
  11. * @return mixed
  12. */
  13. public function getConfig()
  14. {
  15. $config = Cache::remember(RedisKeyConstant::CONFIG, 60 * 60 * 24, function () {
  16. return ConfigModel::query()->get(['name', 'key', 'value'])->keyBy('key')->toArray();
  17. });
  18. return $config;
  19. }
  20. /**
  21. * 获取单个缓存
  22. *
  23. * @param string $key
  24. * @return mixed
  25. */
  26. public function getValue(string $key)
  27. {
  28. $keyList = $this->getConfig();
  29. if (isset($keyList[$key])) {
  30. return $keyList[$key]['value'];
  31. } else {
  32. $cacheKey = 'config_'. $key;
  33. $value = Cache::get($cacheKey);
  34. if (empty($value)) {
  35. $confInfo = ConfigModel::query()->where('key', $key)->first('value');
  36. Cache::put($key, $confInfo['value']);
  37. return $confInfo['value'];
  38. }
  39. return $value;
  40. }
  41. }
  42. }