Skip to content
Snippets Groups Projects
Unverified Commit b4ac77c0 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3016725 by e0ipso: Do not use cache.static

parent 94356e04
No related branches found
No related tags found
No related merge requests found
......@@ -21,13 +21,6 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase {
// Override the class used for the configurable service.
$definition = $container->getDefinition('jsonapi.resource_type.repository');
$definition->setClass(ConfigurableResourceTypeRepository::class);
// We can't use ConfigurableResourceTypeRepository::isJsonApi2x() here.
// 'jsonapi.entity.to_jsonapi' service was removed in 2.x see
// https://www.drupal.org/node/2983182.
// @todo Remove this when JSON API Extras drops support for JSON API 1.x.
if ($container->has('jsonapi.entity.to_jsonapi')) {
$definition->addMethodCall('setStaticCache', [new Reference('cache.static')]);
}
// The configurable service expects the entity repository and the enhancer
// plugin manager.
$definition->addMethodCall('setEntityRepository', [new Reference('entity.repository')]);
......
......@@ -3,13 +3,11 @@
namespace Drupal\jsonapi_extras\ResourceType;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Defines a configurable resource type.
......@@ -40,11 +38,11 @@ class ConfigurableResourceType extends ResourceType {
protected $configFactory;
/**
* Cache instance to use for data caches.
* The static cache.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
* @var array
*/
protected $staticCache;
protected $cache = [];
/**
* {@inheritdoc}
......@@ -88,16 +86,6 @@ class ConfigurableResourceType extends ResourceType {
}
}
/**
* Sets the internal cache storage service.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache back-end.
*/
public function setStaticCache(CacheBackendInterface $cache_backend) {
$this->staticCache = $cache_backend;
}
/**
* {@inheritdoc}
*
......@@ -151,9 +139,8 @@ class ConfigurableResourceType extends ResourceType {
$resource_config = $this->getJsonapiResourceConfig();
$config_id = $resource_config->id();
$cid = sprintf('jsonapi_extras::%s:%s:%s', $config_id, $field_name, $from);
$cache = $this->staticCache->get($cid);
if ($cache !== FALSE) {
return $cache->data ?: NULL;
if (array_key_exists($cid, $this->cache)) {
return $this->cache[$cid];
}
$resource_fields = $resource_config->get('resourceFields');
......@@ -163,19 +150,7 @@ class ConfigurableResourceType extends ResourceType {
$field_name == $resource_field[$from];
});
$result = empty($found) ? NULL : reset($found);
// Save the results into the cache.
$cache_tags = ['jsonapi_resource_types'];
$resource_tags = (new CacheableMetadata())
->addCacheableDependency($resource_config)
->addCacheableDependency($resource_config->getEntityType())
->getCacheTags();
$cache_tags = array_merge($cache_tags, $resource_tags);
$this->staticCache->set(
$cid,
$result,
CacheBackendInterface::CACHE_PERMANENT,
$cache_tags
);
$this->cache[$cid] = $result;
return $result;
}
......
......@@ -9,7 +9,6 @@ use Drupal\Core\Extension\ModuleHandler;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Provides a repository of JSON API configurable resource types.
......@@ -65,13 +64,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
*/
protected $resourceConfigs;
/**
* The static cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $staticCache;
/**
* Detects whether this site has JSON API 1.x or 2.x installed.
*
......@@ -102,16 +94,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
$this->entityRepository = $entity_repository;
}
/**
* Injects the cache back-end.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache back-end.
*/
public function setStaticCache(CacheBackendInterface $cache_backend) {
$this->staticCache = $cache_backend;
}
/**
* Injects the resource enhancer manager.
*
......@@ -183,7 +165,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
$resource_type->setJsonapiResourceConfig($resource_config);
$resource_type->setEnhancerManager($this->enhancerManager);
$resource_type->setConfigFactory($this->configFactory);
$resource_type->setStaticCache($this->staticCache);
return $resource_type;
}
......@@ -208,7 +189,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
$resource_type->setJsonapiResourceConfig($resource_config);
$resource_type->setEnhancerManager($this->enhancerManager);
$resource_type->setConfigFactory($this->configFactory);
$resource_type->setStaticCache($this->staticCache);
$entity_type = $this
->entityTypeManager
->getDefinition($resource_type->getEntityTypeId());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment