Skip to content

Commit 469614a

Browse files
committed
Improve homepage speed
1 parent cb7d95b commit 469614a

File tree

5 files changed

+20
-93
lines changed

5 files changed

+20
-93
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,34 @@ class HomeController extends Controller
1313
{
1414
public function show()
1515
{
16-
$communityMembers = Cache::remember('communityMembers', now()->addMinutes(5), function () {
17-
return User::withCounts()
18-
->hasActivity()
19-
->notBanned()
20-
->inRandomOrder()
21-
->take(100)
22-
->get()
23-
->chunk(20);
24-
});
16+
$communityMembers = Cache::remember(
17+
'communityMembers',
18+
now()->addMinutes(5),
19+
fn () => User::notBanned()->inRandomOrder()->take(100)->get()->chunk(20)
20+
);
2521

26-
$totalUsers = Cache::remember('totalUsers', now()->addDay(), function () {
27-
return number_format(User::notBanned()->count());
28-
});
22+
$totalUsers = Cache::remember('totalUsers', now()->addDay(), fn () => number_format(User::notBanned()->count()));
2923

30-
$totalThreads = Cache::remember('totalThreads', now()->addDay(), function () {
31-
return number_format(Thread::count());
32-
});
24+
$totalThreads = Cache::remember('totalThreads', now()->addDay(), fn () => number_format(Thread::count()));
3325

34-
$totalReplies = Cache::remember('totalReplies', now()->addDay(), function () {
35-
return number_format(Reply::count());
36-
});
26+
$totalReplies = Cache::remember('totalReplies', now()->addDay(), fn () => number_format(Reply::count()));
3727

38-
$latestThreads = Cache::remember('latestThreads', now()->addHour(), function () {
39-
return Thread::whereNull('solution_reply_id')
28+
$latestThreads = Cache::remember(
29+
'latestThreads',
30+
now()->addHour(),
31+
fn () => Thread::whereNull('solution_reply_id')
4032
->whereBetween('threads.created_at', [now()->subMonth(), now()])
4133
->unlocked()
4234
->inRandomOrder()
4335
->limit(3)
44-
->get();
45-
});
36+
->get()
37+
);
4638

47-
$latestArticles = Cache::remember('latestArticles', now()->addHour(), function () {
48-
return Article::published()
49-
->recent()
50-
->limit(4)
51-
->get();
52-
});
39+
$latestArticles = Cache::remember(
40+
'latestArticles',
41+
now()->addHour(),
42+
fn () => Article::published()->recent()->limit(4)->get()
43+
);
5344

5445
return view('home', [
5546
'communityMembers' => $communityMembers,

app/Models/User.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,26 +397,6 @@ public function toSearchableArray(): array
397397
];
398398
}
399399

400-
public function scopeWithCounts(Builder $query)
401-
{
402-
return $query->withCount([
403-
'threadsRelation as threads_count',
404-
'replyAble as replies_count',
405-
'replyAble as solutions_count' => function (Builder $query) {
406-
return $query->join('threads', 'threads.solution_reply_id', '=', 'replies.id')
407-
->where('replyable_type', 'threads');
408-
},
409-
]);
410-
}
411-
412-
public function scopeHasActivity(Builder $query)
413-
{
414-
return $query->where(function ($query) {
415-
$query->has('threadsRelation')
416-
->orHas('replyAble');
417-
});
418-
}
419-
420400
public function scopeModerators(Builder $query)
421401
{
422402
return $query->whereIn('type', [

resources/css/app.css

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,6 @@ svg .secondary {
103103
@apply outline-hidden bg-gray-100;
104104
}
105105

106-
.member:after {
107-
bottom: -1rem;
108-
content: ' ';
109-
display: block;
110-
left: 7rem;
111-
position: absolute;
112-
border-color: #f9fafb transparent transparent transparent;
113-
border-style: solid;
114-
border-width: 0.5rem;
115-
}
116-
117106
/** Choices.js **/
118107
.choices__input.choices__input--cloned {
119108
@apply hidden;

resources/views/components/community-member.blade.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<div class="shrink-0 mr-8 my-2 cursor-pointer">
2-
<x-stat-popout
3-
x-cloak
4-
x-show="active == '{{ $member->id }}'"
5-
class="w-64 absolute -mt-40 -ml-20"
6-
:user="$member"
7-
/>
8-
92
<x-avatar
103
:user="$member"
11-
class="inset-0"
4+
class="inset-0 hover:scale-120 transition-transform duration-300 ease-in-out"
125
x-on:mouseover="active = {{ $member->id }}"
136
x-on:mouseout="active = false"
147
size="xl"

resources/views/components/stat-popout.blade.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,6 @@
33
])
44

55
<div {{ $attributes->merge(['class' => 'member w-64 shadow-xl rounded-sm']) }}>
6-
<div class="flex justify-between border-b border-gray-200 p-3 bg-white rounded-t">
7-
<div class="flex flex-col items-center text-lio-500 text-center">
8-
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
9-
{{ $user->solutions_count }}
10-
</span>
11-
12-
<span class="text-sm">Solutions</span>
13-
</div>
14-
15-
<div class="flex flex-col items-center text-lio-500 text-center">
16-
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
17-
{{ $user->threads_count }}
18-
</span>
19-
20-
<span class="text-sm">Threads</span>
21-
</div>
22-
23-
<div class="flex flex-col items-center text-lio-500 text-center">
24-
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
25-
{{ $user->replies_count }}
26-
</span>
27-
28-
<span class="text-sm">Replies</span>
29-
</div>
30-
</div>
31-
326
<div class="flex flex-col bg-gray-100 p-3 text-sm rounded-b">
337
<span>{{ $user->username() }}</span>
348

0 commit comments

Comments
 (0)