[MEDIA][CACHE] Cache avatar queries and delete stale values; small refactoring

This commit is contained in:
Hugo Sales 2020-08-19 15:32:45 +00:00 committed by Hugo Sales
parent 624aef0a8e
commit 4b8e6bb198
4 changed files with 39 additions and 6 deletions

View File

@ -0,0 +1,28 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace Component\Media\Exception;
use Exception;
class NoAvatarException extends Exception
{
}

View File

@ -35,7 +35,6 @@ namespace App\Controller;
// {{{ Imports // {{{ Imports
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
@ -160,7 +159,7 @@ class UserPanel extends AbstractController
if ($old_file != null) { if ($old_file != null) {
@unlink($old_file); @unlink($old_file);
} }
Cache::delete('avatar-' . $actor->getNickname()); Event::handle('delete_cached_avatar', [$actor->getNickname()]);
} }
return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()]; return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()];

View File

@ -238,7 +238,7 @@ class File extends Entity
], ],
'primary key' => ['id'], 'primary key' => ['id'],
'unique keys' => [ 'unique keys' => [
'file_file_key' => ['file_hash', 'actor_id'], // 'file_file_key' => ['file_hash', 'actor_id'],
], ],
'indexes' => [ 'indexes' => [
'file_filehash_idx' => ['file_hash'], 'file_filehash_idx' => ['file_hash'],

View File

@ -205,17 +205,23 @@ class GSActor extends Entity
public static function getFromId(int $id): ?self public static function getFromId(int $id): ?self
{ {
return Cache::get('gsactor-id-' . $id, function () use ($id) {
return DB::find('gsactor', ['id' => $id]); return DB::find('gsactor', ['id' => $id]);
});
} }
public static function getFromNickname(string $nickname): ?self public static function getFromNickname(string $nickname): ?self
{ {
return Cache::get('gsactor-nick-' . $nickname, function () use ($nickname) {
return DB::findOneBy('gsactor', ['nickname' => $nickname]); return DB::findOneBy('gsactor', ['nickname' => $nickname]);
});
} }
public static function getNicknameFromId(int $id): string public static function getNicknameFromId(int $id): string
{ {
return Cache::get('gsactor-nick-id-' . $id, function () use ($id) {
return self::getFromId($id)->getNickname(); return self::getFromId($id)->getNickname();
});
} }
public function getSelfTags(): array public function getSelfTags(): array