From 712fb479dc918f1512d4aa180fbf7b2b765a8a81 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 10 Jun 2024 00:13:43 +0200 Subject: [PATCH] Add clear cache via Request --- routes.php | 3 +++ support/ClearCache.php | 13 +++++++++++++ support/FetchAllEvents.php | 6 ++---- support/Proxy.php | 10 ++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 support/ClearCache.php diff --git a/routes.php b/routes.php index 62c7f9b..133e009 100644 --- a/routes.php +++ b/routes.php @@ -1,8 +1,11 @@ Response::json(app(Proxy::class)->run($route, 'get'))) ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); Route::post('/adrema-api/{route}', fn (string $route) => app(Proxy::class)->submit($route)) ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); + +Route::get('/adrema/clear-cache', fn () => app(ClearCache::class)->run()); diff --git a/support/ClearCache.php b/support/ClearCache.php new file mode 100644 index 0000000..1c84c35 --- /dev/null +++ b/support/ClearCache.php @@ -0,0 +1,13 @@ +run('/api/form'); - }); + $events = app(Proxy::class)->run('api/form'); if (!$events) { return null; } + return collect($events); } } diff --git a/support/Proxy.php b/support/Proxy.php index 3cee350..4d7dd22 100644 --- a/support/Proxy.php +++ b/support/Proxy.php @@ -2,6 +2,7 @@ namespace Silva\Adrema\Support; +use Cache; use Silva\Adrema\Models\Settings; use Http; use Illuminate\Http\JsonResponse; @@ -9,6 +10,15 @@ use Illuminate\Http\JsonResponse; class Proxy { public function run(string $url): ?array + { + if ($url === 'api/form') { + return Cache::rememberForever('adrema-all-events', fn () => $this->get($url)); + } + + return $this->get($url); + } + + protected function get(string $url): ?array { $response = Http::acceptJson()->get($this->url($url));