Skip to content
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

WIP: Add cache tags to API queries, add cache control section backend page where the API cache for a specific section can be cleared selectively #742

Merged
merged 2 commits into from
Mar 20, 2024
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
2 changes: 2 additions & 0 deletions config/user.role.administrator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
- ghi_blocks
- ghi_content
- ghi_embargoed_access
- ghi_sections
- ghi_teams
- ghi_templates
- layout_builder
Expand Down Expand Up @@ -83,6 +84,7 @@ permissions:
- 'administer nodes'
- 'administer pages'
- 'administer permissions'
- 'administer section cache'
- 'administer social api authentication'
- 'administer social api autoposting'
- 'administer social api widgets'
Expand Down
11 changes: 11 additions & 0 deletions html/modules/custom/ghi_base_objects/src/Entity/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}

/**
* {@inheritdoc}
*/
public function getApiCacheTagsToInvalidate() {
$source_id = $this->getSourceId();
if (!$source_id) {
return [];
}
return [$this->bundle() . '_id:' . $this->getSourceId()];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ public function getCreatedTime();
*/
public function setCreatedTime($timestamp);

/**
* Returns the cache tags that should be used to invalidate API caches.
*
* @return string[]
* Set of cache tags.
*/
public function getApiCacheTagsToInvalidate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\ghi_base_objects\ApiObjects\Location;
use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;

/**
* Provides a query plugin for locations.
Expand All @@ -25,7 +24,6 @@
class LocationsQuery extends EndpointQueryBase {

use StringTranslationTrait;
use SimpleCacheTrait;

const MAX_LEVEL = 5;

Expand Down Expand Up @@ -71,14 +69,15 @@ public function getCountryLocations($country, $max_level = self::MAX_LEVEL) {
'country_id' => $country->id,
'max_level' => $max_level,
]);
$locations = $this->cache($cache_key);
$locations = $this->getCache($cache_key);
if ($locations) {
return $locations;
}

$data = $this->getCountryLocationData($country->id, $max_level);
if (empty($data) || empty($data->children) || !is_array($data->children)) {
return $this->cache($cache_key, []);
$this->setCache($cache_key, []);
return [];
}

// Make it a flat array.
Expand All @@ -97,7 +96,8 @@ public function getCountryLocations($country, $max_level = self::MAX_LEVEL) {
$locations = array_filter($locations, function ($location) {
return !empty($location->latLng[0]) && !empty($location->latLng[1]) && $location->admin_level != 0;
});
return $this->cache($cache_key, $locations);
$this->setCache($cache_key, $locations);
return $locations;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion html/modules/custom/ghi_plans/src/ApiObjects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getOrganizations() {
}
$processed_organizations[$organization->id] = new Organization($organization);
}
$this->cache($cache_key, $processed_organizations);
$this->cache($cache_key, $processed_organizations, FALSE, NULL, $this->getCacheTags());
return $processed_organizations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\ghi_plans\Traits\AttachmentFilterTrait;
use Drupal\ghi_plans\Traits\PlanVersionArgument;
use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;

/**
* Provides a query plugin for attachment search.
Expand All @@ -33,7 +32,6 @@ class AttachmentSearchQuery extends EndpointQueryBase {

use AttachmentFilterTrait;
use PlanVersionArgument;
use SimpleCacheTrait;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -66,7 +64,7 @@ public function getAttachmentsById(array $attachment_ids, $disaggregated = FALSE
$query_args['disaggregation'] = 'false';
}
$cache_key = $this->getCacheKey($query_args);
$attachments = $this->cache($cache_key);
$attachments = $this->getCache($cache_key);
if ($attachments) {
return $attachments;
}
Expand All @@ -76,7 +74,7 @@ public function getAttachmentsById(array $attachment_ids, $disaggregated = FALSE
}

$processed_attachments = AttachmentHelper::processAttachments($attachments);
$this->cache($cache_key, $processed_attachments);
$this->setCache($cache_key, $processed_attachments);
return $processed_attachments;
}

Expand Down Expand Up @@ -115,7 +113,7 @@ public function getAttachmentsByObject($object_type, $object_ids, array $filter
'object_ids' => $object_ids,
'version' => $version,
] + (array) $filter);
$attachments = $this->cache($cache_key);
$attachments = $this->getCache($cache_key);
if ($attachments) {
return $attachments;
}
Expand All @@ -137,7 +135,7 @@ public function getAttachmentsByObject($object_type, $object_ids, array $filter
}

$processed_attachments = AttachmentHelper::processAttachments($attachments);
$this->cache($cache_key, $processed_attachments);
$this->setCache($cache_key, $processed_attachments);
return $processed_attachments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function getCluster($plan_id, $cluster_id) {
* An array of cluster objects, keyed by the cluster id.
*/
public function getTaggedClustersForPlan($plan_id, $cluster_tag) {
$this->setCacheTags([
'plan_id:' . $plan_id,
]);
$clusters = $this->getData([], [
'planId' => $plan_id,
'scopes' => 'governingEntityVersion',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public function getUnprocessedMeasurements(DataAttachment $attachment, $disaggre
if (!$disaggregation) {
$endpoint_args['disaggregation'] = 'false';
}
$plan_id = $attachment->getPlanId();
if ($plan_id) {
$this->setCacheTags([
'plan_id:' . $plan_id,
]);
}
if ($this->isAutenticatedEndpoint) {
if ($plan_id = $attachment->getPlanId()) {
if ($plan_id) {
$endpoint_args['version'] = $this->getPlanVersionArgumentForPlanId($plan_id);
}
$data = $this->getData([], ['attachmentId' => $attachment->id()] + $endpoint_args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\ghi_plans\ApiObjects\Organization;
use Drupal\ghi_plans\Traits\PlanVersionArgument;
use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;

/**
* Provides a query plugin for organizations.
Expand All @@ -23,7 +22,6 @@
class OrganizationQuery extends EndpointQueryBase {

use PlanVersionArgument;
use SimpleCacheTrait;
use StringTranslationTrait;

/**
Expand All @@ -39,14 +37,14 @@ public function getOrganization($organization_id) {
$cache_key = $this->getCacheKey([
'organization_id' => $organization_id,
]);
$organization = $this->cache($cache_key);
$organization = $this->getCache($cache_key);
if ($organization !== NULL) {
return $organization;
}
$data = $this->getData(['organization_id' => $organization_id]);
$organization = !empty($data) ? new Organization($data) : NULL;

$this->cache($cache_key, $organization);
$this->setCache($cache_key, $organization);
return $organization;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\ghi_plans\ApiObjects\Plan;
use Drupal\ghi_plans\Traits\PlanVersionArgument;
use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;

/**
* Provides a query plugin for basic plan data.
Expand All @@ -28,7 +27,6 @@
class PlanBasicQuery extends EndpointQueryBase {

use PlanVersionArgument;
use SimpleCacheTrait;
use StringTranslationTrait;

/**
Expand All @@ -45,14 +43,13 @@ public function getBaseData($plan_id) {
'plan_id' => $plan_id,
'authenticated' => $this->isAutenticatedEndpoint,
]);
$base_data = $this->cache($cache_key);
$base_data = $this->getCache($cache_key);
if ($base_data !== NULL) {
return $base_data;
}
$data = $this->getData(['plan_id' => $plan_id], ['version' => $this->getPlanVersionArgumentForPlanId($plan_id)]);
$base_data = !empty($data) ? new Plan($data) : FALSE;

$this->cache($cache_key, $base_data);
$this->setCache($cache_key, $base_data);
return $base_data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\ghi_plans\Plugin\EndpointQuery;

use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;
use Drupal\hpc_common\Helpers\ArrayHelper;

/**
Expand All @@ -20,19 +19,18 @@
*/
class PlanClusterSummaryQuery extends EndpointQueryBase {

use SimpleCacheTrait;

/**
* {@inheritdoc}
*/
public function getData(array $placeholders = [], array $query_args = []) {
$cache_key = $this->getCacheKey($this->getPlaceholders() + $placeholders + $query_args);
if ($data = $this->cache($cache_key)) {
if ($data = $this->getCache($cache_key)) {
return $data;
}
$data = parent::getData($placeholders);
if (empty($data) || empty($data->objects)) {
return $this->cache($cache_key, []);
$this->setCache($cache_key, []);
return [];
}

$totals = property_exists($data, 'totals') ? $data->totals : $data;
Expand All @@ -57,7 +55,8 @@ public function getData(array $placeholders = [], array $query_args = []) {
'total_funding' => $totals->totalFunding,
],
];
return $this->cache($cache_key, $summary_data);
$this->setCache($cache_key, $summary_data);
return $summary_data;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Drupal\hpc_api\Helpers\ApiEntityHelper;
use Drupal\hpc_api\Helpers\ArrayHelper;
use Drupal\hpc_api\Query\EndpointQueryBase;
use Drupal\hpc_api\Traits\SimpleCacheTrait;

/**
* Provides a query plugin for plan entities.
Expand All @@ -40,7 +39,6 @@ class PlanEntitiesQuery extends EndpointQueryBase {

use AttachmentFilterTrait;
use PlanVersionArgument;
use SimpleCacheTrait;
use StringTranslationTrait;

/**
Expand Down Expand Up @@ -74,7 +72,7 @@ public function getData(array $placeholders = [], array $query_args = []) {
*/
private function getAttachments(ContentEntityInterface $context_object = NULL, array $filter = []) {
$cache_key = $this->getCacheKey(array_filter(['id' => $context_object ? $context_object->id() : NULL] + $filter + $this->getPlaceholders()));
$attachments = $this->cache($cache_key);
$attachments = $this->getCache($cache_key);
if ($attachments) {
return $attachments;
}
Expand Down Expand Up @@ -167,7 +165,7 @@ private function getAttachments(ContentEntityInterface $context_object = NULL, a
if (!empty($filter)) {
$attachments = $this->filterAttachments($attachments, $filter);
}
$this->cache($cache_key, $attachments);
$this->setCache($cache_key, $attachments);
return $attachments;
}

Expand Down Expand Up @@ -293,7 +291,7 @@ public function getPlanEntities(ContentEntityInterface $context_object = NULL, $
'entity_type' => $entity_type,
] + ($filters ?? [])));

$plan_entities = $this->cache($cache_key);
$plan_entities = $this->getCache($cache_key);
if ($plan_entities) {
return $plan_entities;
}
Expand Down Expand Up @@ -323,7 +321,7 @@ public function getPlanEntities(ContentEntityInterface $context_object = NULL, $
return $entity->id();
}, $plan_entities);
$plan_entities = array_combine($entity_ids, $plan_entities);
$this->cache($cache_key, $plan_entities);
$this->setCache($cache_key, $plan_entities);
return $plan_entities;
}

Expand All @@ -338,7 +336,7 @@ public function getPlanEntities(ContentEntityInterface $context_object = NULL, $
*/
public function getGoverningEntityIdsForPlanEntityId($plan_entity_id) {
$cache_key = $this->getCacheKey(['plan_entity_id' => $plan_entity_id] + $this->getPlaceholders());
$cluster_ids = $this->cache($cache_key);
$cluster_ids = $this->getCache($cache_key);
if ($cluster_ids) {
return $cluster_ids;
}
Expand All @@ -364,7 +362,7 @@ public function getGoverningEntityIdsForPlanEntityId($plan_entity_id) {
}
}
}
$this->cache($cache_key, $cluster_ids);
$this->setCache($cache_key, $cluster_ids);
return $cluster_ids;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PlanProjectFundingQuery extends EndpointQueryBase {
public function getData(array $placeholders = [], array $query_args = []) {
$placeholders = array_merge($placeholders, $this->getPlaceholders());
$cache_key = $this->getCacheKey($placeholders);
if ($cached_data = $this->cache($cache_key)) {
if ($cached_data = $this->getCache($cache_key)) {
return $cached_data;
}
$data = (object) parent::getData($placeholders, $query_args);
Expand Down Expand Up @@ -69,7 +69,7 @@ public function getData(array $placeholders = [], array $query_args = []) {
}

$this->data = $funding;
$this->cache($cache_key, $this->data);
$this->setCache($cache_key, $this->data);
return $this->data;
}

Expand Down
Loading
Loading