Skip to content
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

Add caching to fallback routes #228

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
// Should the routes be registered? The controllers
// below will not be used anymore when disabled.
'routes' => true,
'fallback_routes' => [
royduin marked this conversation as resolved.
Show resolved Hide resolved
// How long (in seconds) it should cache which controller handles which route.
// null means cache forever, 0 means never cache. customisation is possible using a closure.
// This does not cache the response, it caches the controller used for that page.
'cache_duration' => 3600,
],

// Link store codes to theme folders
// The structure is `'store_code' => 'folder_path'`
Expand Down
46 changes: 36 additions & 10 deletions src/Http/Controllers/FallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,58 @@

namespace Rapidez\Core\Http\Controllers;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Rapidez\Core\Facades\Rapidez;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class FallbackController
{
public function __invoke()
public function __invoke(Request $request)
{
foreach (Rapidez::getAllFallbackRoutes() as $route) {
try {
$response = App::call($route['action']['uses']);
$cacheKey = 'fallbackroute-' . md5($request->url());
$route = Cache::get($cacheKey);
if($route && $response = $this->tryRoute($route)) {
return $response;
}

// Null response is equal to no response or 404.
if ($response === null) {
abort(404);
}
foreach (Rapidez::getAllFallbackRoutes() as $route) {
if(!($response = $this->tryRoute($route))) {
continue;
}

return $response;
} catch (RouteNotFoundException|NotFoundHttpException|BackedEnumCaseNotFoundException|ModelNotFoundException|RecordsNotFoundException $e) {
try {
Cache::put($cacheKey, $route, value(config('rapidez.fallback_routes.cache_duration', 3600), $request, $response, $route));
} catch (Exception $e) {
// We can't cache it, no worries.
// Ususally a sign it's a direct callback or caching hasn't been configured properly.
royduin marked this conversation as resolved.
Show resolved Hide resolved
}

return $response;
}

abort(404);
}

protected function tryRoute($route)
{
try {
$response = App::call($route['action']['uses']);

// Null response is equal to no response or 404.
if ($response === null) {
abort(404);
}

return $response;
} catch (RouteNotFoundException|NotFoundHttpException|BackedEnumCaseNotFoundException|ModelNotFoundException|RecordsNotFoundException $e) {
return null;
}
}
}