diff --git a/app/Models/Album.php b/app/Models/Album.php index 4f2d7a4a..8493e194 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -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 */ diff --git a/app/Models/Artist.php b/app/Models/Artist.php index 1cf25989..58627674 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -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 */ diff --git a/app/Models/License.php b/app/Models/License.php index 74229dc5..b4b9a71a 100644 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -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(); } } diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index 0cfd14c4..086b8901 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -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 */ diff --git a/app/Models/PlaylistCollaborationToken.php b/app/Models/PlaylistCollaborationToken.php index a80439d0..8f25a1f9 100644 --- a/app/Models/PlaylistCollaborationToken.php +++ b/app/Models/PlaylistCollaborationToken.php @@ -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(); } } diff --git a/app/Models/Song.php b/app/Models/Song.php index 55c97a89..a1af9ea4 100644 --- a/app/Models/Song.php +++ b/app/Models/Song.php @@ -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 diff --git a/app/Models/User.php b/app/Models/User.php index 583cd769..0dec136e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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(); } }