Skip to content

Commit a598f4c

Browse files
committed
Fix slow queries
1 parent 286d549 commit a598f4c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

app/Http/Controllers/Forum/TagsController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Models\Tag;
88
use App\Models\Thread;
99
use App\Models\User;
10+
use Illuminate\Support\Facades\Cache;
1011
use Illuminate\View\View;
1112

1213
class TagsController extends Controller
@@ -35,8 +36,12 @@ public function show(Tag $tag): View
3536
}
3637

3738
$tags = Tag::orderBy('name')->get();
38-
$topMembers = User::mostSolutionsInLastDays(365)->take(5)->get();
39-
$moderators = User::moderators()->get();
39+
$topMembers = Cache::remember('topMembers', now()->addMinutes(30), function () {
40+
return User::mostSolutionsInLastDays(365)->take(5)->get();
41+
});
42+
$moderators = Cache::remember('moderators', now()->addMinutes(30), function () {
43+
return User::moderators()->get();
44+
});
4045
$canonical = canonical('forum.tag', [$tag->name, 'filter' => $filter]);
4146

4247
return view('forum.overview', compact('threads', 'filter', 'tags', 'topMembers', 'moderators', 'canonical') + ['activeTag' => $tag]);

app/Models/Thread.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static function feedByTagQuery(Tag $tag): Builder
295295
public static function feedQuery(): Builder
296296
{
297297
return self::query()
298-
->withCount(['repliesRelation as reply_count', 'likesRelation as like_count'])
298+
->select('threads.*')
299299
->latest('last_activity_at');
300300
}
301301

0 commit comments

Comments
 (0)