chore: cache model attributes (#1839)

This commit is contained in:
Phan An 2024-10-06 21:21:30 +02:00 committed by GitHub
parent f2d0d0cba8
commit ae923547ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 38 additions and 31 deletions

View file

@ -96,8 +96,9 @@ class Album extends Model
protected function hasCover(): Attribute
{
return Attribute::get(fn (): bool => $this->cover_path
&& (app()->runningUnitTests() || File::exists($this->cover_path)));
return Attribute::get(
fn (): bool => $this->cover_path && (app()->runningUnitTests() || File::exists($this->cover_path))
)->shouldCache();
}
protected function coverPath(): Attribute
@ -106,7 +107,7 @@ class Album extends Model
$cover = Arr::get($this->attributes, 'cover');
return $cover ? album_cover_path($cover) : null;
});
})->shouldCache();
}
/**
@ -115,7 +116,7 @@ class Album extends Model
*/
protected function name(): Attribute
{
return Attribute::get(static fn (string $value) => html_entity_decode($value));
return Attribute::get(static fn (string $value) => html_entity_decode($value))->shouldCache();
}
protected function thumbnailName(): Attribute
@ -128,17 +129,19 @@ class Album extends Model
$parts = pathinfo($this->cover_path);
return sprintf('%s_thumb.%s', $parts['filename'], $parts['extension']);
});
})->shouldCache();
}
protected function thumbnailPath(): Attribute
{
return Attribute::get(fn () => $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null);
return Attribute::get(fn () => $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null)
->shouldCache();
}
protected function thumbnail(): Attribute
{
return Attribute::get(fn () => $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null);
return Attribute::get(fn () => $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null)
->shouldCache();
}
/** @deprecated Only here for backward compat with mobile apps */

View file

@ -97,7 +97,8 @@ class Artist extends Model
*/
protected function name(): Attribute
{
return Attribute::get(static fn (string $value): string => html_entity_decode($value) ?: self::UNKNOWN_NAME);
return Attribute::get(static fn (string $value): string => html_entity_decode($value) ?: self::UNKNOWN_NAME)
->shouldCache();
}
/**
@ -105,12 +106,13 @@ class Artist extends Model
*/
protected function image(): Attribute
{
return Attribute::get(static fn (?string $value): ?string => artist_image_url($value));
return Attribute::get(static fn (?string $value): ?string => artist_image_url($value))->shouldCache();
}
protected function imagePath(): Attribute
{
return Attribute::get(fn (): ?string => artist_image_path(Arr::get($this->attributes, 'image')));
return Attribute::get(fn (): ?string => artist_image_path(Arr::get($this->attributes, 'image')))
->shouldCache();
}
protected function hasImage(): Attribute
@ -119,7 +121,7 @@ class Artist extends Model
$image = Arr::get($this->attributes, 'image');
return $image && (app()->runningUnitTests() || File::exists(artist_image_path($image)));
});
})->shouldCache();
}
/** @return array<mixed> */

View file

@ -36,11 +36,11 @@ class License extends Model
protected function shortKey(): Attribute
{
return Attribute::get(fn (): string => '****-' . Str::afterLast($this->key, '-'));
return Attribute::get(fn (): string => '****-' . Str::afterLast($this->key, '-'))->shouldCache();
}
protected function activatedAt(): Attribute
{
return Attribute::get(fn () => $this->instance->createdAt);
return Attribute::get(fn () => $this->instance->createdAt)->shouldCache();
}
}

View file

@ -90,7 +90,7 @@ class Playlist extends Model
protected function isSmart(): Attribute
{
return Attribute::get(fn (): bool => (bool) $this->rule_groups?->isNotEmpty());
return Attribute::get(fn (): bool => (bool) $this->rule_groups?->isNotEmpty())->shouldCache();
}
protected function ruleGroups(): Attribute
@ -101,7 +101,7 @@ class Playlist extends Model
protected function cover(): Attribute
{
return Attribute::get(static fn (?string $value): ?string => playlist_cover_url($value));
return Attribute::get(static fn (?string $value): ?string => playlist_cover_url($value))->shouldCache();
}
protected function coverPath(): Attribute
@ -110,7 +110,7 @@ class Playlist extends Model
$cover = Arr::get($this->attributes, 'cover');
return $cover ? playlist_cover_path($cover) : null;
});
})->shouldCache();
}
public function ownedBy(User $user): bool
@ -185,9 +185,9 @@ class Playlist extends Model
protected function isCollaborative(): Attribute
{
return Attribute::get(fn (): bool => !$this->is_smart
&& LicenseFacade::isPlus()
&& $this->collaborators->isNotEmpty());
return Attribute::get(
fn (): bool => !$this->is_smart && LicenseFacade::isPlus() && $this->collaborators->isNotEmpty()
)->shouldCache();
}
/** @inheritdoc */

View file

@ -34,6 +34,6 @@ class PlaylistCollaborationToken extends Model
protected function expired(): Attribute
{
return Attribute::get(fn (): bool => $this->created_at->addDays(7)->isPast());
return Attribute::get(fn (): bool => $this->created_at->addDays(7)->isPast())->shouldCache();
}
}

View file

@ -134,11 +134,6 @@ class Song extends Model
return $this->belongsTo(Album::class);
}
protected function albumArtist(): Attribute
{
return Attribute::get(fn () => $this->album?->artist);
}
public function podcast(): BelongsTo
{
return $this->belongsTo(Podcast::class);
@ -154,6 +149,11 @@ class Song extends Model
return $this->hasMany(Interaction::class);
}
protected function albumArtist(): Attribute
{
return Attribute::get(fn () => $this->album?->artist)->shouldCache();
}
protected function type(): Attribute
{
return Attribute::get(fn () => $this->podcast_id ? PlayableType::PODCAST_EPISODE : PlayableType::SONG);
@ -175,7 +175,7 @@ class Song extends Model
protected function storageMetadata(): Attribute
{
return new Attribute(
return (new Attribute(
get: function (): SongStorageMetadata {
try {
switch ($this->storage) {
@ -202,7 +202,7 @@ class Song extends Model
return LocalMetadata::make($this->path);
}
}
);
))->shouldCache();
}
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string

View file

@ -109,12 +109,14 @@ class User extends Authenticatable
protected function avatar(): Attribute
{
return Attribute::get(fn (): string => avatar_or_gravatar(Arr::get($this->attributes, 'avatar'), $this->email));
return Attribute::get(fn (): string => avatar_or_gravatar(Arr::get($this->attributes, 'avatar'), $this->email))
->shouldCache();
}
protected function hasCustomAvatar(): Attribute
{
return Attribute::get(fn (): bool => (bool) Arr::get($this->attributes, 'avatar'));
return Attribute::get(fn (): bool => (bool) Arr::get($this->attributes, 'avatar'))
->shouldCache();
}
protected function isProspect(): Attribute
@ -124,11 +126,11 @@ class User extends Authenticatable
protected function isSso(): Attribute
{
return Attribute::get(fn (): bool => License::isPlus() && $this->sso_provider);
return Attribute::get(fn (): bool => License::isPlus() && $this->sso_provider)->shouldCache();
}
protected function connectedToLastfm(): Attribute
{
return Attribute::get(fn (): bool => (bool) $this->preferences->lastFmSessionKey);
return Attribute::get(fn (): bool => (bool) $this->preferences->lastFmSessionKey)->shouldCache();
}
}