refactor: implement centralized cache key management and add utility traits for vCenter SSH operations.

This commit is contained in:
pbao9
2026-05-16 16:52:21 +07:00
parent 06dbd54ecd
commit 8a115c5175
36 changed files with 1694 additions and 517 deletions
@@ -3,7 +3,6 @@
namespace App\Repositories\Addon;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
class AddonCachedRepository extends BaseCachedRepository implements AddonRepositoryInterface
{
@@ -12,10 +11,12 @@ class AddonCachedRepository extends BaseCachedRepository implements AddonReposit
) {
parent::__construct();
}
public function getModel()
{
return $this->addonRepository->getModel();
}
public function filter(
array $data = [],
array $relationships = [],
@@ -23,24 +24,13 @@ class AddonCachedRepository extends BaseCachedRepository implements AddonReposit
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->addonRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
if ($returnQuery || !empty($data['paginate'])) {
return $this->addonRepository->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->addonRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
return $this->rememberFilter(
compact('data', 'relationships', 'withCount', 'select'),
fn () => $this->addonRepository->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
}
+53 -41
View File
@@ -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);
}
}
@@ -3,7 +3,7 @@
namespace App\Repositories\Customer;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
use App\Support\Cache\CacheKey;
class CustomerCachedRepository extends BaseCachedRepository implements CustomerRepositoryInterface
{
@@ -31,39 +31,22 @@ class CustomerCachedRepository extends BaseCachedRepository implements CustomerR
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->customerRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
return $this->rememberFilter(
compact('data', 'relationships', 'withCount', 'select'),
fn() => $this->customerRepository->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
'id' => $id,
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships, $withCount, $withSum) {
$cacheKey = CacheKey::build($this->getModuleName(), 'find-with-relationships', ['id' => $id]);
return $this->cache->remember($cacheKey, function () use ($id, $relationships, $withCount, $withSum) {
return $this->customerRepository->findWithRelationships($id, $relationships, $withCount, $withSum);
});
}
public function findBy(string $key = '', string $value = '')
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findBy' => [
'key' => $key,
'value' => $value
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
$cacheKey = CacheKey::build($this->getModuleName(), 'find-by', ['key' => $key, 'value' => $value]);
return $this->cache->remember($cacheKey, function () use ($key, $value) {
return $this->customerRepository->findBy($key, $value);
});
}
@@ -3,7 +3,7 @@
namespace App\Repositories\Discount;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
use App\Support\Cache\CacheKey;
class DiscountCachedRepository extends BaseCachedRepository implements DiscountRepositoryInterface
{
@@ -18,32 +18,19 @@ class DiscountCachedRepository extends BaseCachedRepository implements DiscountR
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->discountRepository->filter($data, $relationships, $withCount, $select);
});
return $this->rememberFilter(
compact('data', 'relationships', 'withCount', 'select'),
fn() => $this->discountRepository->filter($data, $relationships, $withCount, $select)
);
}
public function findBy(string $key = '', string $value = '')
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findBy' => [
'key' => $key,
'value' => $value
]
])));
if (empty($value) || empty($key)) {
return false;
}
return $this->model->where($key, $value)->first();
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
return $this->discountRepository->findBy($key, $value);
$cacheKey = CacheKey::build($this->getModuleName(), 'find-by', ['key' => $key, 'value' => $value]);
return $this->cache->remember($cacheKey, function () use ($key, $value) {
return $this->model->where($key, $value)->first();
});
}
}
@@ -3,7 +3,7 @@
namespace App\Repositories\Group;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
use App\Support\Cache\CacheKey;
class GroupCachedRepository extends BaseCachedRepository implements GroupRepositoryInterface
{
@@ -21,28 +21,18 @@ class GroupCachedRepository extends BaseCachedRepository implements GroupReposit
public function getProduct(int $group_id = 0, int $type_id = 0)
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'type_id' => $type_id,
'group_id' => $group_id
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($type_id, $group_id) {
$cacheKey = CacheKey::build($this->getModuleName(), 'product', ['group_id' => $group_id, 'type_id' => $type_id]);
return $this->cache->remember($cacheKey, function () use ($type_id, $group_id) {
return $this->groupRepository->getProduct($group_id, $type_id);
});
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->groupRepository->filter($data, $relationships, $withCount, $select);
});
return $this->rememberFilter(
compact('data', 'relationships', 'withCount', 'select'),
fn() => $this->groupRepository->filter($data, $relationships, $withCount, $select)
);
}
public function findBySlugWithProduct(string $slug)
@@ -2,13 +2,18 @@
namespace App\Repositories\Whois;
use Illuminate\Support\Facades\Cache;
use App\Services\Cache\CacheServiceInterface;
use App\Support\Cache\CacheKey;
class CachedWhoisRepository implements WhoisRepositoryInterface
{
protected string $cachePrefix = 'whois';
protected int $cacheTtl = 3600; // 1 hour
protected CacheServiceInterface $cache;
public function __construct(CacheServiceInterface $cache)
{
$this->cache = $cache;
}
/**
* Get domain information from cache
*
@@ -17,10 +22,10 @@ class CachedWhoisRepository implements WhoisRepositoryInterface
*/
public function getDomainInfo(string $domain): ?array
{
$cacheKey = $this->getCacheKey($domain);
return Cache::get($cacheKey);
$cacheKey = CacheKey::build('whois', 'domain', ['domain' => strtolower($domain)]);
return $this->cache->get($cacheKey);
}
/**
* Cache domain information
*
@@ -30,10 +35,11 @@ class CachedWhoisRepository implements WhoisRepositoryInterface
*/
public function cacheDomainInfo(string $domain, array $data): bool
{
$cacheKey = $this->getCacheKey($domain);
return Cache::put($cacheKey, $data, $this->cacheTtl);
$cacheKey = CacheKey::build('whois', 'domain', ['domain' => strtolower($domain)]);
$this->cache->put($cacheKey, $data);
return true;
}
/**
* Clear domain cache
*
@@ -42,10 +48,11 @@ class CachedWhoisRepository implements WhoisRepositoryInterface
*/
public function clearDomainCache(string $domain): bool
{
$cacheKey = $this->getCacheKey($domain);
return Cache::forget($cacheKey);
$cacheKey = CacheKey::build('whois', 'domain', ['domain' => strtolower($domain)]);
$this->cache->forget($cacheKey);
return true;
}
/**
* Clear all whois cache
*
@@ -53,17 +60,7 @@ class CachedWhoisRepository implements WhoisRepositoryInterface
*/
public function clearAllCache(): bool
{
return Cache::flush();
$this->cache->clearByModule('whois');
return true;
}
/**
* Generate cache key for domain
*
* @param string $domain
* @return string
*/
protected function getCacheKey(string $domain): string
{
return "{$this->cachePrefix}.domain." . strtolower($domain);
}
}
}
@@ -3,7 +3,7 @@
namespace App\Repositories\vCenter;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
use App\Support\Cache\CacheKey;
class vCenterCachedRepository extends BaseCachedRepository implements vCenterRepositoryInterface
{
@@ -31,26 +31,15 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->vCenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
return $this->rememberFilter(
compact('data', 'relationships', 'withCount', 'select'),
fn() => $this->vCenterRepository->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findByName(string $name = '')
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findByName' => [
'name' => $name
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($name) {
$cacheKey = CacheKey::build($this->getModuleName(), 'find-by-name', ['name' => $name]);
return $this->cache->remember($cacheKey, function () use ($name) {
return $this->vCenterRepository->findByName($name);
});
}