* @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; } }