Skip to content

Commit

Permalink
feat: introducing flexible cache to improve performance (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 22, 2024
1 parent 0e6a401 commit 3ce9bb0
Show file tree
Hide file tree
Showing 55 changed files with 887 additions and 371 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ API_URL=
APP_NAME=AnimeThemes
APP_ENV=local
APP_DEBUG=true
DEBUGBAR_ENABLED=true
DEBUGBAR_EDITOR=
APP_URL=http://localhost
ASSET_URL=null
APP_KEY=
Expand Down
2 changes: 2 additions & 0 deletions .env.example-sail
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ API_URL=
APP_NAME=AnimeThemes
APP_ENV=local
APP_DEBUG=true
DEBUGBAR_ENABLED=true
DEBUGBAR_EDITOR=
APP_URL=http://localhost
ASSET_URL=null
APP_KEY=
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Discord/DiscordVideoNotificationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function handle(Collection $videos, array $fields): void
Arr::set($videoArray, Video::ATTRIBUTE_OVERLAP, $video->overlap->localize());
Arr::set($videoArray, 'animethemeentries.0.animetheme.type', $theme->type->localize());

foreach ($videoArray['animethemeentries.0.animetheme.anime.images'] as $key => $image) {
foreach (Arr::get($videoArray, 'animethemeentries.0.animetheme.anime.images') as $key => $image) {
Arr::set($videoArray, "animethemeentries.0.animetheme.anime.images.$key.facet", $anime->images->get($key)->facet->localize());
}

Expand Down
19 changes: 10 additions & 9 deletions app/Actions/Models/List/BaseStoreExternalProfileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Wiki\ExternalResource;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

/**
* Class BaseStoreExternalProfileAction.
Expand All @@ -22,18 +23,18 @@ abstract class BaseStoreExternalProfileAction
*
* @param ExternalProfileSite $profileSite
* @param array $entries
* @return void
* @return void
*/
protected function preloadResources(ExternalProfileSite $profileSite, array $entries): void
{
$externalResources = ExternalResource::query()
->where(ExternalResource::ATTRIBUTE_SITE, $profileSite->getResourceSite()->value)
->whereIn(ExternalResource::ATTRIBUTE_EXTERNAL_ID, Arr::pluck($entries, 'external_id'))
->with(ExternalResource::RELATION_ANIME)
->get()
->mapWithKeys(fn (ExternalResource $resource) => [$resource->external_id => $resource->anime]);

$this->resources = $externalResources;
$this->resources = Cache::flexible("externalprofile_resources", [60, 300], function () use ($profileSite, $entries) {
return ExternalResource::query()
->where(ExternalResource::ATTRIBUTE_SITE, $profileSite->getResourceSite()->value)
->whereIn(ExternalResource::ATTRIBUTE_EXTERNAL_ID, Arr::pluck($entries, 'external_id'))
->with(ExternalResource::RELATION_ANIME)
->get()
->mapWithKeys(fn (ExternalResource $resource) => [$resource->external_id => $resource->anime]);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Enums\Models\Wiki\AnimeSynonymType;
use App\Enums\Models\Wiki\ImageFacet;
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;
Expand All @@ -31,7 +32,7 @@ public function getSite(): ResourceSite
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany<ExternalResource, Anime> $resources
* @return static
*/
public function handle(BelongsToMany $resources): static
Expand Down Expand Up @@ -127,7 +128,7 @@ public function getSynonyms(): array

/**
* Get the available sites to backfill.
*
*
* @return array
*/
protected function getResourcesMapping(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\Models\Wiki\ApiAction;
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;
Expand All @@ -29,7 +30,7 @@ public function getSite(): ResourceSite
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany<ExternalResource, Anime> $resources
* @return static
*/
public function handle(BelongsToMany $resources): static
Expand Down Expand Up @@ -78,7 +79,7 @@ public function getResources(): array

/**
* Get the available sites to backfill.
*
*
* @return array
*/
protected function getResourcesMapping(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\Models\Wiki\ApiAction;
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Http;
Expand All @@ -28,7 +29,7 @@ public function getSite(): ResourceSite
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany<ExternalResource, Anime> $resources
* @return static
*/
public function handle(BelongsToMany $resources): static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\Models\Wiki\ApiAction;
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;
Expand All @@ -30,7 +31,7 @@ public function getSite(): ResourceSite
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany<ExternalResource, Anime> $resources
* @return static
*/
public function handle(BelongsToMany $resources): static
Expand Down
6 changes: 5 additions & 1 deletion app/Actions/Models/Wiki/ApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
namespace App\Actions\Models\Wiki;

use App\Enums\Models\Wiki\ResourceSite;
use App\Models\BaseModel;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use App\Models\Wiki\Studio;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;

Expand Down Expand Up @@ -39,7 +43,7 @@ public function __construct()
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany $resources
* @return static
*/
abstract public function handle(BelongsToMany $resources): static;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Enums\Models\Wiki\ImageFacet;
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\ExternalResource;
use App\Models\Wiki\Studio;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
Expand All @@ -28,7 +29,7 @@ public function getSite(): ResourceSite
/**
* Set the response after the request.
*
* @param BelongsToMany<ExternalResource> $resources
* @param BelongsToMany<ExternalResource, Studio> $resources
* @return static
*/
public function handle(BelongsToMany $resources): static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ public function getForm(Form $form): Form
->label(__('filament.fields.video.overlap.name'))
->helperText(__('filament.fields.video.overlap.help'))
->options(VideoOverlap::asSelectArray())
->nullable()
->rules(['nullable', new Enum(VideoOverlap::class)]),
->required()
->rules(['required', new Enum(VideoOverlap::class)]),

Select::make(Video::ATTRIBUTE_SOURCE)
->label(__('filament.fields.video.source.name'))
->helperText(__('filament.fields.video.source.help'))
->options(VideoSource::asSelectArray())
->nullable()
->rules(['nullable', new Enum(VideoSource::class)]),
->required()
->rules(['required', new Enum(VideoSource::class)]),
],
)
),
Expand Down
9 changes: 7 additions & 2 deletions app/Filament/Tabs/BaseTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Filament\Resources\Components\Tab;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;

/**
* Class BaseTab.
Expand Down Expand Up @@ -41,7 +42,9 @@ public function getLabel(): string
*/
public function modifyQuery(Builder $query): Builder
{
return $this->modifyQuery($query);
return Cache::flexible("filament_query_{$this->getKey()}", [15, 60], function () use ($query) {
return $this->modifyQuery($query);
});
}

/**
Expand All @@ -51,7 +54,9 @@ public function modifyQuery(Builder $query): Builder
*/
public function getBadge(): int
{
return $this->getBadge();
return Cache::flexible("filament_badge_{$this->getKey()}", [15, 60], function () {
return $this->getBadge();
});
}

/**
Expand Down
13 changes: 8 additions & 5 deletions app/Filament/Widgets/BaseChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Flowframe\Trend\Trend;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;

/**
Expand All @@ -17,16 +18,18 @@ class BaseChartWidget extends ApexChartWidget

/**
* Get the resources count created per month.
*
*
* @param class-string $model
* @return Collection
*/
protected function perMonth(string $model): Collection
{
return Trend::model($model)
->between(now()->addMonths(-11)->startOfMonth(), now()->endOfMonth())
->perMonth()
->count();
return Cache::flexible("filament_chart_$model", [300, 1200], function () use ($model) {
return Trend::model($model)
->between(now()->addMonths(-11)->startOfMonth(), now()->endOfMonth())
->perMonth()
->count();
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Admin/ActionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
* Class ActionLog.
*
*
* @property int $id
* @property string $batch_id
* @property string $name
Expand Down Expand Up @@ -145,7 +145,7 @@ public function actionable(): MorphTo
/**
* Get the user that initiated the action.
*
* @return BelongsTo<User, ActionLog>
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Admin/FeaturedTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getSubtitle(): string
/**
* Get the user that recommended the featured theme.
*
* @return BelongsTo<User, FeaturedTheme>
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
Expand All @@ -144,7 +144,7 @@ public function user(): BelongsTo
/**
* Get the entry for the featured video.
*
* @return BelongsTo<AnimeThemeEntry, FeaturedTheme>
* @return BelongsTo<AnimeThemeEntry, $this>
*/
public function animethemeentry(): BelongsTo
{
Expand All @@ -154,7 +154,7 @@ public function animethemeentry(): BelongsTo
/**
* Get the video to feature.
*
* @return BelongsTo<Video, FeaturedTheme>
* @return BelongsTo<Video, $this>
*/
public function video(): BelongsTo
{
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function getFilamentAvatarUrl(): string
/**
* Get the playlists that belong to the user.
*
* @return HasMany<Playlist>
* @return HasMany<Playlist, $this>
*/
public function playlists(): HasMany
{
Expand All @@ -231,7 +231,7 @@ public function playlists(): HasMany
/**
* Get the playlists that belong to the user.
*
* @return HasMany<ExternalProfile>
* @return HasMany<ExternalProfile, $this>
*/
public function externalprofiles(): HasMany
{
Expand All @@ -241,7 +241,7 @@ public function externalprofiles(): HasMany
/**
* Get the action logs that the user has executed.
*
* @return HasMany<ActionLog>
* @return HasMany<ActionLog, $this>
*/
public function actionlogs(): HasMany
{
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Discord/DiscordThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getSubtitle(): string
/**
* Gets the anime that the thread uses.
*
* @return BelongsTo<Anime, DiscordThread>
* @return BelongsTo<Anime, $this>
*/
public function anime(): BelongsTo
{
Expand Down
4 changes: 2 additions & 2 deletions app/Models/List/External/ExternalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getRouteKeyName(): string
/**
* Get the anime that owns the user external entry.
*
* @return BelongsTo<Anime, ExternalEntry>
* @return BelongsTo<Anime, $this>
*/
public function anime(): BelongsTo
{
Expand All @@ -143,7 +143,7 @@ public function anime(): BelongsTo
/**
* Get the user profile that owns the user profile.
*
* @return BelongsTo<ExternalProfile, ExternalEntry>
* @return BelongsTo<ExternalProfile, $this>
*/
public function externalprofile(): BelongsTo
{
Expand Down
2 changes: 1 addition & 1 deletion app/Models/List/External/ExternalToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getRouteKeyName(): string
/**
* Get the external profile that owns the external token.
*
* @return BelongsTo<ExternalProfile, ExternalToken>
* @return BelongsTo<ExternalProfile, $this>
*/
public function externalprofile(): BelongsTo
{
Expand Down
Loading

0 comments on commit 3ce9bb0

Please sign in to comment.