From b927f92ef198aa5c707730523c4bde98f4919bd3 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Fri, 18 Jun 2021 17:19:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BE=BF=E6=8D=B7=E7=9A=84ca?= =?UTF-8?q?che=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/CatchModelCollection.php | 8 +++--- extend/catcher/Utils.php | 37 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/extend/catcher/CatchModelCollection.php b/extend/catcher/CatchModelCollection.php index 0c9b627..381c179 100644 --- a/extend/catcher/CatchModelCollection.php +++ b/extend/catcher/CatchModelCollection.php @@ -20,7 +20,7 @@ class CatchModelCollection extends Collection * @param string $children * @return array */ - public function toTree($pid = 0, $pidField = 'parent_id', $children = 'children'): array + public function toTree(int $pid = 0, string $pidField = 'parent_id', string $children = 'children'): array { $pk = 'id'; @@ -39,10 +39,10 @@ class CatchModelCollection extends Collection * @param $header * @param string $path * @param string $disk - * @throws \PhpOffice\PhpSpreadsheet\Exception * @return mixed|string[] + *@throws \PhpOffice\PhpSpreadsheet\Exception */ - public function export($header, $path = '', $disk = 'local'): array + public function export($header, string $path = '', string $disk = 'local'): array { $excel = new Class($header, $this->items) implements ExcelContract { @@ -101,7 +101,7 @@ class CatchModelCollection extends Collection * @param string $column * @return array */ - public function getAllChildrenIds(array $ids, $parentFields = 'parent_id', $column = 'id'): array + public function getAllChildrenIds(array $ids, string $parentFields = 'parent_id', string $column = 'id'): array { array_walk($ids, function (&$item){ $item = intval($item); diff --git a/extend/catcher/Utils.php b/extend/catcher/Utils.php index ee415b8..6d7e712 100644 --- a/extend/catcher/Utils.php +++ b/extend/catcher/Utils.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace catcher; use catchAdmin\system\model\Config; +use think\facade\Cache; use think\facade\Db; use think\helper\Str; @@ -38,8 +39,6 @@ class Utils { $search = []; - // $range = array_merge(['created_at' => ['start_at', 'end_at']], $range); - if (!empty($range)) { foreach ($range as $field => $rangeField) { if (count($rangeField) === 1) { @@ -65,7 +64,7 @@ class Utils * @param string $primaryKey * @return void */ - public static function importTreeData($data, $table, $pid = 'parent_id',$primaryKey = 'id') + public static function importTreeData($data, $table, string $pid = 'parent_id', string $primaryKey = 'id') { foreach ($data as $value) { if (isset($value[$primaryKey])) { @@ -106,7 +105,7 @@ class Utils * @param $rule * @return array */ - public static function parseRule($rule) + public static function parseRule($rule): array { [$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule); @@ -174,7 +173,7 @@ class Utils * @param string $table * @return string */ - public static function tableWithPrefix(string $table) + public static function tableWithPrefix(string $table): string { return Str::contains($table, self::tablePrefix()) ? $table : self::tablePrefix() . $table; @@ -186,7 +185,7 @@ class Utils * @time 2020年07月04日 * @return bool */ - public static function isSuperAdmin() + public static function isSuperAdmin(): bool { return request()->user()->id == config('catch.permissions.super_admin_id'); } @@ -210,7 +209,7 @@ class Utils * @time 2020年09月08日 * @return string */ - public static function publicPath($path = '') + public static function publicPath(string $path = ''): string { return root_path($path ? 'public/'. $path : 'public'); } @@ -293,4 +292,28 @@ class Utils } } } + + /** + * 缓存操作 + * + * @time 2021年06月18日 + * @param string $key + * @param \Closure $callable + * @param int $ttl + * @param string $store + * @return mixed + *@throws \Psr\SimpleCache\InvalidArgumentException + */ + public static function cache(string $key, \Closure $callable, int $ttl = 0, string $store = 'redis') + { + if (Cache::store($store)->has($key)) { + return Cache::store($store)->get($store); + } + + $cache = $callable(); + + Cache::store($store)->set($key, $cache, $ttl); + + return $cache; + } }