This commit is contained in:
f4ilji
2025-01-15 15:31:37 +05:00
parent 45e73e27a0
commit bad611eb84
27 changed files with 815 additions and 124 deletions
+11 -23
View File
@@ -3,18 +3,26 @@
namespace App\Observers;
use App\Models\Post;
use App\Services\App\Cache\PostCacheService;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
class PostObserver
{
protected PostCacheService $postCacheService;
public function __construct()
{
$this->postCacheService = new PostCacheService();
}
/**
* Handle the Post "created" event.
*/
public function saved(Post $post): void
{
$this->postCacheService->clearAllCacheByModel();
}
/**
@@ -22,8 +30,7 @@ class PostObserver
*/
public function updated(Post $post)
{
$this->clearPostCache($post);
$this->cachePost($post); // Кешируем обновленный пост
$this->postCacheService->clearCache($post);
}
/**
@@ -31,7 +38,7 @@ class PostObserver
*/
public function deleted(Post $post)
{
$this->clearPostCache($post);
$this->postCacheService->clearAllCacheByModel();
}
/**
@@ -50,23 +57,4 @@ class PostObserver
//
}
protected function cachePost(Post $post)
{
$cacheKey = 'post_' . md5($post->slug);
$cacheData = [
'post' => $post,
'seo' => $post->seo,
];
Cache::put($cacheKey, $cacheData, now()->addHours(1)); // Кешируем на 1 час
}
/**
* Очистка кеша поста.
*/
protected function clearPostCache(Post $post)
{
$cacheKey = 'post_' . md5($post->slug);
Cache::forget($cacheKey);
}
}