ConfigRepoistory.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * 配置仓库.
  4. *
  5. * @author nj
  6. * @date 2021-01-31 10:39:38
  7. */
  8. namespace App\Repositories;
  9. use KZ2\Laravel\Repositories\Eloquent\Repository;
  10. use App\Models\Mysql\ConfigModel;
  11. use Cache;
  12. use Illuminate\Database\Eloquent\Model;
  13. class ConfigRepoistory extends Repository
  14. {
  15. public function model()
  16. {
  17. return ConfigModel::class;
  18. }
  19. /**
  20. * 根据key取数据.
  21. * @param array $keys Key键名.
  22. * @param mixed $sgroup 分组
  23. * @return Model
  24. */
  25. public function pickByKeys($keys = [], $sgroup = null)
  26. {
  27. $result = [];
  28. $model = new ConfigModel();
  29. if (!is_null($sgroup)) {
  30. $model = $model->where('sgroup', $sgroup);
  31. }
  32. if (!empty($keys)) {
  33. if (is_array($keys)) {
  34. $model = $model->whereIn('skey', $keys);
  35. } else {
  36. $model = $model->where('skey', $keys);
  37. }
  38. }
  39. $listObj = $model->orderBy('squad', 'DESC')->orderBy('created_at', 'ASC')->get();
  40. if (!empty($listObj)) {
  41. $result = $listObj->mapWithKeys(function ($item) use ($keys, $sgroup) {
  42. return [$item->skey => $item];
  43. });
  44. }
  45. return !empty($result) ? $result->toArray() : [];
  46. }
  47. /**
  48. * 根据key数据保存.
  49. * @param array $data 数据.
  50. * @param string $sgroup 分组.
  51. * @return bool
  52. */
  53. public function saveByKeys(array $data, string $sgroup)
  54. {
  55. foreach ($data as $skey => $svalue) {
  56. $svalue = is_null($svalue) ? '' : $svalue;
  57. if ($svalue) {
  58. $svalue = trim((string)$svalue);
  59. }
  60. if ($obj = ConfigModel::query()->where('sgroup', $sgroup)->where('skey', $skey)->first()) {
  61. if ($obj->svalue !== $svalue) {
  62. // 前后有变化才执行保存.
  63. $obj->svalue = $svalue;
  64. $obj->save();
  65. }
  66. } else {
  67. // 没有则创建.
  68. $setarr = [
  69. 'name' => $skey,
  70. 'sgroup' => $sgroup,
  71. 'skey' => $skey,
  72. 'svalue' => $svalue,
  73. 'value_type' => 'string',
  74. 'tag_name' => 'input',
  75. ];
  76. ConfigModel::query()->create($setarr);
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * 更新配置缓存.
  83. * @param $sgroup
  84. * @return bool
  85. * @throws \Psr\SimpleCache\InvalidArgumentException
  86. */
  87. public function updateConfigCache($sgroup)
  88. {
  89. $arrConfig = $this->pickByKeys([], $sgroup);
  90. /**
  91. * @var Cache $cache
  92. */
  93. $cache = make('cache');
  94. $cacheKey = 'config:'. $sgroup;
  95. $cache->forever($cacheKey, $arrConfig);
  96. // 把config清除一下.
  97. $cache->delete('config');
  98. return true;
  99. }
  100. /**
  101. * 从缓存获取单个配置(单个找不到走DB查一次).
  102. * @param string $skey 键名.
  103. * @param string $sgroup 分组.
  104. * @param mixed $default 默认值.
  105. * @return mixed
  106. * @throws \Psr\SimpleCache\InvalidArgumentException
  107. */
  108. public function get(string $skey, string $sgroup, $default = null)
  109. {
  110. $cacheKey = 'config:'. $sgroup;
  111. /**
  112. * @var Cache $cache
  113. */
  114. $cache = make('cache');
  115. $data = $cache->get($cacheKey);
  116. if (isset($data[$skey])) {
  117. return $data[$skey]['svalue'];
  118. } else {
  119. $items = $this->pickByKeys([], $sgroup);
  120. $cache->forever($cacheKey, $items);
  121. $item = $items[$skey] ?? array();
  122. if ($item) {
  123. return $item['svalue'];
  124. } else {
  125. return $default;
  126. }
  127. }
  128. }
  129. /**
  130. * 从缓存配置获取多个配置(根本不走DB).
  131. * @param array $keys 键名.
  132. * @param string $sgroup 分组.
  133. * @param array $default 默认值.
  134. * @return array
  135. * @throws \Psr\SimpleCache\InvalidArgumentException
  136. */
  137. public function gets($keys, string $sgroup, $default = [])
  138. {
  139. /**
  140. * @var Cache $cache
  141. */
  142. $cache = make('cache');
  143. $cacheKey = 'config:'. $sgroup;
  144. $data = $cache->remember(
  145. $cacheKey,
  146. 60 * 60,
  147. function () {
  148. return ConfigModel::query()->orderBy('squad', 'DESC')->get();
  149. }
  150. );
  151. if (!is_null($sgroup)) {
  152. $data = collect($data)->where('sgroup', $sgroup);
  153. }
  154. if (empty($keys)) {
  155. return $data->pluck('svalue', 'skey');
  156. } else {
  157. return $data->whereIn('skey', $keys)->pluck('svalue', 'skey');
  158. }
  159. }
  160. }