Skip to content

Builtin cache invalidation system aka make API Platform fast as hell #952

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

Merged
merged 1 commit into from
May 23, 2017
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
3 changes: 2 additions & 1 deletion behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ default:
- 'FeatureContext': { doctrine: '@doctrine' }
- 'HydraContext'
- 'SwaggerContext'
- 'HttpCacheContext'
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
- 'Behatch\Context\JsonContext'
Expand All @@ -25,11 +26,11 @@ default:
coverage:
suites:
default:
contexts:
contexts:
- 'FeatureContext': { doctrine: '@doctrine' }
- 'HydraContext'
- 'SwaggerContext'
- 'HttpCacheContext'
- 'CoverageContext'
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"doctrine/orm": "^2.5",
"doctrine/annotations": "^1.2",
"friendsofsymfony/user-bundle": "^2.0",
"guzzlehttp/guzzle": "^6.0",
"nelmio/api-doc-bundle": "^2.11.2",
"php-mock/php-mock-phpunit": "^1.1",
"phpdocumentor/reflection-docblock": "^3.0",
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"suggest": {
"friendsofsymfony/user-bundle": "To use the FOSUserBundle bridge.",
"guzzlehttp/guzzle": "To use the HTTP cache invalidation system.",
"phpdocumentor/reflection-docblock": "To support extracting metadata from PHPDoc.",
"psr/cache-implementation": "To use metadata caching.",
"symfony/cache": "To have metadata caching when using Symfony integration.",
Expand Down
48 changes: 48 additions & 0 deletions features/bootstrap/HttpCacheContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use Behat\Symfony2Extension\Context\KernelAwareContext;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* @author Kévin Dunglas <[email protected]>
*/
class HttpCacheContext implements KernelAwareContext
{
/**
* @var KernelInterface
*/
private $kernel;

public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @Then ":iris" IRIs should be purged
*/
public function irisShouldBePurged(string $iris)
{
$purger = $this->kernel->getContainer()->get('api_platform.http_cache.purger');

$purgedIris = implode(',', $purger->getIris());
$purger->clear();

if ($iris !== $purgedIris) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf('IRIs "%s" does not match expected "%s".', $purgedIris, $iris)
);
}
}
}
13 changes: 13 additions & 0 deletions features/http_cache/headers.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Default values of HTTP cache headers
In order to make API responses caheable
As an API software developer
I need to be able to set default cache headers values

@createSchema
@dropSchema
Scenario: Cache headers default value
When I send a "GET" request to "/relation_embedders"
Then the response status code should be 200
And the header "Etag" should be equal to '"21248afbca1f242fd3009ac7cdf13293"'
And the header "Cache-Control" should be equal to "max-age=60, public, s-maxage=3600"
And the header "Vary" should be equal to "Content-Type, Cookie"
104 changes: 104 additions & 0 deletions features/http_cache/tags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Feature: Cache invalidation through HTTP Cache tags
In order to have a fast API
As an API software developer
I need to store API responses in a cache

@createSchema
Scenario: Create some embedded resources
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/relation_embedders" with body:
"""
{
"anotherRelated": {
"name": "Related",
"thirdLevel": {}
}
}
"""
Then the response status code should be 201
And the header "Cache-Tags" should not exist
And "/relation_embedders,/related_dummies,/third_levels" IRIs should be purged

Scenario: Tags must be set for items
When I send a "GET" request to "/relation_embedders/1"
Then the response status code should be 200
And the header "Cache-Tags" should be equal to "/relation_embedders/1,/related_dummies/1,/third_levels/1"

Scenario: Create some more resources
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/relation_embedders" with body:
"""
{
"anotherRelated": {
"name": "Another Related",
"thirdLevel": {}
}
}
"""
Then the response status code should be 201
And the header "Cache-Tags" should not exist

Scenario: Tags must be set for collections
When I send a "GET" request to "/relation_embedders"
Then the response status code should be 200
And the header "Cache-Tags" should be equal to "/relation_embedders/1,/related_dummies/1,/third_levels/1,/relation_embedders/2,/related_dummies/2,/third_levels/2,/relation_embedders"

Scenario: Purge item on update
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/relation_embedders/1" with body:
"""
{
"paris": "France"
}
"""
Then the response status code should be 200
And the header "Cache-Tags" should not exist
And "/relation_embedders,/relation_embedders/1,/related_dummies/1" IRIs should be purged

Scenario: Purge item and the related collection on update
When I add "Content-Type" header equal to "application/ld+json"
And I send a "DELETE" request to "/relation_embedders/1"
Then the response status code should be 204
And the header "Cache-Tags" should not exist
And "/relation_embedders,/relation_embedders/1,/related_dummies/1" IRIs should be purged

Scenario: Create two Relation2
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/relation2s" with body:
"""
{
}
"""
And I send a "POST" request to "/relation2s" with body:
"""
{
}
"""
Then the response status code should be 201

Scenario: Embedded collection must be listed in cache tags
When I send a "GET" request to "/relation2s/1"
Then the header "Cache-Tags" should be equal to "/relation2s/1"

Scenario: Create a Relation1
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/relation1s" with body:
"""
{
"relation2": "/relation2s/1"
}
"""
Then the response status code should be 201
And "/relation1s,/relation2s/1" IRIs should be purged

@dropSchema
Scenario: Update a Relation1
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/relation1s/1" with body:
"""
{
"relation2": "/relation2s/2"
}
"""
Then the response status code should be 200
And "/relation1s,/relation1s/1,/relation2s/2,/relation2s/1" IRIs should be purged
12 changes: 6 additions & 6 deletions features/main/subresource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Feature: Subresource support
And the JSON should be equal to:
"""
{
"@context": "/contexts/Answer",
"@id": "/answers/1",
"@type": "Answer",
"id": 1,
"content": "42",
"question": "/questions/1"
"@context": "\/contexts\/Answer",
"@id": "\/answers\/1",
"@type": "Answer",
"id": 1,
"content": "42",
"question": "\/questions\/1"
}
"""

Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ parameters:
excludes_analyse:
- tests/Fixtures/app/cache
ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\Routing\\Exception\\ExceptionInterface::getCode()#'
- '#Call to an undefined method Symfony\\Component\\Routing\\Exception\\ExceptionInterface::getCode\(\)#'
- '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy::[a-zA-Z0-9_]+\(\)#'
- '#Access to an undefined property Prophecy\\Prophecy\\ObjectProphecy::\$[a-zA-Z0-9_]+#'
- '#Call to an undefined method PHPUnit_Framework_MockObject_MockObject::[a-zA-Z0-9_]+\(\)#'
- '#Call to an undefined method Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata::getAssociationMappings\(\)#'

# False positives
- '#Parameter \#2 \$dqlPart of method Doctrine\\ORM\\QueryBuilder::add\(\) expects Doctrine\\ORM\\Query\\Expr\\Base, Doctrine\\ORM\\Query\\Expr\\Join\[\] given#' # Fixed in Doctrine's master
Expand Down
2 changes: 2 additions & 0 deletions src/Api/IdentifiersExtractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ApiPlatform\Core\Api;

use ApiPlatform\Core\Exception\RuntimeException;

/**
* Extracts identifiers for a given Resource according to the retrieved Metadata.
*
Expand Down
Loading