From 4f669d4e011ba3dd24667365a0511d87452f9276 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 4 Dec 2021 14:07:15 +0000 Subject: [PATCH] [ENTITY][NoteTag][Language] Add convinience cache getters --- src/Entity/Language.php | 5 +++++ src/Entity/NoteTag.php | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Entity/Language.php b/src/Entity/Language.php index 288ed24118..4bce24917f 100644 --- a/src/Entity/Language.php +++ b/src/Entity/Language.php @@ -126,6 +126,11 @@ class Language extends Entity ); } + public static function getFromNote(Note $note): self + { + return self::getFromId($note->getLanguageId()); + } + public static function getLanguageChoices(): array { $langs = Cache::getHashMap( diff --git a/src/Entity/NoteTag.php b/src/Entity/NoteTag.php index 9fd2575dd2..5373b0a1c2 100644 --- a/src/Entity/NoteTag.php +++ b/src/Entity/NoteTag.php @@ -110,9 +110,17 @@ class NoteTag extends Entity // @codeCoverageIgnoreEnd // }}} Autocode + public static function cacheKey(int|Note $note_id) + { + if (!\is_int($note_id)) { + $note_id = $note_id->getId(); + } + return "note-tags-{$note_id}"; + } + public static function getFromNoteId(int $note_id): array { - return Cache::getList("note-tags-{$note_id}", fn () => DB::dql('select nt from note_tag nt join note n with n.id = nt.note_id where n.id = :id', ['id' => $note_id])); + return Cache::getList(self::cacheKey($note_id), fn () => DB::dql('select nt from note_tag nt join note n with n.id = nt.note_id where n.id = :id', ['id' => $note_id])); } public function getUrl(?Actor $actor = null): string