Skip to content

Commit

Permalink
Cache ALL the things
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Jul 24, 2024
1 parent 4e70468 commit 903daed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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

0 comments on commit 903daed

Please sign in to comment.