Skip to content

Add Caching for faster response time. #2859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
use App\Image\Files\NativeLocalFile;
use App\Legacy\Actions\Photo\Create as LegacyPhotoCreate;
use App\Models\Photo;
use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pipeline\Pipeline;
use LycheeVerify\Verify;
use User;

class Create
{
Expand Down Expand Up @@ -218,6 +218,7 @@ private function executePipeOnDTO(array $pipes, VideoPartnerDTO|StandaloneDTO|Ph
// If source file could not be put into final destination, remove
// freshly created photo from DB to avoid having "zombie" entries.
try {
/** @disregard */
$dto->getPhoto()->delete();
} catch (\Throwable) {
// Sic! If anything goes wrong here, we still throw the original exception
Expand Down Expand Up @@ -317,7 +318,7 @@ private function checkQuota(NativeLocalFile $sourceFile): void
return;
}

$user = \User::find($this->strategyParameters->intendedOwnerId) ?? throw new ModelNotFoundException();
$user = User::find($this->strategyParameters->intendedOwnerId) ?? throw new ModelNotFoundException();

// User does not have quota
if ($user->quota_kb === null) {
Expand Down
20 changes: 20 additions & 0 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Exceptions\Internal\ZeroModuloException;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use function Safe\ini_get;

Expand Down Expand Up @@ -289,4 +290,23 @@ public function exceptionTraceToText(\Exception $e): string
'line' => $err['line'] ?? '?',
'function' => $err['function']])->all());
}

/**
* Given a request return the uri WITH the query paramters.
* This makes sure that we handle the case where the query parameters are empty or contains an album id or pagination.
*
* @param Request $request
*
* @return string
*/
public function getUriWithQueryString(Request $request): string
{
/** @var array<string,mixed>|null $query */
$query = $request->query();
if ($query === null || $query === []) {
return $request->path();
}

return $request->path() . '?' . http_build_query($query);
}
}
24 changes: 24 additions & 0 deletions app/Enum/CacheTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Enum;

/**
* Enum ConfigType.
*
* The most important type possibilities.
*/
enum CacheTag: string
{
case GALLERY = 'gallery';
case AUTH = 'auth';
case USER = 'user';
case SETTINGS = 'settings';
case STATISTICS = 'statistics';
case USERS = 'users';
}
35 changes: 35 additions & 0 deletions app/Events/AlbumRouteCacheUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class AlbumRouteCacheUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* This event is fired when the gallery is updated.
* Note that:
* - if $album_id is null, then all routes are to be cleared.
* - if $album_id is '', then only the root is updated.
* - if $album_id is an id, then only that id is updated.
*
* @param string|null $album_id
*
* @return void
*/
public function __construct(public ?string $album_id = null)
{
}
}
28 changes: 28 additions & 0 deletions app/Events/TaggedRouteCacheUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Events;

