This commit is contained in:
f4ilji
2025-01-13 13:56:50 +05:00
parent fab57a3306
commit 6b48c31afd
11 changed files with 115 additions and 62 deletions
+27 -9
View File
@@ -4,6 +4,7 @@ namespace App\Observers;
use App\Models\Post;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
class PostObserver
@@ -13,27 +14,24 @@ class PostObserver
*/
public function saved(Post $post): void
{
$recipient = auth()->user();
Notification::make()
->title('Saved successfully')
->sendToDatabase($recipient);
}
/**
* Handle the Post "updated" event.
*/
public function updated(Post $post): void
public function updated(Post $post)
{
//
$this->clearPostCache($post);
$this->cachePost($post); // Кешируем обновленный пост
}
/**
* Handle the Post "deleted" event.
* Очистка кеша при удалении поста.
*/
public function deleted(Post $post): void
public function deleted(Post $post)
{
//
$this->clearPostCache($post);
}
/**
@@ -51,4 +49,24 @@ 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);
}
}