Skip to content

Commit

Permalink
fix cache clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Dec 30, 2024
1 parent c63d7c2 commit adf0a70
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
5 changes: 0 additions & 5 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ private function updateProtectionPolicyBase(
*/
public function delete(DeleteAlbumsRequest $request, Delete $delete): void
{
Album::select('parent_id')
->whereIn('id', $request->albumIds())
->get()
->each(fn (Album $album) => AlbumRouteCacheUpdated::dispatch($album->parent_id ?? ''));

$fileDeleter = $delete->do($request->albumIds());
App::terminating(fn () => $fileDeleter->do());
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/Caching/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ private function cacheWithTags(Request $request, \Closure $next, RouteCacheConfi
{
$key = $this->route_cache_manager->get_key($request, $config);

return Cache::tags([$config->tag])->remember($key, Configs::getValueAsInt('cache_ttl'), fn () => $next($request));
return Cache::tags([$config->tag->value])->remember($key, Configs::getValueAsInt('cache_ttl'), fn () => $next($request));
}
}
6 changes: 3 additions & 3 deletions app/Listeners/AlbumCacheCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use App\Enum\SmartAlbumType;
use App\Events\AlbumRouteCacheUpdated;
use App\Metadata\Cache\RouteCacheManager;
use App\Models\BaseAlbumImpl;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class AlbumCacheCleaner
{
Expand Down Expand Up @@ -73,8 +73,8 @@ private function handleAllAlbums(): void
{
// The long way.
$cached_routes_with_extra = $this->route_cache_manager->retrieve_keys_for_tag(CacheTag::GALLERY, with_extra: true);
BaseAlbumImpl::select('id')->get()->each(function (BaseAlbumImpl $album) use ($cached_routes_with_extra) {
$extra = ['album_id' => $album->id];
DB::table('base_albums')->select('id')->pluck('id')->each(function ($id) use ($cached_routes_with_extra) {
$extra = ['album_id' => $id];
foreach ($cached_routes_with_extra as $route) {
$cache_key = $this->route_cache_manager->gen_key(uri: $route, extras: $extra);
Cache::forget($cache_key);
Expand Down
7 changes: 4 additions & 3 deletions app/Metadata/Cache/RouteCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enum\CacheTag;
use App\Exceptions\Internal\LycheeLogicException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

final readonly class RouteCacheManager
Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct()
'api/v2/Photo::random' => false,

'api/v2/Search' => false, // TODO: how to support pagination ?? new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true, extra: ['album_id', 'terms']),
'api/v2/Search::init' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Search::init' => false,
'api/v2/Settings' => new RouteCacheConfig(tag: CacheTag::SETTINGS, user_dependant: true),
'api/v2/Settings::getLanguages' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Sharing' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true, extra: [RequestAttribute::ALBUM_ID_ATTRIBUTE]),
Expand Down Expand Up @@ -103,14 +104,14 @@ public function get_key(Request $request, RouteCacheConfig $config): string
// If the request is user dependant, we add the user id to the key.
// That way we ensure that this does not contaminate between logged in and looged out users.
if ($config->user_dependant) {
$key .= self::USER . $request->user?->getId();
$key .= self::USER . Auth::id();
}

if (count($config->extra) > 0) {
$key .= self::EXTRA;
foreach ($config->extra as $extra) {
/** @var string $vals */
$vals = $request->query($extra) ?? '';
$vals = $request->input($extra) ?? '';
$key .= ':' . $vals;
}
}
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<env name="LYCHEE_UPLOADS_DIR" value="uploads/"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value="database/database.sqlite"/>
<env name="DB_LOG_SQL" value="true"/>
<env name="DB_LOG_SQL_EXPLAIN" value="true"/>
<env name="DB_LOG_SQL" value="false"/>
<env name="DB_LOG_SQL_EXPLAIN" value="false"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
4 changes: 4 additions & 0 deletions tests/Feature_v2/Statistics/AlbumSpaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@

namespace Tests\Feature_v2\Statistics;

use App\Models\Configs;
use LycheeVerify\Http\Middleware\VerifySupporterStatus;
use Tests\Feature_v2\Base\BaseApiV2Test;

class AlbumSpaceTest extends BaseApiV2Test
{
public function testAlbumSpaceTestUnauthorized(): void
{
Configs::set('cache_enabled', '0');
Configs::invalidateCache();

$response = $this->getJson('Statistics::albumSpace');
$this->assertSupporterRequired($response);

Expand Down

0 comments on commit adf0a70

Please sign in to comment.