Add clear cache via Request

This commit is contained in:
philipp lang 2024-06-10 00:13:43 +02:00
parent 14b6e22b49
commit 712fb479dc
4 changed files with 28 additions and 4 deletions

View File

@ -1,8 +1,11 @@
<?php
use Silva\Adrema\Support\ClearCache;
use Silva\Adrema\Support\Proxy;
Route::get('/adrema-api/{route}', fn (string $route) => 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());

13
support/ClearCache.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace Silva\Adrema\Support;
use Cache;
class ClearCache
{
public function run(): void
{
Cache::forget('adrema-all-events');
}
}

View File

@ -3,19 +3,17 @@
namespace Silva\Adrema\Support;
use Illuminate\Support\Collection;
use Cache;
class FetchAllEvents
{
public function run(): ?Collection
{
$events = Cache::remember('adrema-all-events', 3600, function () {
return app(Proxy::class)->run('/api/form');
});
$events = app(Proxy::class)->run('api/form');
if (!$events) {
return null;
}
return collect($events);
}
}

View File

@ -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));