新增便捷的cache操作

This commit is contained in:
JaguarJack 2021-06-18 17:19:08 +08:00
parent 99e53be3e4
commit b927f92ef1
2 changed files with 34 additions and 11 deletions

View File

@ -20,7 +20,7 @@ class CatchModelCollection extends Collection
* @param string $children * @param string $children
* @return array * @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'; $pk = 'id';
@ -39,10 +39,10 @@ class CatchModelCollection extends Collection
* @param $header * @param $header
* @param string $path * @param string $path
* @param string $disk * @param string $disk
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @return mixed|string[] * @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 $excel = new Class($header, $this->items) implements ExcelContract
{ {
@ -101,7 +101,7 @@ class CatchModelCollection extends Collection
* @param string $column * @param string $column
* @return array * @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){ array_walk($ids, function (&$item){
$item = intval($item); $item = intval($item);

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace catcher; namespace catcher;
use catchAdmin\system\model\Config; use catchAdmin\system\model\Config;
use think\facade\Cache;
use think\facade\Db; use think\facade\Db;
use think\helper\Str; use think\helper\Str;
@ -38,8 +39,6 @@ class Utils
{ {
$search = []; $search = [];
// $range = array_merge(['created_at' => ['start_at', 'end_at']], $range);
if (!empty($range)) { if (!empty($range)) {
foreach ($range as $field => $rangeField) { foreach ($range as $field => $rangeField) {
if (count($rangeField) === 1) { if (count($rangeField) === 1) {
@ -65,7 +64,7 @@ class Utils
* @param string $primaryKey * @param string $primaryKey
* @return void * @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) { foreach ($data as $value) {
if (isset($value[$primaryKey])) { if (isset($value[$primaryKey])) {
@ -106,7 +105,7 @@ class Utils
* @param $rule * @param $rule
* @return array * @return array
*/ */
public static function parseRule($rule) public static function parseRule($rule): array
{ {
[$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule); [$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule);
@ -174,7 +173,7 @@ class Utils
* @param string $table * @param string $table
* @return string * @return string
*/ */
public static function tableWithPrefix(string $table) public static function tableWithPrefix(string $table): string
{ {
return Str::contains($table, self::tablePrefix()) ? return Str::contains($table, self::tablePrefix()) ?
$table : self::tablePrefix() . $table; $table : self::tablePrefix() . $table;
@ -186,7 +185,7 @@ class Utils
* @time 2020年07月04日 * @time 2020年07月04日
* @return bool * @return bool
*/ */
public static function isSuperAdmin() public static function isSuperAdmin(): bool
{ {
return request()->user()->id == config('catch.permissions.super_admin_id'); return request()->user()->id == config('catch.permissions.super_admin_id');
} }
@ -210,7 +209,7 @@ class Utils
* @time 2020年09月08日 * @time 2020年09月08日
* @return string * @return string
*/ */
public static function publicPath($path = '') public static function publicPath(string $path = ''): string
{ {
return root_path($path ? 'public/'. $path : 'public'); 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;
}
} }