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); } }