|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers; |
4 | 4 |
|
| 5 | +use App\Models\Build; |
5 | 6 | use App\Models\Client; |
6 | 7 | use App\Models\Key; |
7 | 8 | use App\Models\Mod; |
@@ -228,27 +229,34 @@ private function fetchModpack($slug) |
228 | 229 |
|
229 | 230 | private function fetchBuild($modpackSlug, $buildName) |
230 | 231 | { |
231 | | - $modpack = Cache::remember('modpack:'.$modpackSlug, now()->addMinutes(5), function () use ($modpackSlug) { |
232 | | - return Modpack::with('builds') |
233 | | - ->where('slug', $modpackSlug) |
234 | | - ->first(); |
235 | | - }); |
| 232 | + $buildCacheKey = 'modpack:'.$modpackSlug.':build:'.$buildName; |
236 | 233 |
|
237 | | - if (! $modpack) { |
238 | | - return ['error' => 'Modpack does not exist']; |
239 | | - } |
| 234 | + $build = Cache::get($buildCacheKey); |
240 | 235 |
|
241 | | - $build = Cache::remember('modpack:'.$modpackSlug.':build:'.$buildName, |
242 | | - now()->addMinutes(5), |
243 | | - function () use ($modpack, $buildName) { |
244 | | - $build = $modpack->builds->firstWhere('version', '===', $buildName); |
| 236 | + if (! $build) { |
| 237 | + $modpack = Cache::remember('modpack:'.$modpackSlug, now()->addMinutes(5), function () use ($modpackSlug) { |
| 238 | + return Modpack::with('builds')->where('slug', $modpackSlug)->first(); |
| 239 | + }); |
245 | 240 |
|
246 | | - $build->load(['modversions', 'modversions.mod']); |
| 241 | + if (! $modpack) { |
| 242 | + return ['error' => 'Modpack does not exist']; |
| 243 | + } |
247 | 244 |
|
248 | | - return $build; |
249 | | - }); |
| 245 | + $build = $modpack->builds->firstWhere('version', '===', $buildName); |
250 | 246 |
|
251 | | - if (! $build) { |
| 247 | + if ($build) { |
| 248 | + // Cache the found build with its relationships for 5 minutes |
| 249 | + $build->load(['modversions', 'modversions.mod']); |
| 250 | + Cache::put($buildCacheKey, $build, now()->addMinutes(5)); |
| 251 | + } else { |
| 252 | + // Cache the "not found" result for 1 minute |
| 253 | + $build = Build::NOT_FOUND_CACHE_VALUE; |
| 254 | + Cache::put($buildCacheKey, $build, now()->addMinute()); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + // Handle the "not found" case |
| 259 | + if ($build === Build::NOT_FOUND_CACHE_VALUE) { |
252 | 260 | return ['error' => 'Build does not exist']; |
253 | 261 | } |
254 | 262 |
|
|
0 commit comments