发布推送
This commit is contained in:
71
app/Cache/Base/BaseCache.php
Normal file
71
app/Cache/Base/BaseCache.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cache\Base;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
//用户缓存用户基础信息(结构自己组装或等于数据表)primary_key为主键
|
||||
abstract class BaseCache
|
||||
{
|
||||
const CACHE_TTL = 60 * 60 * 24 * 3; //缓存时间 3天
|
||||
|
||||
public string $primary_prefix; //前缀
|
||||
public string $primary_key; //值
|
||||
public string $primary_key_column; //值标识
|
||||
|
||||
function setPrimaryKey($primary_key): void
|
||||
{
|
||||
$this->primary_key = $primary_key;
|
||||
}
|
||||
|
||||
function getPrimaryKey($primary_key = null): ?string
|
||||
{
|
||||
if (empty($primary_key) && empty($this->primary_key)) return null;
|
||||
if (empty($primary_key)) $primary_key = $this->primary_key;
|
||||
return $this->primary_prefix . $primary_key;
|
||||
}
|
||||
|
||||
function loadToCache(): bool
|
||||
{
|
||||
$sCacheKey = $this->getPrimaryKey();
|
||||
$aData = $this->loadData();
|
||||
if (empty($aData)) return false;
|
||||
return Cache::put($sCacheKey, serialize($aData), self::CACHE_TTL);
|
||||
}
|
||||
|
||||
abstract function loadData(): array|null;
|
||||
|
||||
function getCacheData($primary_key = null): array|null
|
||||
{
|
||||
if ($primary_key === null) $primary_key = $this->primary_key;
|
||||
if (empty($primary_key)) return [];
|
||||
$sCacheKey = $this->getPrimaryKey($primary_key);
|
||||
$sData = Cache::get($sCacheKey);
|
||||
if (empty($sData)) {
|
||||
$this->primary_key = $primary_key;
|
||||
if($this->loadToCache()) $sData = Cache::get($sCacheKey);
|
||||
}
|
||||
if (empty($sData)) return null;
|
||||
return unserialize($sData);
|
||||
}
|
||||
|
||||
//根据primary_key获取缓存数据单个值,不指定key就默认获取所有
|
||||
function getCacheDataByKey($primary_key = null, $key = null): string|array|bool|null
|
||||
{
|
||||
if ($primary_key === null) $primary_key = $this->primary_key;
|
||||
if (empty($primary_key)) return false;
|
||||
$aData = $this->getCacheData($primary_key);
|
||||
if(empty($aData)) return null;
|
||||
if (empty($key)) return $aData;
|
||||
return $aData[$key] ?? '';
|
||||
}
|
||||
|
||||
function delCacheData($primary_key = null): bool
|
||||
{
|
||||
if ($primary_key === null) $primary_key = $this->primary_key;
|
||||
if (empty($primary_key)) return false;
|
||||
$sCacheKey = $this->getPrimaryKey($primary_key);
|
||||
return Cache::forget($sCacheKey);
|
||||
}
|
||||
|
||||
}
|
||||
21
app/Cache/Base/StructBaseCache.php
Normal file
21
app/Cache/Base/StructBaseCache.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cache\Base;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
//用户缓存用户基础信息(结构自己组装)uid为主键
|
||||
abstract class StructBaseCache extends BaseCache
|
||||
{
|
||||
public string $primary_key_column = 'uid'; //值标识
|
||||
public array $aCacheKey = [];
|
||||
function loadData(): array|null
|
||||
{
|
||||
$aData = [];
|
||||
foreach ($this->aCacheKey as $key => $value) {
|
||||
if (method_exists($this, $value)) $aData[$key] = $this->$value();
|
||||
}
|
||||
return $aData;
|
||||
}
|
||||
|
||||
}
|
||||
45
app/Cache/Base/TableBaseCache.php
Normal file
45
app/Cache/Base/TableBaseCache.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cache\Base;
|
||||
|
||||
use App\Models\Follow\FollowModel;
|
||||
|
||||
//用户缓存用户基础信息(等于数据表)primary_key为主键
|
||||
abstract class TableBaseCache extends BaseCache
|
||||
{
|
||||
public string $table_class;
|
||||
public string $primary_key_column = 'uid';
|
||||
public array $get_columns = ['*'];
|
||||
public array $get_exclude_columns = [];
|
||||
|
||||
function loadTable(): array|null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
function loadData(): array|null
|
||||
{
|
||||
//默认获取表数据方式
|
||||
if (!empty($table_class)) {
|
||||
$oTable = new $this->table_class();
|
||||
//排除不需要的字段
|
||||
if (!empty($this->get_exclude_columns) && $this->get_columns != ['*']) $this->get_columns = array_diff($this->get_columns, $this->get_exclude_columns);
|
||||
$oData = $oTable->findItemByWhere([$this->primary_key_column => $this->primary_key], $this->get_columns);
|
||||
if($oData->isEmpty()) return null;
|
||||
$aData = $oData->toArray();
|
||||
//排除不需要的字段
|
||||
if (!empty($this->get_exclude_columns) && $this->get_columns == ['*'] && !empty($aData)) {
|
||||
foreach ($this->get_exclude_columns as $sColumn) {
|
||||
unset($aData[$sColumn]);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($aData)) return null;
|
||||
return $aData;
|
||||
} else {
|
||||
return $this->loadTable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
38
app/Cache/Struct/StructUserCommonCacheUid.php
Normal file
38
app/Cache/Struct/StructUserCommonCacheUid.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace App\Cache\Struct;
|
||||
|
||||
use App\Cache\Base\StructBaseCache;
|
||||
use App\Models\Customer\CustomerUserExtendModel;
|
||||
|
||||
//用户缓存用户基础信息(结构自己组装不完全等于数据表)primary_key为主键
|
||||
class StructUserCommonCacheUid extends StructBaseCache
|
||||
{
|
||||
|
||||
public string $primary_prefix = 'StructUserCommonCacheUid:'; //后面跟primary_key
|
||||
|
||||
//缓存元素
|
||||
const USER_UID = 'uid';
|
||||
const USER_IS_ACTIVE = 'user_is_active'; //是否活跃用户
|
||||
|
||||
//生成函数,自动调用生成函数
|
||||
public array $aCacheKey = [
|
||||
self::USER_UID => 'genUserUid',
|
||||
self::USER_IS_ACTIVE => 'genUserIsActive',
|
||||
];
|
||||
|
||||
function genUserUid(): string
|
||||
{
|
||||
return $this->primary_key;
|
||||
}
|
||||
|
||||
function genUserIsActive(): string
|
||||
{
|
||||
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
||||
$oCustomerUserExtend = $oCustomerUserExtendModel->findItem($this->primary_key);
|
||||
if(!$oCustomerUserExtend) return '';
|
||||
return $oCustomerUserExtend->is_active;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
22
app/Cache/Table/TableCustomerUserCache.php
Normal file
22
app/Cache/Table/TableCustomerUserCache.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace App\Cache\Table;
|
||||
|
||||
use App\Cache\Base\TableBaseCache;
|
||||
use App\Models\Customer\CustomerUserModel;
|
||||
|
||||
//用户缓存用户基础信息(结构自己组装不完全等于数据表)uid为主键
|
||||
class TableCustomerUserCache extends TableBaseCache
|
||||
{
|
||||
|
||||
public string $table_class = CustomerUserModel::class;
|
||||
public string $primary_prefix = 'TableCustomerUserCache:'; //后面跟uid
|
||||
|
||||
public string $primary_key_column = 'id';
|
||||
public array $get_exclude_columns = [
|
||||
'password',
|
||||
'google_auth_secret',
|
||||
];
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user