refactor: remove compression middleware from API routes and optimize cache invalidation in BaseCachedRepository.

This commit is contained in:
pbao9
2026-03-16 17:21:21 +07:00
parent b5c81cf23c
commit 50a5bfd530
2 changed files with 11 additions and 2 deletions
-1
View File
@@ -42,7 +42,6 @@ class Kernel extends HttpKernel
'api' => [ 'api' => [
'throttle:api', 'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CompressionMiddleware::class,
], ],
]; ];
+11 -1
View File
@@ -47,6 +47,7 @@ abstract class BaseCachedRepository extends BaseRepository
{ {
$result = parent::update($id, $data); $result = parent::update($id, $data);
$this->clearCache(); $this->clearCache();
Cache::forget($this->getCacheKey($id));
return $result; return $result;
} }
@@ -54,6 +55,7 @@ abstract class BaseCachedRepository extends BaseRepository
{ {
$result = parent::delete($id); $result = parent::delete($id);
$this->clearCache(); $this->clearCache();
Cache::forget($this->getCacheKey($id));
return $result; return $result;
} }
@@ -70,7 +72,15 @@ abstract class BaseCachedRepository extends BaseRepository
} }
public function clearCache() public function clearCache()
{ {
Cache::flush(); // 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)
{ {