| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * 配置仓库.
- *
- * @author jim <jim458734573@gmail.com>
- * @date 2020-01-31 10:39:38
- */
- declare(strict_types=1);
- namespace App\Repositories;
- use App\Model\ConfigModel;
- use Hyperf\Database\Model\Builder;
- use KZ2\Repositories\Model\Repository;
- use Psr\SimpleCache\CacheInterface;
- class ConfigRepoistory extends Repository
- {
- public function model()
- {
- return ConfigModel::class;
- }
- /**
- * 根据key取数据.
- * @param array $keys Key键名.
- * @param mixed $sgroup 分组
- * @return \Hyperf\DbConnection\Model\Model
- */
- public function pickByKeys($keys = [], $sgroup = null)
- {
- $result = [];
- $model = new ConfigModel();
- if (!is_null($sgroup)) {
- $model = $model->where('sgroup', $sgroup);
- }
- if (!empty($keys)) {
- if (is_array($keys)) {
- $model = $model->whereIn('skey', $keys);
- } else {
- $model = $model->where('skey', $keys);
- }
- }
- $listObj = $model->orderBy('created_at', 'ASC')->get();
- if (!empty($listObj)) {
- $result = $listObj->mapWithKeys(function ($item) use ($keys, $sgroup) {
- return [$item->skey => $item];
- });
- }
- return !empty($result) ? $result->toArray() : [];
- }
- /**
- * 根据key数据保存.
- * @param array $data 数据.
- * @param string $sgroup 分组.
- * @return bool
- */
- public function saveByKeys(array $data, string $sgroup)
- {
- foreach ($data as $skey => $svalue) {
- $svalue = is_null($svalue) ? '' : $svalue;
- if ($obj = ConfigModel::where('sgroup', $sgroup)->where('skey', $skey)->first()) {
- if ($obj->svalue !== $svalue) {
- // 前后有变化才执行保存.
- $obj->svalue = $svalue;
- $obj->save();
- }
- } else {
- // 没有则创建.
- $setarr = [
- 'name' => $skey,
- 'sgroup' => $sgroup,
- 'skey' => $skey,
- 'svalue' => $svalue,
- 'value_type' => 'string',
- 'tag_name' => 'input',
- ];
- ConfigModel::create($setarr);
- }
- }
- return true;
- }
- /**
- * 更新配置缓存.
- * @param $sgroup
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function updateConfigCache($sgroup)
- {
- $arrConfig = $this->pickByKeys([], $sgroup);
- $cache = make(CacheInterface::class);
- $cacheKey = 'config_'. $sgroup;
- $cache->forever($cacheKey, $arrConfig);
- // 把config清除一下.
- $cache->delete('config');
- return true;
- }
- /**
- * 清理所有缓存.
- * @return bool
- */
- public function clearAllCache()
- {
- $cacheDir = env('SERVER_CACHE_PREFIX', 'c:');
- $cache = make(CacheInterface::class);
- $cache->clear();
- return true;
- }
- /**
- * 从缓存获取单个配置(单个找不到走DB查一次).
- * @param string $skey 键名.
- * @param string $sgroup 分组.
- * @param mixed $default 默认值.
- * @return mixed
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function get(string $skey, string $sgroup, $default = null)
- {
- $cacheKey = 'config_'. $sgroup;
- $cache = make(CacheInterface::class);
- $data = $cache->get($cacheKey);
- if (isset($data[$skey])) {
- return $data[$skey]['svalue'];
- } else {
- $items = $this->pickByKeys([], $sgroup);
- $cache->forever($cacheKey, $items);
- $item = $items[$skey] ?? array();
- if (!is_null($item)) {
- return $item['svalue'];
- } else {
- return $default;
- }
- }
- }
- /**
- * 从缓存配置获取多个配置(根本不走DB).
- * @param array $keys 键名.
- * @param string $sgroup 分组.
- * @param array $default 默认值.
- * @return array
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function gets($keys, string $sgroup, $default = [])
- {
- $cache = make(CacheInterface::class);
- $data = $cache->remember(
- 'config',
- 60 * 60,
- function () use ($keys, $sgroup) {
- return ConfigModel::query()->get();
- }
- );
- if (!is_null($sgroup)) {
- $model = collect($data)->where('sgroup', $sgroup);
- }
- if (empty($keys)) {
- return $model->pluck('svalue', 'skey');
- } else {
- return $model->whereIn('skey', $keys)->pluck('svalue', 'skey');
- }
- }
- /**
- * 获取所有分组.
- * @return Builder[]|\Hyperf\Database\Model\Collection
- */
- public function getAllGroup()
- {
- $items = ConfigModel::query()->distinct('sgroup')->groupBy('sgroup')->get([ 'sgroup']);
- return $items;
- }
- }
|