Skip to content

Commit

Permalink
[apigee#430] Add configurable cache max age for edge entity list builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn committed Jul 7, 2020
1 parent 8866595 commit 5de2a77
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,13 @@ public function pageTitle(): TranslatableMarkup {
return $this->t('@team_apps', ['@team_apps' => $this->entityType->getCollectionLabel()]);
}

/**
* {@inheritdoc}
*/
protected function getCacheMaxAge() {
return $this->configFactory
->get('apigee_edge_teams.team_app_settings')
->get('cache_expiration');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,13 @@ public function pageTitle(): TranslatableMarkup {
return $title;
}

/**
* {@inheritdoc}
*/
protected function getCacheMaxAge() {
return $this->configFactory
->get('apigee_edge.developer_app_settings')
->get('cache_expiration');
}

}
27 changes: 21 additions & 6 deletions src/Entity/ListBuilder/EdgeEntityListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,21 @@ protected function buildEntityIdQuery(): QueryInterface {
* {@inheritdoc}
*/
public function render() {
$build = parent::render();

$settings = $this->getDisplaySettings();
if ($this->usingDisplayType(static::VIEW_MODE_DISPLAY_TYPE)) {
return $this->renderUsingViewMode($settings['view_mode']);
$build = $this->renderUsingViewMode($settings['view_mode']);
}

return parent::render();
// Add cache contexts.
$build['#cache'] = [
'contexts' => $this->entityType->getListCacheContexts(),
'tags' => $this->entityType->getListCacheTags(),
'max-age' => $this->getCacheMaxAge(),
];

return $build;
}

/**
Expand All @@ -147,10 +156,6 @@ protected function renderUsingViewMode($view_mode): array {
'#entities' => $this->load(),
'#entity_type' => $this->entityType,
'#view_mode' => $view_mode,
'#cache' => [
'contexts' => $this->entityType->getListCacheContexts(),
'tags' => $this->entityType->getListCacheTags(),
]
];
}

Expand Down Expand Up @@ -185,4 +190,14 @@ protected function getDisplaySettings(): array {
->getRawData();
}

/**
* Returns the cache max age.
*
* @return int
* The cache max age.
*/
protected function getCacheMaxAge() {
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{#
/**
* @file
* GET /v1/organizations/{org_name}/developers/{developer_email_or_id}/apps?expand=false
*
* Response Code: 200
*
* Variables:
* - org_name: The name of the org.
* - developer_email_or_id: The developer email or id.
*/
#}
[
{% for app in apps %}
"{{ app.name }}"{{ loop.last ? '' : ',' }}
{% endfor %}
]


42 changes: 41 additions & 1 deletion tests/src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
namespace Drupal\Tests\apigee_edge\Kernel\Entity\ListBuilder;

use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

/**
* Tests for EntityListBuilder.
Expand Down Expand Up @@ -161,8 +162,47 @@ public function testDisplaySettings() {

// Using view mode.
$build = $entity_type_manager->getListBuilder(static::ENTITY_TYPE)->render();
dump($build);
static::assertSame('apigee_entity_list', $build['#type']);
static::assertSame('foo', $build['#view_mode']);
}

/**
* Tests configurable cache max-age for entity list builders.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function testCacheSettings() {
$this->app = $this->createDeveloperApp();

$this->setCurrentUser($this->account);
$url = Url::fromRoute('entity.developer_app.collection_by_developer', ['user' => $this->account->id()]);
$request = Request::create($url->toString(), 'GET');

$this->stack->queueMockResponse([
'get_developer_apps_names' => [
'apps' => [$this->app],
],
]);
$this->queueDeveloperResponse($this->account);
/** @var \Drupal\Core\Render\HtmlResponse $response */
$response = $this->container->get('http_kernel')->handle($request);
$this->assertEqual($response->getCacheableMetadata()->getCacheMaxAge(), 900);

// Update the config.
$config = $this->config('apigee_edge.' . static::ENTITY_TYPE . '_settings');
$config->set('cache_expiration', 100)
->save();

$this->stack->queueMockResponse([
'get_developer_apps_names' => [
'apps' => [$this->app],
],
]);
$this->queueDeveloperResponse($this->account);
/** @var \Drupal\Core\Render\HtmlResponse $response */
$response = $this->container->get('http_kernel')->handle($request);
$this->assertEqual($response->getCacheableMetadata()->getCacheMaxAge(), 100);
}

}

0 comments on commit 5de2a77

Please sign in to comment.