use App\Enum\CacheTag;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TaggedRouteCacheUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public CacheTag $tag)
{
}
}
1 change: 1 addition & 0 deletions app/Facades/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @method static int convertSize(string $size)
* @method static string decimalToDegreeMinutesSeconds(float $decimal, bool $type)
* @method static string censor(string $string, float $percentOfClear = 0.5)
* @method static string getUriWithQueryString(\Illuminate\Http\Request $request): string
*/
class Helpers extends Facade
{
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/FlushCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Http\Controllers\Admin\Maintenance;

use App\Http\Requests\Maintenance\MaintenanceRequest;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;

/**
* Sometimes the job history is a bit messed up,
* this happens when there are crashes or error in the logic.
*
* In theory this should not be needed but if this is not resolved
* the pulse feedback would always stay alive.
*/
class FlushCache extends Controller
{
/**
* Flush all the caches.
*
* @param MaintenanceRequest $_request
*
* @return void
*/
public function do(MaintenanceRequest $_request): void
{
Cache::flush();
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/FullTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace App\Http\Controllers\Admin\Maintenance;

use App\Events\AlbumRouteCacheUpdated;
use App\Http\Controllers\Admin\Maintenance\Model\Album;
use App\Http\Requests\Maintenance\FullTreeUpdateRequest;
use App\Http\Requests\Maintenance\MaintenanceRequest;
Expand All @@ -32,6 +33,8 @@ public function do(FullTreeUpdateRequest $request): void
$keyName = 'id';
$albumInstance = new Album();
batch()->update($albumInstance, $request->albums(), $keyName);

AlbumRouteCacheUpdated::dispatch();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/GenSizeVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Contracts\Models\SizeVariantFactory;
use App\Enum\SizeVariantType;
use App\Events\AlbumRouteCacheUpdated;
use App\Exceptions\MediaFileOperationException;
use App\Http\Requests\Maintenance\CreateThumbsRequest;
use App\Image\PlaceholderEncoder;
Expand Down Expand Up @@ -62,6 +63,8 @@ public function do(CreateThumbsRequest $request, SizeVariantFactory $sizeVariant
} catch (MediaFileOperationException $e) {
Log::error('Failed to create ' . $request->kind()->value . ' for photo id ' . $photo->id . '');
}

AlbumRouteCacheUpdated::dispatch();
// @codeCoverageIgnoreEnd
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/MissingFileSizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace App\Http\Controllers\Admin\Maintenance;

use App\Enum\StorageDiskType;
use App\Events\AlbumRouteCacheUpdated;
use App\Http\Requests\Maintenance\MaintenanceRequest;
use App\Models\SizeVariant;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -58,6 +59,8 @@ public function do(MaintenanceRequest $request): void
}
// @codeCoverageIgnoreEnd
}

AlbumRouteCacheUpdated::dispatch();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace App\Http\Controllers\Admin\Maintenance;

use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Http\Requests\Maintenance\RegisterRequest;
use App\Http\Resources\GalleryConfigs\RegisterData;
use App\Models\Configs;
Expand Down Expand Up @@ -37,6 +39,8 @@ public function __invoke(RegisterRequest $request): RegisterData
// Not valid, reset the key.
Configs::set('license_key', '');

TaggedRouteCacheUpdated::dispatch(CacheTag::SETTINGS);

return new RegisterData(false);
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace App\Http\Controllers\Admin;

use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Exceptions\InsufficientFilesystemPermissions;
use App\Http\Requests\Settings\GetAllConfigsRequest;
use App\Http\Requests\Settings\SetConfigsRequest;
Expand Down Expand Up @@ -55,6 +57,7 @@ public function setConfigs(SetConfigsRequest $request): ConfigCollectionResource
});

Configs::invalidateCache();
TaggedRouteCacheUpdated::dispatch(CacheTag::SETTINGS);

return new ConfigCollectionResource(Configs::orderBy('cat', 'asc')->get());
}
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/Admin/UserManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use App\Actions\Statistics\Spaces;
use App\Actions\User\Create;
use App\Actions\User\Save;
use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Exceptions\UnauthorizedException;
use App\Http\Requests\UserManagement\AddUserRequest;
use App\Http\Requests\UserManagement\DeleteUserRequest;
Expand Down Expand Up @@ -66,6 +68,8 @@ public function save(SetUserSettingsRequest $request, Save $save): void
quota_kb: $request->quota_kb(),
note: $request->note()
);

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);
}

/**
Expand All @@ -84,6 +88,8 @@ public function delete(DeleteUserRequest $request): void
throw new UnauthorizedException('You are not allowed to delete yourself');
}
$request->user2()->delete();

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);
}

/**
Expand All @@ -105,6 +111,8 @@ public function create(AddUserRequest $request, Create $create): UserManagementR
note: $request->note()
);

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);

return new UserManagementResource($user, ['id' => $user->id, 'size' => 0], $request->is_se());
}
}
Loading