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
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace App\Observers;
use App\Models\Category;
use App\Services\App\Cache\CategoryCacheService;
class CategoryObserver
{
protected CategoryCacheService $categoryCacheService;
public function __construct()
{
$this->categoryCacheService = new CategoryCacheService();
}
/**
* Handle the Post "created" event.
*/
public function saved(Category $category): void
{
$this->categoryCacheService->clearAllCacheByModel();
}
/**
* Handle the Post "updated" event.
*/
public function updated(Category $category)
{
$this->categoryCacheService->clearAllCacheByModel();
}
/**
* Очистка кеша при удалении поста.
*/
public function deleted(Category $category)
{
$this->categoryCacheService->clearAllCacheByModel();
}
/**
* Handle the Post "restored" event.
*/
public function restored(Category $category): void
{
//
}
/**
* Handle the Post "force deleted" event.
*/
public function forceDeleted(Category $category): void
{
//
}
}
+11 -7
View File
@@ -3,16 +3,24 @@
namespace App\Observers;
use App\Models\Page;
use App\Services\App\Cache\PageCacheService;
use Illuminate\Support\Facades\Cache;
class PageObserver
{
private PageCacheService $pageCacheService;
public function __construct()
{
$this->pageCacheService = app(PageCacheService::class);
}
/**
* Handle the Page "created" event.
*/
public function created(Page $page): void
{
Cache::forget('navigation');
$this->pageCacheService->clearAllCacheByModel();
}
/**
@@ -20,9 +28,7 @@ class PageObserver
*/
public function updated(Page $page)
{
$cacheKey = 'page_' . md5($page->path);
Cache::forget($cacheKey);
Cache::forget('navigation');
$this->pageCacheService->clearCache($page);
}
/**
@@ -30,9 +36,7 @@ class PageObserver
*/
public function deleted(Page $page)
{
$cacheKey = 'page_' . md5($page->path);
Cache::forget($cacheKey);
Cache::forget('navigation');
$this->pageCacheService->clearAllCacheByModel();
}
/**
+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);
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace App\Observers;
use App\Models\Tag;
use App\Services\App\Cache\TagCacheService;
class TagObserver
{
protected TagCacheService $tagCacheService;
public function __construct()
{
$this->tagCacheService = new TagCacheService();
}
/**
* Handle the Post "created" event.
*/
public function saved(Tag $tag): void
{
$this->tagCacheService->clearAllCacheByModel();
}
/**
* Handle the Post "updated" event.
*/
public function updated(Tag $tag)
{
$this->tagCacheService->clearAllCacheByModel();
}
/**
* Очистка кеша при удалении поста.
*/
public function deleted(Tag $tag)
{
$this->tagCacheService->clearAllCacheByModel();
}
/**
* Handle the Post "restored" event.
*/
public function restored(Tag $tag): void
{
//
}
/**
* Handle the Post "force deleted" event.
*/
public function forceDeleted(Tag $tag): void
{
//
}
}