Skip to content

Commit

Permalink
WIP: Add cache tags to API queries, add cache control section backend…
Browse files Browse the repository at this point in the history
… page where the API cache for a specific section can be cleared selectively
  • Loading branch information
berliner committed Jul 28, 2023
1 parent 3abe30a commit c2b17fa
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 76 deletions.
2 changes: 2 additions & 0 deletions config/user.role.administrator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
- ghi_base_objects
- ghi_blocks
- ghi_content
- 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
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 @@ -79,7 +79,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 @@ -6,7 +6,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 @@ -29,7 +28,6 @@ class AttachmentSearchQuery extends EndpointQueryBase {

use AttachmentFilterTrait;
use PlanVersionArgument;
use SimpleCacheTrait;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -61,7 +59,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 @@ -71,7 +69,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 @@ -99,7 +97,7 @@ public function getAttachmentsByObject($object_type, $object_ids, array $filter
'object_type' => $object_type,
'object_ids' => $object_ids,
] + (array) $filter);
$attachments = $this->cache($cache_key);
$attachments = $this->getCache($cache_key);
if ($attachments) {
return $attachments;
}
Expand All @@ -120,7 +118,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,12 @@ public function getUnprocessedMeasurements(DataAttachment $attachment, $disaggre
if (!$disaggregation) {
$endpoint_args['disaggregation'] = 'false';
}
$plan_id = $attachment->getPlanId();
$this->setCacheTags([
'plan_id:' . $plan_id,
]);
if ($this->isAutenticatedEndpoint) {
$endpoint_args['version'] = $this->getPlanVersionArgumentForPlanId($attachment->getPlanId());
$endpoint_args['version'] = $this->getPlanVersionArgumentForPlanId($plan_id);
$data = $this->getData([], ['attachmentId' => $attachment->id()] + $endpoint_args);
return $data;
}
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 @@ -39,7 +38,6 @@ class PlanEntitiesQuery extends EndpointQueryBase {

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

/**
Expand Down Expand Up @@ -72,7 +70,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 @@ -165,7 +163,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 @@ -291,7 +289,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 @@ -321,7 +319,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 @@ -336,7 +334,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 @@ -362,7 +360,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 @@ -36,7 +36,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 @@ -65,7 +65,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

0 comments on commit c2b17fa

Please sign in to comment.