Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 12 additions & 2 deletions src/API/Cachers/DefaultCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ class DefaultCacher extends AbstractCacher
*/
public function get(Request $request)
{
return Cache::get($this->getCacheKey($request));
$cached = Cache::get($this->getCacheKey($request));

if (! is_array($cached)) {
return null;
}

return new JsonResponse($cached['content'], $cached['status'], $cached['headers'], json: true);
}

/**
Expand All @@ -25,7 +31,11 @@ public function put(Request $request, JsonResponse $response)
{
$key = $this->trackEndpoint($request);

Cache::put($key, $response, $this->cacheExpiry());
Cache::put($key, [
'content' => $response->getContent(),
'status' => $response->getStatusCode(),
'headers' => $response->headers->all(),
], $this->cacheExpiry());
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/GraphQL/ResponseCache/DefaultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\GraphQL\ResponseCache;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
Expand All @@ -12,7 +13,13 @@ class DefaultCache implements ResponseCache
{
public function get(Request $request)
{
return Cache::get($this->getCacheKey($request));
$cached = Cache::get($this->getCacheKey($request));

if (! is_array($cached)) {
return null;
}

return new JsonResponse($cached['content'], $cached['status'], $cached['headers'], json: true);
}

public function put(Request $request, $response)
Expand All @@ -21,7 +28,11 @@ public function put(Request $request, $response)

$ttl = Carbon::now()->addMinutes((int) config('statamic.graphql.cache.expiry', 60));

Cache::put($key, $response, $ttl);
Cache::put($key, [
'content' => $response->getContent(),
'status' => $response->getStatusCode(),
'headers' => $response->headers->all(),
], $ttl);
}

protected function track($request)
Expand Down
7 changes: 2 additions & 5 deletions src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,11 @@ protected function registerSerializableClasses(array $classes)
{
$existing = $this->app['config']->get('cache.serializable_classes');

if ($existing === true) {
if ($existing === null || $existing === true) {
return;
}

$this->app['config']->set('cache.serializable_classes', array_merge(
is_array($existing) ? $existing : [],
$classes
));
$this->app['config']->set('cache.serializable_classes', array_merge(is_array($existing) ? $existing : [], $classes));
}

protected function schedule(Schedule $schedule)
Expand Down
13 changes: 5 additions & 8 deletions src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@ private function registerSerializableClasses()
{
$existing = $this->app['config']->get('cache.serializable_classes');

if ($existing === true) {
if ($existing === null || $existing === true) {
return;
}

$classes = [
$this->app['config']->set('cache.serializable_classes', array_merge(is_array($existing) ? $existing : [], [
\Statamic\Auth\File\User::class,
\Statamic\Auth\File\Passkey::class,
\Statamic\Auth\Eloquent\Passkey::class,
\Statamic\Assets\Asset::class,
\Statamic\Assets\AssetContainer::class,
\Statamic\Entries\Collection::class,
Expand All @@ -264,12 +266,7 @@ private function registerSerializableClasses()
\Carbon\Carbon::class,
\Illuminate\Support\Carbon::class,
\Illuminate\Support\Collection::class,
];

$this->app['config']->set('cache.serializable_classes', array_merge(
is_array($existing) ? $existing : [],
$classes
));
]));
}

protected function registerMiddlewareGroup()
Expand Down
6 changes: 5 additions & 1 deletion tests/API/CacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public function it_caches_endpoint_using_default_cacher()
$this->assertEquals([$cacheKey], Cache::get('api-cache:tracked-responses'));

// manually manipulate whats in the cache so we can be sure it uses it.
Cache::put($cacheKey, response()->json(['foo' => 'bar']), 10);
Cache::put($cacheKey, [
'content' => json_encode(['foo' => 'bar']),
'status' => 200,
'headers' => ['content-type' => ['application/json']],
], 10);

$this->get($endpoint)
->assertOk()
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/GraphQL/RequestCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ public function it_ignores_configured_events()
], $requests->all());
}

#[Test]
public function it_caches_response_data_as_primitives()
{
app()->instance('request-tracking', $requests = Collection::make());

$this->post('/graphql', ['query' => '{one}']);

$key = 'gql-cache:'.md5('{one}').'_'.md5(json_encode(null));
$cached = Cache::get($key);

$this->assertIsArray($cached);
$this->assertArrayHasKey('content', $cached);
$this->assertArrayHasKey('status', $cached);
$this->assertArrayHasKey('headers', $cached);
$this->assertEquals(200, $cached['status']);
}

#[Test]
public function it_can_disable_caching()
{
Expand Down
Loading