refactor: implement centralized cache key management and add utility traits for vCenter SSH operations.
This commit is contained in:
@@ -2,90 +2,102 @@
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Services\Cache\CacheServiceInterface;
|
||||
use App\Support\Cache\CacheKey;
|
||||
|
||||
abstract class BaseCachedRepository extends BaseRepository
|
||||
{
|
||||
protected $cacheTime = 172800; // Thời gian lưu cache )giây)
|
||||
protected int $cacheTime = 172800; // 48 hours (fallback for raw Cache calls)
|
||||
|
||||
protected CacheServiceInterface $cache;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->cache = app(CacheServiceInterface::class);
|
||||
}
|
||||
|
||||
public function find($id)
|
||||
{
|
||||
$cacheKey = $this->getCacheKey($id);
|
||||
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id) {
|
||||
return parent::find($id);
|
||||
});
|
||||
$key = CacheKey::build($this->getModuleName(), 'find', ['id' => $id]);
|
||||
|
||||
return $this->cache->remember($key, fn () => parent::find($id), CacheServiceInterface::TTL_LONG);
|
||||
}
|
||||
|
||||
public function getAll()
|
||||
{
|
||||
|
||||
$cacheKey = $this->getCacheKey("getAll");
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () {
|
||||
return parent::getAll();
|
||||
});
|
||||
$key = CacheKey::build($this->getModuleName(), 'all');
|
||||
|
||||
return $this->cache->remember($key, fn () => parent::getAll(), CacheServiceInterface::TTL_LONG);
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
$key = CacheKey::build($this->getModuleName(), 'count');
|
||||
|
||||
return $this->cache->remember($key, fn () => parent::getCount(), CacheServiceInterface::TTL_LONG);
|
||||
}
|
||||
|
||||
public function create($data = [])
|
||||
{
|
||||
$result = parent::create($data);
|
||||
$this->clearCache();
|
||||
$this->cache->clearByModule($this->getModuleName());
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function insert(array $data = [])
|
||||
{
|
||||
$result = parent::insert($data);
|
||||
$this->clearCache();
|
||||
$this->cache->clearByModule($this->getModuleName());
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function update(int $id = 0, array $data = [])
|
||||
{
|
||||
$result = parent::update($id, $data);
|
||||
$this->clearCache();
|
||||
Cache::forget($this->getCacheKey($id));
|
||||
$this->cache->clearByModule($this->getModuleName());
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$result = parent::delete($id);
|
||||
$this->clearCache();
|
||||
Cache::forget($this->getCacheKey($id));
|
||||
$this->cache->clearByModule($this->getModuleName());
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function getCacheKey($method)
|
||||
public function clearCache(): void
|
||||
{
|
||||
return sprintf('%s_%s', static::class, $method);
|
||||
$this->cache->clearByModule($this->getModuleName());
|
||||
}
|
||||
public function getCount()
|
||||
{
|
||||
$cacheKey = $this->getCacheKey("getCount");
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () {
|
||||
return parent::getCount();
|
||||
});
|
||||
}
|
||||
public function clearCache()
|
||||
{
|
||||
// Chỉ xóa cache của repository này, không flush toàn bộ cache app
|
||||
$keysToForget = [
|
||||
$this->getCacheKey('getAll'),
|
||||
$this->getCacheKey('getCount'),
|
||||
];
|
||||
|
||||
foreach ($keysToForget as $key) {
|
||||
Cache::forget($key);
|
||||
}
|
||||
}
|
||||
public function clearCacheByKey(string $key)
|
||||
public function clearCacheByKey(string $key): void
|
||||
{
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
$this->cache->forget(CacheKey::build($this->getModuleName(), $key));
|
||||
}
|
||||
|
||||
Cache::forget($cacheKey);
|
||||
// -------------------------------------------------------------------------
|
||||
// Helpers for child repositories
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
protected function rememberFilter(array $params, callable $query): mixed
|
||||
{
|
||||
$key = CacheKey::build($this->getModuleName(), 'filter', [
|
||||
'hash' => md5(serialize($params)),
|
||||
]);
|
||||
|
||||
return $this->cache->remember($key, $query, CacheServiceInterface::TTL_LONG);
|
||||
}
|
||||
|
||||
protected function getModuleName(): string
|
||||
{
|
||||
$shortName = class_basename(static::class);
|
||||
return strtolower(str_replace('CachedRepository', '', $shortName));
|
||||
}
|
||||
|
||||
// Keep for backward compat — child classes that still call getCacheKey() directly.
|
||||
protected function getCacheKey(string $suffix): string
|
||||
{
|
||||
return CacheKey::build($this->getModuleName(), $suffix);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user