-
-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathRouteCacheManager.php
More file actions
186 lines (158 loc) · 7.37 KB
/
RouteCacheManager.php
File metadata and controls
186 lines (158 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
namespace App\Metadata\Cache;
use App\Contracts\Http\Requests\RequestAttribute;
use App\Enum\CacheTag;
use App\Facades\Helpers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
final readonly class RouteCacheManager
{
public const REQUEST = 'REQ:';
public const USER = '|USR:';
public const EXTRA = '|EXT:';
public const ONLY_WITH_EXTRA = 1;
public const ONLY_WITHOUT_EXTRA = 2;
/** @var array<string,false|RouteCacheConfig> */
public readonly array $cache_list;
/**
* Initalize the cache list.
*
* @return void
*/
public function __construct()
{
$this->cache_list = [
'api/v2/Album' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true, extra: [RequestAttribute::ALBUM_ID_ATTRIBUTE]),
'api/v2/Album::getTargetListAlbums' => false, // TODO: cache me later.
'api/v2/Albums' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true),
'api/v2/Auth::config' => new RouteCacheConfig(tag: CacheTag::SETTINGS, user_dependant: true),
'api/v2/Auth::rights' => new RouteCacheConfig(tag: CacheTag::SETTINGS, user_dependant: true),
'api/v2/Auth::user' => new RouteCacheConfig(tag: CacheTag::USER, user_dependant: true),
// We do not want to cache diagnostics errors and config as they are a debugging tool. The MUST represent the state of Lychee at any time.
'api/v2/Diagnostics' => false,
'api/v2/Diagnostics::config' => false,
'api/v2/Diagnostics::info' => false,
'api/v2/Diagnostics::permissions' => false,
// We can cache the space computation because it is not changing often and very computationally heavy.
'api/v2/Diagnostics::space' => new RouteCacheConfig(tag: CacheTag::SETTINGS, user_dependant: true),
// Response must be different for each call.
'api/v2/Frame' => false,
'api/v2/Gallery::Footer' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Gallery::Init' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Gallery::getLayout' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Gallery::getUploadLimits' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Jobs' => false, // TODO: fix me later
'api/v2/LandingPage' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
// We do not need to cache those.
'api/v2/Maintenance::cleaning' => false,
'api/v2/Maintenance::fullTree' => false,
'api/v2/Maintenance::genSizeVariants' => false,
'api/v2/Maintenance::jobs' => false,
'api/v2/Maintenance::missingFileSize' => false,
'api/v2/Maintenance::tree' => false,
'api/v2/Maintenance::update' => false,
'api/v2/Maintenance::countDuplicates' => false,
'api/v2/Maintenance::searchDuplicates' => false,
'api/v2/Maintenance::statisticsIntegrity' => false,
'api/v2/Map' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true, extra: [RequestAttribute::ALBUM_ID_ATTRIBUTE]),
'api/v2/Map::provider' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Oauth' => new RouteCacheConfig(tag: CacheTag::USER, user_dependant: true),
'api/v2/Oauth::providers' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/WebAuthn' => new RouteCacheConfig(tag: CacheTag::USER, user_dependant: true),
// Response must be different for each call.
'api/v2/Photo::random' => false,
// Ideally we should cache the search results, unfortunately it is not clear how to handle the pagination and the parts of the query.
// Furthermore the result of the serach depends of the user. Making the caching strategy more complex.
// TODO: how to support pagination ?? new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true, extra: ['album_id', 'terms']),
'api/v2/Search' => false,
'api/v2/Search::init' => new RouteCacheConfig(tag: CacheTag::SETTINGS),
'api/v2/Settings' => new RouteCacheConfig(tag: CacheTag::SETTINGS, user_dependant: true),
'api/v2/Settings::init' => 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]),
'api/v2/Sharing::all' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true),
'api/v2/Sharing::albums' => new RouteCacheConfig(tag: CacheTag::GALLERY, user_dependant: true),
'api/v2/Statistics::albumSpace' => new RouteCacheConfig(tag: CacheTag::STATISTICS, user_dependant: true),
'api/v2/Statistics::sizeVariantSpace' => new RouteCacheConfig(tag: CacheTag::STATISTICS, user_dependant: true),
'api/v2/Statistics::totalAlbumSpace' => new RouteCacheConfig(tag: CacheTag::STATISTICS, user_dependant: true),
'api/v2/Statistics::getCountsOverTime' => new RouteCacheConfig(tag: CacheTag::STATISTICS, user_dependant: true),
'api/v2/Statistics::userSpace' => new RouteCacheConfig(tag: CacheTag::STATISTICS, user_dependant: true),
'api/v2/UserManagement' => new RouteCacheConfig(tag: CacheTag::USERS, user_dependant: true),
'api/v2/Users' => new RouteCacheConfig(tag: CacheTag::USERS, user_dependant: true),
'api/v2/Users::count' => new RouteCacheConfig(tag: CacheTag::USERS, user_dependant: true),
'api/v2/Version' => false,
'api/v2/ChangeLogs' => false,
// This is returning a stream, we do not cache it.
'api/v2/Zip' => false,
// No point in caching this.
'api/v2/Metrics' => false,
];
}
public function get_config(string $uri): RouteCacheConfig|false
{
if (!array_key_exists($uri, $this->cache_list)) {
Log::warning('ResponseCache: No cache config for ' . $uri);
return false;
}
return $this->cache_list[$uri];
}
public function get_key(Request $request, RouteCacheConfig $config): string
{
$key = self::REQUEST . Helpers::getUriWithQueryString($request);
// 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 . Auth::id();
}
return $key;
}
/**
* Return the tag associated to the route if there is one.
* Return false if there is no tag for this route or if the route is not cached (just safe precaution).
*
* @param string $uri
*
* @return CacheTag|false
*/
public function get_tag(string $uri): CacheTag|false
{
if (!array_key_exists($uri, $this->cache_list)) {
return false;
}
if ($this->cache_list[$uri] === false) {
return false;
}
return $this->cache_list[$uri]->tag;
}
/**
* Given a tag, return all the routes associated to this tag.
*
* @param CacheTag $tag
* @param int $flag composed of RouteCacheManager::ONLY_WITH_EXTRA and RouteCacheManager::ONLY_WITHOUT_EXTRA
*
* @return string[]
*/
public function retrieve_routes_for_tag(CacheTag $tag, int $flag): array
{
$routes = [];
foreach ($this->cache_list as $uri => $value) {
if (
$value !== false &&
$value->tag === $tag &&
// Either with ONLY_WITH_EXTRA flag not set => ignore condition
// Or with ONLY_WITH_EXTRA flag set and we have extra parameters => ignore condition
(($flag & self::ONLY_WITH_EXTRA) === 0 || count($value->extra) > 0) &&
(($flag & self::ONLY_WITHOUT_EXTRA) === 0 || count($value->extra) === 0)
) {
$routes[] = $uri;
}
}
return $routes;
}
}