Skip to content

Commit a3fd009

Browse files
committed
correct PR part2 - edit cache logic
1 parent c90e9d1 commit a3fd009

File tree

12 files changed

+24
-13
lines changed

12 files changed

+24
-13
lines changed

app/Comment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace App;
44

5+
use App\Traits\CacheModelActions;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78

89
class Comment extends Model
910
{
1011
use HasFactory;
12+
use CacheModelActions;
1113

1214
protected $table = 'comments';
15+
public static $cacheTags = ['comments', 'news', 'posts'];
1316

1417
protected $guarded = [''];
1518

app/Http/Controllers/AdministrationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function posts() {
2727
}
2828

2929
public function news() {
30-
$news = Cache::tags('news')->remember('news', 3600, function () {
30+
$news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () {
3131
return News::with(['tags', 'comments'])->latest()->get();
3232
});
3333

app/Http/Controllers/NewsController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ public function validateRequest($request, $new)
2727

2828
public function index()
2929
{
30-
$news = Cache::tags('news')->remember('news', 3600, function () {
30+
$news = Cache::tags(['news', 'index_news'])->remember('news', 3600, function () {
3131
return News::with(['tags', 'comments'])->latest()->get();
3232
});
33-
// $news = News::with(['tags', 'comments'])->latest()->get();
3433

3534
return view('news.index', compact('news'));
3635
}

app/Http/Controllers/PostsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function validateRequest($request, $post)
3131

3232
public function index()
3333
{
34-
$posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () {
34+
$posts = Cache::tags(['posts', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () {
3535
return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get();
3636
});
3737

app/Http/Controllers/StaticPagesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function aboutIndex() {
2323
}
2424

2525
public function statisticsIndex() {
26-
$statistics = Cache::tags('statistics_data')->remember('statistics_data', 3600, function () {
26+
$statistics = Cache::tags(['news', 'posts', 'tags_cloud', 'statistics', 'statistics_data'])->remember('statistics_data', 3600, function () {
2727
//Общее количество статей
2828
$postsCount = $this->statisticService->getPostsCount();
2929

app/Http/Controllers/TagsController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
use App\Tag;
66
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Cache;
78

89
class TagsController extends Controller
910
{
1011
public function index(Tag $tag)
1112
{
12-
$posts = $tag->posts()->with('tags')->get();
13-
$news = $tag->news()->with('tags')->get();
13+
$posts = Cache::tags(['posts', 'posts_by_tag'])->remember('posts_by_tag', 3600, function () use ($tag) {
14+
return $tag->posts()->with('tags')->get();
15+
});
16+
17+
$news = Cache::tags(['news', 'news_by_tag'])->remember('news_by_tag', 3600, function () use ($tag) {
18+
return $tag->news()->with('tags')->get();
19+
});
20+
1421
return view('layouts.tags.index', compact('posts', 'news'));
1522
}
1623
}

app/News.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class News extends Model
1212
use CacheModelActions;
1313

1414
protected $guarded = [];
15-
public static $cacheTags = ['news', 'latest_news', 'statistics_data', 'tags_cloud'];
15+
public static $cacheTags = ['news'];
1616

1717
public function tags()
1818
{

app/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Post extends Model
1616
protected $table = 'posts';
1717
protected $guarded = [];
1818

19-
public static $cacheTags = ['posts', 'latest_published_posts', 'tags_cloud', 'statistics_data'];
19+
public static $cacheTags = ['posts'];
2020

2121
protected $dispatchesEvents = [
2222
'created' => PostCreated::class,

app/Providers/AppServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AppServiceProvider extends ServiceProvider
2222
public function register()
2323
{
2424
view()->composer('layouts.aside-tags', function ($view) {
25-
$tags = Cache::tags('tags_cloud')->remember('tags_cloud', 3600, function () {
25+
$tags = Cache::tags(['posts', 'news', 'tags_cloud'])->remember('tags_cloud', 3600, function () {
2626
return Tag::whereHas('posts')->orWhereHas('news')->get();
2727
});
2828

app/Tag.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace App;
44

5+
use App\Traits\CacheModelActions;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78

89
class Tag extends Model
910
{
1011
use HasFactory;
12+
use CacheModelActions;
1113

1214
protected $table = 'tags';
15+
public static $cacheTags = ['posts', 'news', 'tags_cloud'];
1316

1417
protected $guarded = [];
1518

0 commit comments

Comments
 (0)