Skip to content

Commit

Permalink
Merge pull request #18 from yediyuz/cache-tag
Browse files Browse the repository at this point in the history
Fixed cache-tags
  • Loading branch information
mertasan authored Mar 31, 2024
2 parents 0e6acee + b52bcda commit 2d2dd78
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
43 changes: 27 additions & 16 deletions src/CloudflarePagesMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,47 @@

class CloudflarePagesMiddleware
{
public function handle(Request $request, Closure $next): Response
public function handle(Request $request, Closure $next, string $ttl, string $tags): Response
{
/** @var Response $response */
$response = $next($request);

if ($this->shouldCacheResponse($request, $response)) {
$ttl = $this->getCacheTTL($request);
$response->headers->add(['Cache-Control' => "max-age=$ttl, public"]);
$response->headers->remove('set-cookie');
if (! $this->shouldCacheResponse($request, $response)) {
return $response;
}

if ($this->hasCacheTags($request)) {
$tags = implode(',', $this->getCacheTags($request));
$response->headers->add(['Cache-Tags' => $tags]);
}
if (! $ttl) {
$ttl = $request->attributes->get(
CloudflareCache::TTL_ATTR,
config('cloudflare-cache.default_cache_ttl') ?? 600
);
}

return $response;
}
$response->headers->set('Cache-Control', "max-age=$ttl, public");
$request->attributes->set(CloudflareCache::TTL_ATTR, $ttl);
$response->headers->remove('set-cookie');

protected function hasCacheTags(Request $request): bool
{
return filled($request->attributes->get(CloudflareCache::TAGS_ATTR));
if ($tags = $this->getCacheTags($request, $tags)) {
$response->headers->set('Cache-Tags', implode(',', $tags));
}

return $response;
}

/**
* @return array<int, string>
*/
protected function getCacheTags(Request $request): array
protected function getCacheTags(Request $request, string $tags): array
{
return array_merge(array_unique($request->attributes->get(CloudflareCache::TAGS_ATTR, [])));
/**
* cache('tag1')
* cache(['tag1', 'tag2']).
*/
$tags = $tags ? explode(';', $tags) : [];
$tags = array_unique(array_merge($request->attributes->get(CloudflareCache::TAGS_ATTR, []), $tags));
$request->attributes->set(CloudflareCache::TAGS_ATTR, $tags);

return $tags;
}

protected function getCacheTTL(Request $request): int
Expand Down
35 changes: 11 additions & 24 deletions src/Mixins/CloudflareRouterMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,24 @@ public function cache(): Closure
$router->middlewarePriority = Arr::prepend($router->middlewarePriority, CloudflarePagesMiddleware::class);
}

$routeRegistrar = function () {
return $this->withoutMiddleware(CloudflareCache::getIgnoredMiddlewares())->middleware(CloudflarePagesMiddleware::class);
};

$registerTtl = static function ($ttl, $request = null) {
if ($ttl === null) {
return;
}

$request = $request ?? request();
$request->attributes->set(CloudflareCache::TTL_ATTR, $ttl);
$parameters = [
'ttl' => $ttl,
'tags' => $tags instanceof Closure
? ''
: collect(Arr::wrap($tags))
->where(static fn ($tag) => is_string($tag) && filled($tag))
->implode(';'),
];

$routeRegistrar = function () use ($parameters) {
return $this->withoutMiddleware(CloudflareCache::getIgnoredMiddlewares())->middleware(CloudflarePagesMiddleware::class . ":{$parameters['ttl']},{$parameters['tags']}");
};

// cache(function() { ... }
if ($tags instanceof Closure) {
$registerTtl($ttl);

return $routeRegistrar()->group($tags);
}

/**
* cache('tag1')
* cache(['tag1', 'tag2']).
*/
$tags = Arr::where(Arr::wrap($tags), static fn ($tag) => is_string($tag) && filled($tag));

$request = request();
$currentTags = $request->attributes->get(CloudflareCache::TAGS_ATTR, []);
$request->attributes->set(CloudflareCache::TAGS_ATTR, array_merge($currentTags, $tags));
$registerTtl($ttl, $request);

return $routeRegistrar();
};
}
Expand Down
26 changes: 13 additions & 13 deletions tests/Unit/CloudflareRouteMixinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
$this->assertFalse($request->attributes->has(CloudflareCache::TAGS_ATTR));
$this->assertFalse($request->attributes->has(CloudflareCache::TTL_ATTR));

Route::cache($tags, $ttl);
Route::cache($tags, $ttl)->get('/test', function () {
return 'test';
});

ray($request->attributes);

expect($request->attributes)
->has(CloudflareCache::TAGS_ATTR)
->toBeTrue()
->get(CloudflareCache::TAGS_ATTR)
->toEqual($expectedTags)
->and($request->attributes)
->match($ttl, [
null => fn ($attributes) => $attributes->has(CloudflareCache::TTL_ATTR)->toBeFalse(),
600 => fn ($attributes) => $attributes->has(CloudflareCache::TTL_ATTR)->toBeTrue(),
]);
$response = $this->get('test');
expect($response)->assertHeader('Cache-Tags', implode(',', $expectedTags));

})->with('cache_mixin_tag_types')
->with([
null,
600,
]);

test('cache tag must exist in the header only if used', function () {
$response = $this->get('content_without_tags');
$this->assertFalse($response->headers->has('Cache-Tags'));

$response = $this->get('content_in_args');
$this->assertTrue($response->headers->has('Cache-Tags'));
});

0 comments on commit 2d2dd78

Please sign in to comment.