Skip to content

Commit

Permalink
refactor: cut the number of queries in featured collections (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
crnkovic authored Dec 19, 2023
1 parent bb9ff82 commit 8b77be0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/Data/Collections/CollectionFeaturedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static function fromModel(Collection $collection, CurrencyCode $currency)
banner: $collection->extra_attributes->get('banner'),
openSeaSlug: $collection->extra_attributes->get('opensea_slug'),
website: $collection->website(),
nfts: GalleryNftData::collection($collection->nfts),
supply: $collection->supply,
nfts: GalleryNftData::collection($collection->cachedNfts),
isFeatured: $collection->is_featured,
description: $collection->description,
volume: $collection->volume,
Expand Down
5 changes: 4 additions & 1 deletion app/Filament/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;

class CollectionResource extends Resource
{
Expand Down Expand Up @@ -94,9 +95,11 @@ public static function table(Table $table): Table
->warning()
->send();
} else {
return $collection->update([
$collection->update([
'is_featured' => ! $collection->is_featured,
]);

Cache::forget('featured-collections');
}
})
->label(fn (Collection $collection) => $collection->is_featured ? 'Unmark as featured' : 'Mark as featured')
Expand Down
29 changes: 16 additions & 13 deletions app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,23 @@ private function getVotableCollections(Request $request): SupportCollection
*/
private function getFeaturedCollections(Request $request): SupportCollection
{
$user = $request->user();

$currency = $user ? $user->currency() : CurrencyCode::USD;

$featuredCollections = Collection::featured()
->get();

$featuredCollections->each(function (Collection $collection) {
$collection->cachedNfts = Cache::remember('featuredNftsForCollection'.$collection->id, 3600 * 12, function () use ($collection) {
return $collection->nfts()->inRandomOrder()->take(3)->get();
});
});
$currency = $request->user()?->currency() ?? CurrencyCode::USD;

$collections = Cache::remember(
'featured-collections',
now()->addHour(),
fn () => Collection::featured()
->with([
'network',
'floorPriceToken',
'nfts' => fn ($q) => $q->inRandomOrder()->limit(3),
])
->get()
);

return $collections->map(function (Collection $collection) use ($currency) {
$collection->nfts->each->setRelation('collection', $collection);

return $featuredCollections->map(function (Collection $collection) use ($currency) {
return CollectionFeaturedData::fromModel($collection, $currency);
});
}
Expand Down
21 changes: 0 additions & 21 deletions tests/App/Http/Controllers/CollectionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Models\Token;
use Carbon\Carbon;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Inertia\Testing\AssertableInertia as Assert;

it('can render the collections overview page', function () {
Expand Down Expand Up @@ -54,26 +53,6 @@
);
});

it('can cache 3 random nfts from a featured collection', function () {
$user = createUser();

$collection = Collection::factory()->create([
'is_featured' => true,
]);

$nfts = Nft::factory(10)->create([
'collection_id' => $collection->id,
]);

$this->actingAs($user)
->get(route('collections'))
->assertStatus(200);

$cachedNfts = Cache::get('featuredNftsForCollection'.$collection->id);

expect(count($cachedNfts))->toEqual(3);
});

it('can render the collections view page', function () {
$user = createUser();

Expand Down

0 comments on commit 8b77be0

Please sign in to comment.