Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache ALL the things #60

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function homepage()
->orderBy('published_at', 'desc')
->get();

$brands = Brand::with('translations')->get();
$categories = Category::with('translations')->get();
$brands = Brand::cached();
$categories = Category::cached();
$recent = Item::with(Item::PARTIAL_LOAD)
->whereNotNull('published_at')
->orderBy('published_at', 'desc')
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Items/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\App;

class ItemController extends Controller
{
Expand Down Expand Up @@ -55,6 +56,7 @@ public function wishlist(Item $item)
$user = auth()->user();
$attached = $user->updateWishlist($item);
$status = $attached ? 'added' : 'removed';
cache()->tags(['wishlist'])->forget($item->getKey());

return back()->withStatus(trans("ui.wishlist.{$status}", ['item' => Str::limit($item->english_name, 28)]));
}
Expand All @@ -70,6 +72,7 @@ public function closet(Item $item)
$user = auth()->user();
$attached = $user->updateCloset($item);
$status = $attached ? 'added' : 'removed';
cache()->tags(['closet'])->forget($item->getKey());

return back()->withStatus(trans("ui.closet.{$status}", ['item' => Str::limit($item->english_name, 28)]));
}
Expand Down
19 changes: 19 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Traits\ItemRelations;
use App\Models\Traits\Publishable;
use App\Models\Traits\Sluggable;
use Illuminate\Support\Facades\App;
use NumberFormatter;

/**
Expand Down Expand Up @@ -252,4 +253,22 @@ public function categories()
{
return $this->belongsToMany('App\Models\Category');
}

public function wishlist() {
$wishlist = cache()->tags(['wishlist'])->get($this->getKey());
if (!$wishlist) {
$wishlist = $this->stargazers()->count();
cache()->tags(['wishlist'])->forever($this->getKey(), $wishlist);
}
return $wishlist;
}

public function closet() {
$closet = cache()->tags(['closet'])->get($this->getKey());
if (!$closet) {
$closet = $this->owners()->count();
cache()->tags(['closet'])->forever($this->getKey(), $closet);
}
return $closet;
}
}
4 changes: 2 additions & 2 deletions resources/views/items/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ class="text-regular">{{ $item->published_at->format('jS M Y, H:i') }} UTC
<div class="row">
<div class="col p-1 list-group text-center small">
<div class="list-group-item">
<i class="fal fa-star"></i> {{ $item->stargazers()->count() }} {{ trans_choice('ui.wishlist.stargazers', $item->stargazers()->count()) }}
<i class="fal fa-star"></i> {{ $item->wishlist() }} {{ trans_choice('ui.wishlist.stargazers', $item->stargazers()->count()) }}
</div>
</div>
<div class="col p-1 list-group text-center small">
<div class="list-group-item">
<i class="fal fa-shopping-bag"></i> {{ $item->owners()->count() }} {{ trans_choice('ui.closet.owners', $item->owners()->count()) }}
<i class="fal fa-shopping-bag"></i> {{ $item->closet() }} {{ trans_choice('ui.closet.owners', $item->owners()->count()) }}
</div>
</div>
</div>
Expand Down
Loading