From 9649bec01e6ea755e97e4102dfad749af31bc571 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 19 Aug 2020 15:32:45 +0000 Subject: [PATCH] [MEDIA][CACHE] Cache avatar queries and delete stale values; small refactoring --- .../Media/Exception/NoAvatarException.php | 28 +++++++++++++++++++ src/Controller/UserPanel.php | 3 +- src/Entity/File.php | 2 +- src/Entity/GSActor.php | 12 ++++++-- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 components/Media/Exception/NoAvatarException.php diff --git a/components/Media/Exception/NoAvatarException.php b/components/Media/Exception/NoAvatarException.php new file mode 100644 index 0000000000..0c78246a16 --- /dev/null +++ b/components/Media/Exception/NoAvatarException.php @@ -0,0 +1,28 @@ +. + +// }}} + +namespace Component\Media\Exception; + +use Exception; + +class NoAvatarException extends Exception +{ +} diff --git a/src/Controller/UserPanel.php b/src/Controller/UserPanel.php index 00d3ea41cb..23291d8d57 100644 --- a/src/Controller/UserPanel.php +++ b/src/Controller/UserPanel.php @@ -35,7 +35,6 @@ namespace App\Controller; // {{{ Imports -use App\Core\Cache; use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; @@ -160,7 +159,7 @@ class UserPanel extends AbstractController if ($old_file != null) { @unlink($old_file); } - Cache::delete('avatar-' . $actor->getNickname()); + Event::handle('delete_cached_avatar', [$actor->getNickname()]); } return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()]; diff --git a/src/Entity/File.php b/src/Entity/File.php index 5fdec34f4e..daa4a03dd7 100644 --- a/src/Entity/File.php +++ b/src/Entity/File.php @@ -238,7 +238,7 @@ class File extends Entity ], 'primary key' => ['id'], 'unique keys' => [ - 'file_file_key' => ['file_hash', 'actor_id'], + // 'file_file_key' => ['file_hash', 'actor_id'], ], 'indexes' => [ 'file_filehash_idx' => ['file_hash'], diff --git a/src/Entity/GSActor.php b/src/Entity/GSActor.php index d91060ae71..cc5e44e0f8 100644 --- a/src/Entity/GSActor.php +++ b/src/Entity/GSActor.php @@ -205,17 +205,23 @@ class GSActor extends Entity public static function getFromId(int $id): ?self { - return DB::find('gsactor', ['id' => $id]); + return Cache::get('gsactor-id-' . $id, function () use ($id) { + return DB::find('gsactor', ['id' => $id]); + }); } public static function getFromNickname(string $nickname): ?self { - return DB::findOneBy('gsactor', ['nickname' => $nickname]); + return Cache::get('gsactor-nick-' . $nickname, function () use ($nickname) { + return DB::findOneBy('gsactor', ['nickname' => $nickname]); + }); } public static function getNicknameFromId(int $id): string { - return self::getFromId($id)->getNickname(); + return Cache::get('gsactor-nick-id-' . $id, function () use ($id) { + return self::getFromId($id)->getNickname(); + }); } public function getSelfTags(): array