ConfigRepoistory.php 5.0 KB

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