Skip to content

Commit

Permalink
correct PR #4 edit cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pashaapsky committed Nov 6, 2020
1 parent 9129fdc commit 84ed030
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/AdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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();
});

return view('/admin.posts', compact('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();
});

Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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'));
}

Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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'));
}

Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit 84ed030

Please sign in to comment.