Skip to content

Commit

Permalink
correct PR #5, edit cache on Posts and News detail pages with Route::…
Browse files Browse the repository at this point in the history
…bind method on RouteServiceProvider
  • Loading branch information
pashaapsky committed Nov 8, 2020
1 parent 84ed030 commit 87ae2be
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
4 changes: 3 additions & 1 deletion app/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
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', 'tags', 'statistics'])->remember('posts', 3600, function () {
$posts = Cache::tags(['posts', 'tags', 'comments'])->remember('posts', 3600, function () {
return Post::with(['tags', 'comments'])->latest()->get();
});

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

Expand Down
6 changes: 1 addition & 5 deletions 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', 'tags', 'statistics'])->remember('news', 3600, function () {
$news = Cache::tags(['news', 'tags', 'comments'])->remember('news', 3600, function () {
return News::with(['tags', 'comments'])->latest()->get();
});

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

Expand Down
6 changes: 1 addition & 5 deletions 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', '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();
});

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

Expand Down
21 changes: 19 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
}

/**
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', '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();
});

Expand Down

0 comments on commit 87ae2be

Please sign in to comment.