From 209360cbe72dae234bbb5ec1696340382b7647cf Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Mon, 2 Nov 2020 22:46:20 +0300 Subject: [PATCH 1/6] add caching with redis --- .env.example | 7 +- app/Events/News/NewsCreated.php | 25 +++++++ app/Events/News/NewsDeleted.php | 25 +++++++ app/Events/News/NewsUpdated.php | 25 +++++++ app/Events/{ => Posts}/PostCreated.php | 5 -- app/Events/Posts/PostDeleted.php | 20 ++++++ app/Events/{ => Posts}/PostUpdated.php | 0 .../{ => Posts}/PostUpdatedAdminChat.php | 0 .../Controllers/AdministrationController.php | 11 ++- app/Http/Controllers/FeedbacksController.php | 11 ++- app/Http/Controllers/NewsController.php | 10 +-- app/Http/Controllers/PostsController.php | 7 +- .../Controllers/StaticPagesController.php | 71 ++++++++++--------- app/News.php | 9 +++ app/Observers/NewsObserver.php | 42 +++++++++++ app/Observers/PostsObserver.php | 42 +++++++++++ app/Post.php | 4 ++ app/Providers/AppServiceProvider.php | 13 +++- public/css/app.css | 29 ++++++++ resources/sass/app.scss | 1 + resources/sass/{test.scss => flash-msg.scss} | 0 routes/web.php | 14 ++-- 22 files changed, 311 insertions(+), 60 deletions(-) create mode 100644 app/Events/News/NewsCreated.php create mode 100644 app/Events/News/NewsDeleted.php create mode 100644 app/Events/News/NewsUpdated.php rename app/Events/{ => Posts}/PostCreated.php (78%) create mode 100644 app/Events/Posts/PostDeleted.php rename app/Events/{ => Posts}/PostUpdated.php (100%) rename app/Events/{ => Posts}/PostUpdatedAdminChat.php (100%) create mode 100644 app/Observers/NewsObserver.php create mode 100644 app/Observers/PostsObserver.php rename resources/sass/{test.scss => flash-msg.scss} (100%) diff --git a/.env.example b/.env.example index a4b04fd..0053a9e 100644 --- a/.env.example +++ b/.env.example @@ -13,15 +13,16 @@ DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= -BROADCAST_DRIVER=log -CACHE_DRIVER=file -QUEUE_CONNECTION=sync +BROADCAST_DRIVER=redis +CACHE_DRIVER=redis +QUEUE_CONNECTION=redis SESSION_DRIVER=file SESSION_LIFETIME=120 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 +REDIS_PREFIX=apsky_laravel_database_ MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io diff --git a/app/Events/News/NewsCreated.php b/app/Events/News/NewsCreated.php new file mode 100644 index 0000000..04aa780 --- /dev/null +++ b/app/Events/News/NewsCreated.php @@ -0,0 +1,25 @@ +news = $news; + } +} diff --git a/app/Events/News/NewsDeleted.php b/app/Events/News/NewsDeleted.php new file mode 100644 index 0000000..1f88909 --- /dev/null +++ b/app/Events/News/NewsDeleted.php @@ -0,0 +1,25 @@ +news = $news; + } +} diff --git a/app/Events/News/NewsUpdated.php b/app/Events/News/NewsUpdated.php new file mode 100644 index 0000000..0edae9b --- /dev/null +++ b/app/Events/News/NewsUpdated.php @@ -0,0 +1,25 @@ +news = $news; + } +} diff --git a/app/Events/PostCreated.php b/app/Events/Posts/PostCreated.php similarity index 78% rename from app/Events/PostCreated.php rename to app/Events/Posts/PostCreated.php index e1f6f1f..d3ec0d3 100644 --- a/app/Events/PostCreated.php +++ b/app/Events/Posts/PostCreated.php @@ -12,11 +12,6 @@ class PostCreated public Post $post; - /** - * Create a new event instance. - * - * @param Post $post - */ public function __construct(Post $post) { $this->post = $post; diff --git a/app/Events/Posts/PostDeleted.php b/app/Events/Posts/PostDeleted.php new file mode 100644 index 0000000..a3cfba3 --- /dev/null +++ b/app/Events/Posts/PostDeleted.php @@ -0,0 +1,20 @@ +post = $post; + } +} diff --git a/app/Events/PostUpdated.php b/app/Events/Posts/PostUpdated.php similarity index 100% rename from app/Events/PostUpdated.php rename to app/Events/Posts/PostUpdated.php diff --git a/app/Events/PostUpdatedAdminChat.php b/app/Events/Posts/PostUpdatedAdminChat.php similarity index 100% rename from app/Events/PostUpdatedAdminChat.php rename to app/Events/Posts/PostUpdatedAdminChat.php diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index 1df4d39..40f9786 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -5,6 +5,7 @@ use App\News; use App\Post; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; class AdministrationController extends Controller { @@ -18,12 +19,18 @@ public function index() { } public function posts() { - $posts = Post::with('tags')->latest()->get(); + $posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () { + return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); + }); + return view('/admin.posts', compact('posts')); } public function news() { - $news = News::latest()->get(); + $news = Cache::tags('news')->remember('users_news|' . auth()->id(), 3600, function () { + return News::with(['tags', 'comments'])->latest()->get(); + }); + return view('/admin.news', compact('news')); } diff --git a/app/Http/Controllers/FeedbacksController.php b/app/Http/Controllers/FeedbacksController.php index 2482d35..a368bc0 100644 --- a/app/Http/Controllers/FeedbacksController.php +++ b/app/Http/Controllers/FeedbacksController.php @@ -3,13 +3,14 @@ namespace App\Http\Controllers; use App\Feedback; +use Illuminate\Filesystem\Cache; use Illuminate\Http\Request; class FeedbacksController extends Controller { public function __construct() { - $this->middleware('role:admin'); + $this->middleware('role:admin')->except('store'); } public function index() @@ -28,6 +29,12 @@ public function store(Request $request) Feedback::create($request->all()); - return redirect('/admin/feedbacks'); + flash('Feedback successfully send (:', 'success'); + + if (auth()->user() && auth()->user()->hasRole('admin')) { + return redirect('/admin/feedbacks'); + } else { + return back(); + } } } diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index e87ced5..a6806b8 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -3,10 +3,10 @@ namespace App\Http\Controllers; use App\News; -use App\NewTag; use App\Services\TagsCreatorService; use App\Tag; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use Illuminate\Validation\Rule; class NewsController extends Controller @@ -27,7 +27,11 @@ public function validateRequest($request, $new) public function index() { - $news = News::with(['tags', 'comments'])->latest()->get(); + $news = Cache::tags('news')->remember('users_news|' . auth()->id(), 3600, function () { + return News::with(['tags', 'comments'])->latest()->get(); + }); + +// $news = News::with(['tags', 'comments'])->latest()->get(); return view('news.index', compact('news')); } @@ -75,8 +79,6 @@ public function update(Request $request, News $new) $updater = new TagsCreatorService($new, $request); $updater->updateTags(); -// updateTags($new, $request); - flash( 'New updated successfully'); return back(); diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index dce5829..7aca47b 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -6,10 +6,10 @@ use App\Notifications\PostDeleted; use App\Notifications\PostEdited; use App\Post; -use App\PostTag; use App\Services\TagsCreatorService; use App\Tag; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use Illuminate\Validation\Rule; class PostsController extends Controller @@ -31,7 +31,10 @@ public function validateRequest($request, $post) public function index() { - $posts = auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); + $posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () { + return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); + }); + return view('/posts.index', compact('posts')); } diff --git a/app/Http/Controllers/StaticPagesController.php b/app/Http/Controllers/StaticPagesController.php index 50ff25e..bcf941b 100644 --- a/app/Http/Controllers/StaticPagesController.php +++ b/app/Http/Controllers/StaticPagesController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Services\StatisticService; +use Illuminate\Support\Facades\Cache; class StaticPagesController extends Controller { @@ -22,40 +23,42 @@ public function aboutIndex() { } public function statisticsIndex() { - //Общее количество статей - $postsCount = $this->statisticService->getPostsCount(); - - //Общее количество новостей - $newsCount = $this->statisticService->getNewsCount(); - - //ФИО автора, у которого больше всего статей на сайте - $userWithMostPosts = $this->statisticService->getUserWithMaxPosts(); - - //Самая длинная статья - название, ссылка на статью и длина статьи в символах - $theLongestPost = $this->statisticService->getTheLongestPost(); - - //Самая короткая статья - название, ссылка на статью и длина статьи в символах - $theShortestPost = $this->statisticService->getTheShortestPost(); - - //Средние количество статей у “активных” пользователей, при этом активным пользователь считается, если у него есть более 1-й статьи - $avgPostsHaveActiveUsers = $this->statisticService->getAveragePosts(); - - //Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз - $mostChangingPost = $this->statisticService->getMostChangingPost(); - - //Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев. - $mostCommentPost = $this->statisticService->getMostCommentPost(); - - $statistics = [ - 'posts_count' => $postsCount, - 'news_count' => $newsCount, - 'user_with_most_posts' => $userWithMostPosts, - 'the_longest_post' => $theLongestPost, - 'the_shortest_post' => $theShortestPost, - 'avg_posts_have_active_users' => $avgPostsHaveActiveUsers, - 'most_changing_post' => $mostChangingPost, - 'most_comment_post' => $mostCommentPost - ]; + $statistics = Cache::tags('statistics_data')->remember('statistics_data', 3600, function () { + //Общее количество статей + $postsCount = $this->statisticService->getPostsCount(); + + //Общее количество новостей + $newsCount = $this->statisticService->getNewsCount(); + + //ФИО автора, у которого больше всего статей на сайте + $userWithMostPosts = $this->statisticService->getUserWithMaxPosts(); + + //Самая длинная статья - название, ссылка на статью и длина статьи в символах + $theLongestPost = $this->statisticService->getTheLongestPost(); + + //Самая короткая статья - название, ссылка на статью и длина статьи в символах + $theShortestPost = $this->statisticService->getTheShortestPost(); + + //Средние количество статей у “активных” пользователей, при этом активным пользователь считается, если у него есть более 1-й статьи + $avgPostsHaveActiveUsers = $this->statisticService->getAveragePosts(); + + //Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз + $mostChangingPost = $this->statisticService->getMostChangingPost(); + + //Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев. + $mostCommentPost = $this->statisticService->getMostCommentPost(); + + return [ + 'posts_count' => $postsCount, + 'news_count' => $newsCount, + 'user_with_most_posts' => $userWithMostPosts, + 'the_longest_post' => $theLongestPost, + 'the_shortest_post' => $theShortestPost, + 'avg_posts_have_active_users' => $avgPostsHaveActiveUsers, + 'most_changing_post' => $mostChangingPost, + 'most_comment_post' => $mostCommentPost + ]; + }); return view('static.statistics', ['statistics' => $statistics]); } diff --git a/app/News.php b/app/News.php index 91b9eae..374a2dc 100644 --- a/app/News.php +++ b/app/News.php @@ -2,6 +2,9 @@ namespace App; +use App\Events\NewsCreated; +use App\Events\NewsDeleted; +use App\Events\NewsUpdated; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -11,6 +14,12 @@ class News extends Model protected $guarded = []; + protected $dispatchesEvents = [ + 'created' => NewsCreated::class, + 'updated' => NewsUpdated::class, + 'deleted' => NewsDeleted::class + ]; + public function tags() { // return $this->belongsToMany(Tag::class, 'new_tag', 'new_id', 'tag_id'); diff --git a/app/Observers/NewsObserver.php b/app/Observers/NewsObserver.php new file mode 100644 index 0000000..db4907b --- /dev/null +++ b/app/Observers/NewsObserver.php @@ -0,0 +1,42 @@ +flush(); + } + + /** + * Handle the news "updated" event. + * + * @param \App\News $news + * @return void + */ + public function updated(News $news) + { + Cache::tags(['news', 'latest_news', 'statistics_data', 'tags_cloud'])->flush(); + } + + /** + * Handle the news "deleted" event. + * + * @param \App\News $news + * @return void + */ + public function deleted(News $news) + { + Cache::tags(['news', 'latest_news', 'statistics_data', 'tags_cloud'])->flush(); + } +} diff --git a/app/Observers/PostsObserver.php b/app/Observers/PostsObserver.php new file mode 100644 index 0000000..8e25ce1 --- /dev/null +++ b/app/Observers/PostsObserver.php @@ -0,0 +1,42 @@ +flush(); + } + + /** + * Handle the post "updated" event. + * + * @param \App\Post $post + * @return void + */ + public function updated(Post $post) + { + Cache::tags(['posts', 'latest_published_posts', 'tags_cloud'])->flush(); + } + + /** + * Handle the post "deleted" event. + * + * @param \App\Post $post + * @return void + */ + public function deleted(Post $post) + { + Cache::tags(['posts', 'latest_published_posts', 'tags_cloud'])->flush(); + } +} diff --git a/app/Post.php b/app/Post.php index 2341625..7aecc19 100644 --- a/app/Post.php +++ b/app/Post.php @@ -2,7 +2,9 @@ namespace App; +use App\Events\PostDeleted; use App\Events\PostUpdated; +use App\Mail\PostCreated; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -15,7 +17,9 @@ class Post extends Model protected $guarded = []; protected $dispatchesEvents = [ + 'created' => PostCreated::class, 'updated' => PostUpdated::class, + 'deleted' => PostDeleted::class ]; public function tags() diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 158eefe..dd2ef92 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,8 +2,13 @@ namespace App\Providers; +use App\News; +use App\Observers\NewsObserver; +use App\Observers\PostsObserver; +use App\Post; use App\Services\StatisticService; use App\Tag; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -12,11 +17,14 @@ class AppServiceProvider extends ServiceProvider * Register any application services. * * @return void + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function register() { view()->composer('layouts.aside-tags', function ($view) { - $tags = Tag::whereHas('posts')->orWhereHas('news')->get(); + $tags = Cache::tags('tags_cloud')->remember('tags_cloud', 3600, function () { + return Tag::whereHas('posts')->orWhereHas('news')->get(); + }); $view->with('tagsCloud', $tags); }); @@ -31,6 +39,7 @@ public function register() */ public function boot() { - + Post::observe(PostsObserver::class); + News::observe(NewsObserver::class); } } diff --git a/public/css/app.css b/public/css/app.css index c0776bd..9860964 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -10928,3 +10928,32 @@ a.text-dark:focus { text-decoration: underline; } +.notify-msg { + -webkit-animation: my-animation 10s; + animation: my-animation 10s; + -webkit-animation-fill-mode: both; + animation-fill-mode: both; +} + +@-webkit-keyframes my-animation { + 0% { + opacity: 1; + } + + 100% { + opacity: 0; + z-index: -2; + } +} + +@keyframes my-animation { + 0% { + opacity: 1; + } + + 100% { + opacity: 0; + z-index: -2; + } +} + diff --git a/resources/sass/app.scss b/resources/sass/app.scss index 2a25372..2c9cc91 100644 --- a/resources/sass/app.scss +++ b/resources/sass/app.scss @@ -8,3 +8,4 @@ @import '~bootstrap/scss/bootstrap'; @import "admin-chat"; +@import "flash-msg"; diff --git a/resources/sass/test.scss b/resources/sass/flash-msg.scss similarity index 100% rename from resources/sass/test.scss rename to resources/sass/flash-msg.scss diff --git a/routes/web.php b/routes/web.php index 3b8a818..f56e15d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,15 +3,17 @@ use App\News; use App\Post; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Route; -Route::get('/test', function () { - -}); - Route::get('/', function () { - $posts = Post::with('tags')->where('published', 1)->latest()->take(4)->get(); - $news = News::with('tags')->latest()->take(3)->get(); + $posts = Cache::tags('latest_published_posts')->remember('latest_publish_posts', 3600, function () { + return Post::with('tags')->where('published', 1)->latest()->take(4)->get(); + }); + + $news = Cache::tags('latest_news')->remember('latest_news', 3600, function () { + return News::with('tags')->latest()->take(3)->get(); + }); return view('/index', compact('posts', 'news')); })->name('home'); From c90e9d11df5824f8e97e8726d33a1119d6f5e36f Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Tue, 3 Nov 2020 21:48:02 +0300 Subject: [PATCH 2/6] correct PR, make trait for cacheModelActions --- app/Events/News/NewsCreated.php | 25 ----------- app/Events/News/NewsDeleted.php | 25 ----------- app/Events/News/NewsUpdated.php | 25 ----------- app/Events/Posts/PostDeleted.php | 20 --------- .../Controllers/AdministrationController.php | 2 +- app/Http/Controllers/NewsController.php | 3 +- app/Listeners/AddHistoryOnUpdatePost.php | 6 ++- app/News.php | 12 ++---- app/Observers/NewsObserver.php | 42 ------------------- app/Observers/PostsObserver.php | 42 ------------------- app/Post.php | 7 ++-- app/Providers/AppServiceProvider.php | 3 +- app/Providers/EventServiceProvider.php | 5 --- app/Traits/CacheModelActions.php | 27 ++++++++++++ 14 files changed, 41 insertions(+), 203 deletions(-) delete mode 100644 app/Events/News/NewsCreated.php delete mode 100644 app/Events/News/NewsDeleted.php delete mode 100644 app/Events/News/NewsUpdated.php delete mode 100644 app/Events/Posts/PostDeleted.php delete mode 100644 app/Observers/NewsObserver.php delete mode 100644 app/Observers/PostsObserver.php create mode 100644 app/Traits/CacheModelActions.php diff --git a/app/Events/News/NewsCreated.php b/app/Events/News/NewsCreated.php deleted file mode 100644 index 04aa780..0000000 --- a/app/Events/News/NewsCreated.php +++ /dev/null @@ -1,25 +0,0 @@ -news = $news; - } -} diff --git a/app/Events/News/NewsDeleted.php b/app/Events/News/NewsDeleted.php deleted file mode 100644 index 1f88909..0000000 --- a/app/Events/News/NewsDeleted.php +++ /dev/null @@ -1,25 +0,0 @@ -news = $news; - } -} diff --git a/app/Events/News/NewsUpdated.php b/app/Events/News/NewsUpdated.php deleted file mode 100644 index 0edae9b..0000000 --- a/app/Events/News/NewsUpdated.php +++ /dev/null @@ -1,25 +0,0 @@ -news = $news; - } -} diff --git a/app/Events/Posts/PostDeleted.php b/app/Events/Posts/PostDeleted.php deleted file mode 100644 index a3cfba3..0000000 --- a/app/Events/Posts/PostDeleted.php +++ /dev/null @@ -1,20 +0,0 @@ -post = $post; - } -} diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index 40f9786..e10048f 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -27,7 +27,7 @@ public function posts() { } public function news() { - $news = Cache::tags('news')->remember('users_news|' . auth()->id(), 3600, function () { + $news = Cache::tags('news')->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index a6806b8..e1b7945 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -27,10 +27,9 @@ public function validateRequest($request, $new) public function index() { - $news = Cache::tags('news')->remember('users_news|' . auth()->id(), 3600, function () { + $news = Cache::tags('news')->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); - // $news = News::with(['tags', 'comments'])->latest()->get(); return view('news.index', compact('news')); diff --git a/app/Listeners/AddHistoryOnUpdatePost.php b/app/Listeners/AddHistoryOnUpdatePost.php index 2516d74..f581755 100644 --- a/app/Listeners/AddHistoryOnUpdatePost.php +++ b/app/Listeners/AddHistoryOnUpdatePost.php @@ -33,8 +33,10 @@ public function handle(PostUpdated $event) 'text' => $result ]; - $event->post->history()->create($values); + if (!empty($result)) { + $event->post->history()->create($values); - event(new PostUpdatedAdminChat($event->post, $result)); + event(new PostUpdatedAdminChat($event->post, $result)); + } } } diff --git a/app/News.php b/app/News.php index 374a2dc..f6af288 100644 --- a/app/News.php +++ b/app/News.php @@ -2,23 +2,17 @@ namespace App; -use App\Events\NewsCreated; -use App\Events\NewsDeleted; -use App\Events\NewsUpdated; +use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class News extends Model { use HasFactory; + use CacheModelActions; protected $guarded = []; - - protected $dispatchesEvents = [ - 'created' => NewsCreated::class, - 'updated' => NewsUpdated::class, - 'deleted' => NewsDeleted::class - ]; + public static $cacheTags = ['news', 'latest_news', 'statistics_data', 'tags_cloud']; public function tags() { diff --git a/app/Observers/NewsObserver.php b/app/Observers/NewsObserver.php deleted file mode 100644 index db4907b..0000000 --- a/app/Observers/NewsObserver.php +++ /dev/null @@ -1,42 +0,0 @@ -flush(); - } - - /** - * Handle the news "updated" event. - * - * @param \App\News $news - * @return void - */ - public function updated(News $news) - { - Cache::tags(['news', 'latest_news', 'statistics_data', 'tags_cloud'])->flush(); - } - - /** - * Handle the news "deleted" event. - * - * @param \App\News $news - * @return void - */ - public function deleted(News $news) - { - Cache::tags(['news', 'latest_news', 'statistics_data', 'tags_cloud'])->flush(); - } -} diff --git a/app/Observers/PostsObserver.php b/app/Observers/PostsObserver.php deleted file mode 100644 index 8e25ce1..0000000 --- a/app/Observers/PostsObserver.php +++ /dev/null @@ -1,42 +0,0 @@ -flush(); - } - - /** - * Handle the post "updated" event. - * - * @param \App\Post $post - * @return void - */ - public function updated(Post $post) - { - Cache::tags(['posts', 'latest_published_posts', 'tags_cloud'])->flush(); - } - - /** - * Handle the post "deleted" event. - * - * @param \App\Post $post - * @return void - */ - public function deleted(Post $post) - { - Cache::tags(['posts', 'latest_published_posts', 'tags_cloud'])->flush(); - } -} diff --git a/app/Post.php b/app/Post.php index 7aecc19..4798227 100644 --- a/app/Post.php +++ b/app/Post.php @@ -2,24 +2,25 @@ namespace App; -use App\Events\PostDeleted; use App\Events\PostUpdated; use App\Mail\PostCreated; +use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; + use CacheModelActions; protected $table = 'posts'; - protected $guarded = []; + public static $cacheTags = ['posts', 'latest_published_posts', 'tags_cloud', 'statistics_data']; + protected $dispatchesEvents = [ 'created' => PostCreated::class, 'updated' => PostUpdated::class, - 'deleted' => PostDeleted::class ]; public function tags() diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index dd2ef92..0ad4f7e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -39,7 +39,6 @@ public function register() */ public function boot() { - Post::observe(PostsObserver::class); - News::observe(NewsObserver::class); + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index feec67d..beef509 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,14 +2,11 @@ namespace App\Providers; -use App\Events\PostCreated; use App\Events\PostUpdated; use App\Listeners\AddHistoryOnUpdatePost; -use App\Listeners\SendPostCreateNotification; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; -use Illuminate\Support\Facades\Event; class EventServiceProvider extends ServiceProvider { @@ -35,7 +32,5 @@ class EventServiceProvider extends ServiceProvider public function boot() { parent::boot(); - - // } } diff --git a/app/Traits/CacheModelActions.php b/app/Traits/CacheModelActions.php new file mode 100644 index 0000000..314a685 --- /dev/null +++ b/app/Traits/CacheModelActions.php @@ -0,0 +1,27 @@ +flush(); + }); + + static::updated(function($item) + { + dd($item); + Cache::tags(static::$cacheTags)->flush(); + }); + + static::deleted(function() + { + Cache::tags(static::$cacheTags)->flush(); + }); + } +} From a3fd0097d9beb25678a866e93f8ed4c5ea69cf84 Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Wed, 4 Nov 2020 16:20:29 +0300 Subject: [PATCH 3/6] correct PR part2 - edit cache logic --- app/Comment.php | 3 +++ app/Http/Controllers/AdministrationController.php | 2 +- app/Http/Controllers/NewsController.php | 3 +-- app/Http/Controllers/PostsController.php | 2 +- app/Http/Controllers/StaticPagesController.php | 2 +- app/Http/Controllers/TagsController.php | 11 +++++++++-- app/News.php | 2 +- app/Post.php | 2 +- app/Providers/AppServiceProvider.php | 2 +- app/Tag.php | 3 +++ app/Traits/CacheModelActions.php | 1 - routes/web.php | 4 ++-- 12 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/Comment.php b/app/Comment.php index f17422d..f84d161 100644 --- a/app/Comment.php +++ b/app/Comment.php @@ -2,14 +2,17 @@ namespace App; +use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; + use CacheModelActions; protected $table = 'comments'; + public static $cacheTags = ['comments', 'news', 'posts']; protected $guarded = ['']; diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index e10048f..6bed02b 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -27,7 +27,7 @@ public function posts() { } public function news() { - $news = Cache::tags('news')->remember('news', 3600, function () { + $news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index e1b7945..d52ae2a 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -27,10 +27,9 @@ public function validateRequest($request, $new) public function index() { - $news = Cache::tags('news')->remember('news', 3600, function () { + $news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); -// $news = News::with(['tags', 'comments'])->latest()->get(); return view('news.index', compact('news')); } diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index 7aca47b..85c8678 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -31,7 +31,7 @@ public function validateRequest($request, $post) public function index() { - $posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () { + $posts = Cache::tags(['posts', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () { return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/StaticPagesController.php b/app/Http/Controllers/StaticPagesController.php index bcf941b..2e17f90 100644 --- a/app/Http/Controllers/StaticPagesController.php +++ b/app/Http/Controllers/StaticPagesController.php @@ -23,7 +23,7 @@ public function aboutIndex() { } public function statisticsIndex() { - $statistics = Cache::tags('statistics_data')->remember('statistics_data', 3600, function () { + $statistics = Cache::tags(['news', 'posts', 'tags_cloud', 'statistics', 'statistics_data'])->remember('statistics_data', 3600, function () { //Общее количество статей $postsCount = $this->statisticService->getPostsCount(); diff --git a/app/Http/Controllers/TagsController.php b/app/Http/Controllers/TagsController.php index c9f9cf4..33c6b70 100644 --- a/app/Http/Controllers/TagsController.php +++ b/app/Http/Controllers/TagsController.php @@ -4,13 +4,20 @@ use App\Tag; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; class TagsController extends Controller { public function index(Tag $tag) { - $posts = $tag->posts()->with('tags')->get(); - $news = $tag->news()->with('tags')->get(); + $posts = Cache::tags(['posts', 'posts_by_tag'])->remember('posts_by_tag', 3600, function () use ($tag) { + return $tag->posts()->with('tags')->get(); + }); + + $news = Cache::tags(['news', 'news_by_tag'])->remember('news_by_tag', 3600, function () use ($tag) { + return $tag->news()->with('tags')->get(); + }); + return view('layouts.tags.index', compact('posts', 'news')); } } diff --git a/app/News.php b/app/News.php index f6af288..11e8639 100644 --- a/app/News.php +++ b/app/News.php @@ -12,7 +12,7 @@ class News extends Model use CacheModelActions; protected $guarded = []; - public static $cacheTags = ['news', 'latest_news', 'statistics_data', 'tags_cloud']; + public static $cacheTags = ['news']; public function tags() { diff --git a/app/Post.php b/app/Post.php index 4798227..56bfd75 100644 --- a/app/Post.php +++ b/app/Post.php @@ -16,7 +16,7 @@ class Post extends Model protected $table = 'posts'; protected $guarded = []; - public static $cacheTags = ['posts', 'latest_published_posts', 'tags_cloud', 'statistics_data']; + public static $cacheTags = ['posts']; protected $dispatchesEvents = [ 'created' => PostCreated::class, diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 0ad4f7e..713a6c4 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -22,7 +22,7 @@ class AppServiceProvider extends ServiceProvider public function register() { view()->composer('layouts.aside-tags', function ($view) { - $tags = Cache::tags('tags_cloud')->remember('tags_cloud', 3600, function () { + $tags = Cache::tags(['posts', 'news', 'tags_cloud'])->remember('tags_cloud', 3600, function () { return Tag::whereHas('posts')->orWhereHas('news')->get(); }); diff --git a/app/Tag.php b/app/Tag.php index 1fa0d0d..c6eab5d 100644 --- a/app/Tag.php +++ b/app/Tag.php @@ -2,14 +2,17 @@ namespace App; +use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Tag extends Model { use HasFactory; + use CacheModelActions; protected $table = 'tags'; + public static $cacheTags = ['posts', 'news', 'tags_cloud']; protected $guarded = []; diff --git a/app/Traits/CacheModelActions.php b/app/Traits/CacheModelActions.php index 314a685..67a5591 100644 --- a/app/Traits/CacheModelActions.php +++ b/app/Traits/CacheModelActions.php @@ -15,7 +15,6 @@ public static function bootCacheModelActions() static::updated(function($item) { - dd($item); Cache::tags(static::$cacheTags)->flush(); }); diff --git a/routes/web.php b/routes/web.php index f56e15d..635b3d6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -7,11 +7,11 @@ use Illuminate\Support\Facades\Route; Route::get('/', function () { - $posts = Cache::tags('latest_published_posts')->remember('latest_publish_posts', 3600, function () { + $posts = Cache::tags(['posts', 'latest_publish_posts'])->remember('latest_publish_posts', 3600, function () { return Post::with('tags')->where('published', 1)->latest()->take(4)->get(); }); - $news = Cache::tags('latest_news')->remember('latest_news', 3600, function () { + $news = Cache::tags(['news', 'latest_news'])->remember('latest_news', 3600, function () { return News::with('tags')->latest()->take(3)->get(); }); From 9129fdc54362f9154ffabe90298b0df9cb6e4838 Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Thu, 5 Nov 2020 20:25:41 +0300 Subject: [PATCH 4/6] correct PR #3, edit cache in models --- app/Comment.php | 3 --- app/Http/Controllers/AdministrationController.php | 6 +++--- app/Http/Controllers/NewsController.php | 2 +- app/Http/Controllers/StaticPagesController.php | 2 +- app/News.php | 2 +- app/Post.php | 2 +- app/Providers/AppServiceProvider.php | 6 +----- app/Tag.php | 2 +- app/Traits/CacheModelActions.php | 2 +- 9 files changed, 10 insertions(+), 17 deletions(-) diff --git a/app/Comment.php b/app/Comment.php index f84d161..f17422d 100644 --- a/app/Comment.php +++ b/app/Comment.php @@ -2,17 +2,14 @@ namespace App; -use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; - use CacheModelActions; protected $table = 'comments'; - public static $cacheTags = ['comments', 'news', 'posts']; protected $guarded = ['']; diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index 6bed02b..1452954 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -19,15 +19,15 @@ public function index() { } public function posts() { - $posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () { - return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); + $posts = Cache::tags('posts')->remember('posts', 3600, function () { + return Post::with(['tags', 'comments'])->latest()->get(); }); return view('/admin.posts', compact('posts')); } public function news() { - $news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () { + $news = Cache::tags('news')->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index d52ae2a..4dcd698 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -27,7 +27,7 @@ public function validateRequest($request, $new) public function index() { - $news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () { + $news = Cache::tags(['news'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/StaticPagesController.php b/app/Http/Controllers/StaticPagesController.php index 2e17f90..b11a598 100644 --- a/app/Http/Controllers/StaticPagesController.php +++ b/app/Http/Controllers/StaticPagesController.php @@ -23,7 +23,7 @@ public function aboutIndex() { } public function statisticsIndex() { - $statistics = Cache::tags(['news', 'posts', 'tags_cloud', 'statistics', 'statistics_data'])->remember('statistics_data', 3600, function () { + $statistics = Cache::tags(['news', 'posts', 'tags', 'statistics'])->remember('statistics_data', 3600, function () { //Общее количество статей $postsCount = $this->statisticService->getPostsCount(); diff --git a/app/News.php b/app/News.php index 11e8639..e82ac17 100644 --- a/app/News.php +++ b/app/News.php @@ -12,7 +12,7 @@ class News extends Model use CacheModelActions; protected $guarded = []; - public static $cacheTags = ['news']; + public static $cacheTags = 'news'; public function tags() { diff --git a/app/Post.php b/app/Post.php index 56bfd75..16767f3 100644 --- a/app/Post.php +++ b/app/Post.php @@ -16,7 +16,7 @@ class Post extends Model protected $table = 'posts'; protected $guarded = []; - public static $cacheTags = ['posts']; + public static $cacheTags = 'posts'; protected $dispatchesEvents = [ 'created' => PostCreated::class, diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 713a6c4..ccbb0e1 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,10 +2,6 @@ namespace App\Providers; -use App\News; -use App\Observers\NewsObserver; -use App\Observers\PostsObserver; -use App\Post; use App\Services\StatisticService; use App\Tag; use Illuminate\Support\Facades\Cache; @@ -22,7 +18,7 @@ class AppServiceProvider extends ServiceProvider public function register() { view()->composer('layouts.aside-tags', function ($view) { - $tags = Cache::tags(['posts', 'news', 'tags_cloud'])->remember('tags_cloud', 3600, function () { + $tags = Cache::tags(['posts', 'news', 'tags'])->remember('tags', 3600, function () { return Tag::whereHas('posts')->orWhereHas('news')->get(); }); diff --git a/app/Tag.php b/app/Tag.php index c6eab5d..f65aeae 100644 --- a/app/Tag.php +++ b/app/Tag.php @@ -12,7 +12,7 @@ class Tag extends Model use CacheModelActions; protected $table = 'tags'; - public static $cacheTags = ['posts', 'news', 'tags_cloud']; + public static $cacheTags = 'tags'; protected $guarded = []; diff --git a/app/Traits/CacheModelActions.php b/app/Traits/CacheModelActions.php index 67a5591..d4668de 100644 --- a/app/Traits/CacheModelActions.php +++ b/app/Traits/CacheModelActions.php @@ -13,7 +13,7 @@ public static function bootCacheModelActions() Cache::tags(static::$cacheTags)->flush(); }); - static::updated(function($item) + static::updated(function() { Cache::tags(static::$cacheTags)->flush(); }); From 84ed03056afd5b09e61db96434c599f73622e6b7 Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Fri, 6 Nov 2020 19:26:53 +0300 Subject: [PATCH 5/6] correct PR #4 edit cache logic --- app/Http/Controllers/AdministrationController.php | 4 ++-- app/Http/Controllers/NewsController.php | 6 +++++- app/Http/Controllers/PostsController.php | 6 +++++- routes/web.php | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index 1452954..6903aa9 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -19,7 +19,7 @@ public function index() { } public function posts() { - $posts = Cache::tags('posts')->remember('posts', 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'statistics'])->remember('posts', 3600, function () { return Post::with(['tags', 'comments'])->latest()->get(); }); @@ -27,7 +27,7 @@ public function posts() { } public function news() { - $news = Cache::tags('news')->remember('news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'statistics'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index 4dcd698..a6fceb7 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -27,7 +27,7 @@ public function validateRequest($request, $new) public function index() { - $news = Cache::tags(['news'])->remember('news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'statistics'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); @@ -60,6 +60,10 @@ public function store(Request $request) public function show(News $new) { + $new = Cache::tags(['news', 'tags', 'statistics'])->remember('new|' . $new->id, 3600, function () use ($new) { + return $new; + }); + return view('news.show', compact('new')); } diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index 85c8678..753b66e 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -31,7 +31,7 @@ public function validateRequest($request, $post) public function index() { - $posts = Cache::tags(['posts', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'statistics', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () { return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); }); @@ -74,6 +74,10 @@ public function show(Post $post) { $this->authorize('view', $post); + $post = Cache::tags(['posts', 'tags', 'statistics'])->remember('post|' . $post->id, 3600, function () use ($post) { + return $post; + }); + return view('/posts.show', compact('post')); } diff --git a/routes/web.php b/routes/web.php index 635b3d6..41320c4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -7,11 +7,11 @@ use Illuminate\Support\Facades\Route; Route::get('/', function () { - $posts = Cache::tags(['posts', 'latest_publish_posts'])->remember('latest_publish_posts', 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'statistics', 'latest_publish_posts'])->remember('latest_publish_posts', 3600, function () { return Post::with('tags')->where('published', 1)->latest()->take(4)->get(); }); - $news = Cache::tags(['news', 'latest_news'])->remember('latest_news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'statistics', 'latest_news'])->remember('latest_news', 3600, function () { return News::with('tags')->latest()->take(3)->get(); }); From 87ae2be7398cba77f1cd46d2da5385c7a5b761a4 Mon Sep 17 00:00:00 2001 From: pashaapsky Date: Sun, 8 Nov 2020 13:31:59 +0300 Subject: [PATCH 6/6] correct PR #5, edit cache on Posts and News detail pages with Route::bind method on RouteServiceProvider --- app/Comment.php | 4 +++- .../Controllers/AdministrationController.php | 4 ++-- app/Http/Controllers/NewsController.php | 6 +----- app/Http/Controllers/PostsController.php | 6 +----- app/Providers/RouteServiceProvider.php | 21 +++++++++++++++++-- routes/web.php | 4 ++-- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/Comment.php b/app/Comment.php index f17422d..fe928a1 100644 --- a/app/Comment.php +++ b/app/Comment.php @@ -2,16 +2,18 @@ namespace App; +use App\Traits\CacheModelActions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; + use CacheModelActions; protected $table = 'comments'; - protected $guarded = ['']; + public static $cacheTags = 'comments'; public function commentable() { diff --git a/app/Http/Controllers/AdministrationController.php b/app/Http/Controllers/AdministrationController.php index 6903aa9..57d3fa6 100644 --- a/app/Http/Controllers/AdministrationController.php +++ b/app/Http/Controllers/AdministrationController.php @@ -19,7 +19,7 @@ public function index() { } public function posts() { - $posts = Cache::tags(['posts', 'tags', 'statistics'])->remember('posts', 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'comments'])->remember('posts', 3600, function () { return Post::with(['tags', 'comments'])->latest()->get(); }); @@ -27,7 +27,7 @@ public function posts() { } public function news() { - $news = Cache::tags(['news', 'tags', 'statistics'])->remember('news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'comments'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index a6fceb7..bd98e20 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -27,7 +27,7 @@ public function validateRequest($request, $new) public function index() { - $news = Cache::tags(['news', 'tags', 'statistics'])->remember('news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'comments'])->remember('news', 3600, function () { return News::with(['tags', 'comments'])->latest()->get(); }); @@ -60,10 +60,6 @@ public function store(Request $request) public function show(News $new) { - $new = Cache::tags(['news', 'tags', 'statistics'])->remember('new|' . $new->id, 3600, function () use ($new) { - return $new; - }); - return view('news.show', compact('new')); } diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index 753b66e..6e5ad8d 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -31,7 +31,7 @@ public function validateRequest($request, $post) public function index() { - $posts = Cache::tags(['posts', 'tags', 'statistics', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'comments', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () { return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get(); }); @@ -74,10 +74,6 @@ public function show(Post $post) { $this->authorize('view', $post); - $post = Cache::tags(['posts', 'tags', 'statistics'])->remember('post|' . $post->id, 3600, function () use ($post) { - return $post; - }); - return view('/posts.show', compact('post')); } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 3a816d8..acf1bdf 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -2,7 +2,10 @@ namespace App\Providers; +use App\News; +use App\Post; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Route; class RouteServiceProvider extends ServiceProvider @@ -30,9 +33,23 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { - // - parent::boot(); + + Route::bind('post', function ($value) { + $post = Cache::tags(['posts', 'tags', 'comments'])->remember('post|' . $value, 3600 , function () use ($value) { + return Post::where('id', $value)->firstOrFail(); + }); + + return $post; + }); + + Route::bind('new', function ($value) { + $new = Cache::tags(['news', 'tags', 'comments'])->remember('new|' . $value, 3600 , function () use ($value) { + return News::where('id', $value)->firstOrFail(); + }); + + return $new; + }); } /** diff --git a/routes/web.php b/routes/web.php index 41320c4..cab3da9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -7,11 +7,11 @@ use Illuminate\Support\Facades\Route; Route::get('/', function () { - $posts = Cache::tags(['posts', 'tags', 'statistics', 'latest_publish_posts'])->remember('latest_publish_posts', 3600, function () { + $posts = Cache::tags(['posts', 'tags', 'comments', 'latest_publish_posts'])->remember('latest_publish_posts', 3600, function () { return Post::with('tags')->where('published', 1)->latest()->take(4)->get(); }); - $news = Cache::tags(['news', 'tags', 'statistics', 'latest_news'])->remember('latest_news', 3600, function () { + $news = Cache::tags(['news', 'tags', 'comments', 'latest_news'])->remember('latest_news', 3600, function () { return News::with('tags')->latest()->take(3)->get(); });