Changes
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user