Skip to content

Commit

Permalink
Invalidate cache on datastore drop (#4351)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai authored Dec 4, 2024
1 parent 19541b4 commit 7c9308e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/datastore/datastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- '@dkan.datastore.service.resource_processor.dictionary_enforcer'
- '@dkan.metastore.resource_mapper'
- '@event_dispatcher'
- '@dkan.metastore.reference_lookup'

dkan.datastore.service.post_import:
class: \Drupal\datastore\Service\PostImport
Expand Down
29 changes: 28 additions & 1 deletion modules/datastore/src/DatastoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\datastore\Service\Factory\ImportFactoryInterface;
use Drupal\datastore\Service\ImportService;
use Drupal\datastore\Service\ResourceLocalizer;
use Drupal\metastore\Reference\ReferenceLookup;
use Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer;
use Drupal\datastore\Storage\ImportJobStoreFactory;
use Drupal\metastore\ResourceMapper;
Expand Down Expand Up @@ -84,6 +85,13 @@ class DatastoreService implements ContainerInjectionInterface {
*/
private EventDispatcherInterface $eventDispatcher;

/**
* Reference lookup service.
*
* @var \Drupal\metastore\Reference\ReferenceLookup
*/
protected $referenceLookup;

/**
* {@inheritdoc}
*/
Expand All @@ -95,7 +103,8 @@ public static function create(ContainerInterface $container) {
$container->get('dkan.datastore.import_job_store_factory'),
$container->get('dkan.datastore.service.resource_processor.dictionary_enforcer'),
$container->get('dkan.metastore.resource_mapper'),
$container->get('event_dispatcher')
$container->get('event_dispatcher'),
$container->get('dkan.metastore.reference_lookup')
);
}

Expand All @@ -116,6 +125,8 @@ public static function create(ContainerInterface $container) {
* Resource mapper service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* Event dispatcher service.
* @param \Drupal\metastore\Reference\ReferenceLookup $referenceLookup
* The reference lookup service.
*/
public function __construct(
ResourceLocalizer $resourceLocalizer,
Expand All @@ -125,6 +136,7 @@ public function __construct(
DictionaryEnforcer $dictionaryEnforcer,
ResourceMapper $resourceMapper,
EventDispatcherInterface $eventDispatcher,
ReferenceLookup $referenceLookup,
) {
$this->resourceLocalizer = $resourceLocalizer;
$this->importServiceFactory = $importServiceFactory;
Expand All @@ -133,6 +145,7 @@ public function __construct(
$this->dictionaryEnforcer = $dictionaryEnforcer;
$this->resourceMapper = $resourceMapper;
$this->eventDispatcher = $eventDispatcher;
$this->referenceLookup = $referenceLookup;
}

/**
Expand Down Expand Up @@ -297,6 +310,10 @@ public function drop(string $identifier, ?string $version = NULL, bool $remove_l
if ($remove_local_resource) {
$this->resourceLocalizer->remove($identifier, $version);
}

// Invalidate cache tag.
$uid = $resource->getIdentifier() . '__' . $resource->getVersion();
$this->invalidateCacheTags($uid . '__source');
}

/**
Expand Down Expand Up @@ -360,4 +377,14 @@ public function getQueueFactory(): QueueFactory {
return $this->queue;
}

/**
* Invalidate all appropriate cache tags for this resource.
*
* @param mixed $resourceId
* A resource ID.
*/
protected function invalidateCacheTags(mixed $resourceId) {
$this->referenceLookup->invalidateReferencerCacheTags('distribution', $resourceId, 'downloadURL');
}

}
11 changes: 11 additions & 0 deletions modules/datastore/tests/src/Kernel/DatastoreServiceEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
*/
class DatastoreServiceEventsTest extends KernelTestBase implements EventSubscriberInterface {

protected $strictConfigSchema = FALSE;

protected static $modules = [
'common',
'datastore',
'metastore',
'node',
'user',
'field',
'text',
'system',
];

/**
Expand Down Expand Up @@ -79,6 +86,9 @@ public function register(ContainerBuilder $container) {
}

public function testEvents() {
$this->installEntitySchema('node');
$this->installConfig(['node']);
$this->installConfig(['metastore']);
// Mock a data resource.
$data_resource = $this->getMockBuilder(DataResource::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -130,6 +140,7 @@ public function testEvents() {
$this->container->get('dkan.datastore.service.resource_processor.dictionary_enforcer'),
$this->container->get('dkan.metastore.resource_mapper'),
$this->container->get('event_dispatcher'),
$this->container->get('dkan.metastore.reference_lookup'),
])
->onlyMethods(['getStorage'])
->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testErrorPath() {
$this->container->get('dkan.datastore.service.resource_processor.dictionary_enforcer'),
$this->container->get('dkan.metastore.resource_mapper'),
$this->container->get('event_dispatcher'),
$this->container->get('dkan.metastore.reference_lookup'),
])
->onlyMethods(['import'])
->getMock();
Expand Down Expand Up @@ -95,6 +96,7 @@ public function testRequeue() {
$this->container->get('dkan.datastore.service.resource_processor.dictionary_enforcer'),
$this->container->get('dkan.metastore.resource_mapper'),
$this->container->get('event_dispatcher'),
$this->container->get('dkan.metastore.reference_lookup'),
])
->onlyMethods(['import'])
->getMock();
Expand Down Expand Up @@ -226,6 +228,7 @@ public function testAlreadyImported() {
$this->container->get('dkan.datastore.service.resource_processor.dictionary_enforcer'),
$this->container->get('dkan.metastore.resource_mapper'),
$this->container->get('event_dispatcher'),
$this->container->get('dkan.metastore.reference_lookup'),
])
->onlyMethods(['getStorage'])
->getMock();
Expand Down
6 changes: 5 additions & 1 deletion modules/datastore/tests/src/Unit/DatastoreServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\datastore\Service\ImportService;
use Drupal\datastore\Service\Info\ImportInfoList;
use Drupal\datastore\Service\ResourceLocalizer;
use Drupal\metastore\Reference\ReferenceLookup;
use Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer;
use Drupal\datastore\Storage\DatabaseTable;
use Drupal\metastore\ResourceMapper;
Expand Down Expand Up @@ -69,8 +70,10 @@ public function testDrop() {
->add(ResourceLocalizer::class, 'get', $resource)
->add(ResourceMapper::class, 'get', $resource)
->add(JobStoreFactory::class, 'getInstance', JobStore::class)
->add(JobStore::class, 'remove', TRUE)
->add(ImportJobStoreFactory::class, 'getInstance', JobStore::class)
->add(JobStore::class, 'remove', TRUE);
->add(ReferenceLookup::class, 'getReferencers', [$resource->getIdentifier()])
->add(ReferenceLookup::class, 'invalidateReferencerCacheTags');

$service = DatastoreService::create($mockChain->getMock());
// These are all valid ways to call drop().
Expand Down Expand Up @@ -106,6 +109,7 @@ private function getCommonChain() {
->add('dkan.datastore.import_job_store_factory', ImportJobStoreFactory::class)
->add('dkan.datastore.import_info_list', ImportInfoList::class)
->add('dkan.datastore.service.resource_processor.dictionary_enforcer', DictionaryEnforcer::class)
->add('dkan.metastore.reference_lookup', ReferenceLookup::class)
->index(0);

return (new Chain($this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Drupal\metastore\Storage\DataFactory;
use Drupal\Tests\common\Unit\Storage\QueryDataProvider as QueryData;
use Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer;
use Drupal\metastore\Reference\ReferenceLookup;

/**
* @group dkan
Expand Down Expand Up @@ -204,6 +205,7 @@ public function getCommonMockChain() {
->add('dkan.metastore.storage', DataFactory::class)
->add('dkan.datastore.import_info_list', ImportInfoList::class)
->add('dkan.datastore.service.resource_processor.dictionary_enforcer', DictionaryEnforcer::class)
->add('dkan.metastore.reference_lookup', ReferenceLookup::class)
->index(0);

$resource_metadata = '{"data":{"%Ref:downloadURL":[{"data":{"identifier":"qwerty","version":"uiop"}}]}}';
Expand All @@ -223,7 +225,9 @@ public function getCommonMockChain() {
->add(DatabaseTable::class, "query", $queryResult, 'DatabaseTableQuery')
->add(DatabaseTable::class, "getSchema", ["fields" => ["a" => "a", "b" => "b"]])
->add(DatabaseTable::class, "getTableName", "table2")
->add(DatabaseTable::class, "primaryKey", "record_number");
->add(DatabaseTable::class, "primaryKey", "record_number")
->add(ReferenceLookup::class, 'getReferencers', [$resource->getIdentifier()])
->add(ReferenceLookup::class, 'invalidateReferencerCacheTags');

}

Expand Down

0 comments on commit 7c9308e

Please sign in to comment.