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

Add native types to caches #9508

Merged
merged 1 commit into from
Feb 21, 2022
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
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Upgrade to 3.0

## BC BREAK: Remove helper methods from `AbstractCollectionPersister`

The following protected methods of
`Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister`
have been removed.

* `evictCollectionCache()`
* `evictElementCache()`

## BC BREAK: `Doctrine\ORM\Query\TreeWalkerChainIterator`

This class has been removed without replacement.
Expand Down
86 changes: 14 additions & 72 deletions lib/Doctrine/ORM/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,127 +38,69 @@ interface Cache
*/
public const MODE_REFRESH = 4;

/**
* @param string $className The entity class.
*
* @return Region|null
*/
public function getEntityCacheRegion($className);
public function getEntityCacheRegion(string $className): ?Region;

/**
* @param string $className The entity class.
* @param string $association The field name that represents the association.
*
* @return Region|null
*/
public function getCollectionCacheRegion($className, $association);
public function getCollectionCacheRegion(string $className, string $association): ?Region;

/**
* Determine whether the cache contains data for the given entity "instance".
*
* @param string $className The entity class.
* @param mixed $identifier The entity identifier
*
* @return bool true if the underlying cache contains corresponding data; false otherwise.
*/
public function containsEntity($className, $identifier);
public function containsEntity(string $className, mixed $identifier): bool;

/**
* Evicts the entity data for a particular entity "instance".
*
* @param string $className The entity class.
* @param mixed $identifier The entity identifier.
*
* @return void
*/
public function evictEntity($className, $identifier);
public function evictEntity(string $className, mixed $identifier): void;

/**
* Evicts all entity data from the given region.
*
* @param string $className The entity metadata.
*
* @return void
*/
public function evictEntityRegion($className);
public function evictEntityRegion(string $className): void;

/**
* Evict data from all entity regions.
*
* @return void
*/
public function evictEntityRegions();
public function evictEntityRegions(): void;

/**
* Determine whether the cache contains data for the given collection.
*
* @param string $className The entity class.
* @param string $association The field name that represents the association.
* @param mixed $ownerIdentifier The identifier of the owning entity.
*
* @return bool true if the underlying cache contains corresponding data; false otherwise.
*/
public function containsCollection($className, $association, $ownerIdentifier);
public function containsCollection(string $className, string $association, mixed $ownerIdentifier): bool;

/**
* Evicts the cache data for the given identified collection instance.
*
* @param string $className The entity class.
* @param string $association The field name that represents the association.
* @param mixed $ownerIdentifier The identifier of the owning entity.
*
* @return void
*/
public function evictCollection($className, $association, $ownerIdentifier);
public function evictCollection(string $className, string $association, mixed $ownerIdentifier): void;

/**
* Evicts all entity data from the given region.
*
* @param string $className The entity class.
* @param string $association The field name that represents the association.
*
* @return void
*/
public function evictCollectionRegion($className, $association);
public function evictCollectionRegion(string $className, string $association): void;

/**
* Evict data from all collection regions.
*
* @return void
*/
public function evictCollectionRegions();
public function evictCollectionRegions(): void;

/**
* Determine whether the cache contains data for the given query.
*
* @param string $regionName The cache name given to the query.
*
* @return bool true if the underlying cache contains corresponding data; false otherwise.
*/
public function containsQuery($regionName);
public function containsQuery(string $regionName): bool;

/**
* Evicts all cached query results under the given name, or default query cache if the region name is NULL.
*
* @param string|null $regionName The cache name associated to the queries being cached.
*
* @return void
*/
public function evictQueryRegion($regionName = null);
public function evictQueryRegion(?string $regionName = null): void;

/**
* Evict data from all query regions.
*
* @return void
*/
public function evictQueryRegions();
public function evictQueryRegions(): void;

/**
* Get query cache by region name or create a new one if none exist.
*
* @param string|null $regionName Query cache region name, or default query cache if the region name is NULL.
*
* @return QueryCache The Query Cache associated with the region name.
*/
public function getQueryCache($regionName = null);
public function getQueryCache(?string $regionName = null): QueryCache;
}
12 changes: 4 additions & 8 deletions lib/Doctrine/ORM/Cache/AssociationCacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ class AssociationCacheEntry implements CacheEntry
* @readonly Public only for performance reasons, it should be considered immutable.
* @var array<string, mixed>
*/
public $identifier;
public array $identifier;

/**
* The entity class name
*
* @readonly Public only for performance reasons, it should be considered immutable.
* @var string
* @psalm-var class-string
*/
public $class;
public string $class;

/**
* @param string $class The entity class.
* @param array<string, mixed> $identifier The entity identifier.
* @psalm-param class-string $class
*/
public function __construct($class, array $identifier)
public function __construct(string $class, array $identifier)
{
$this->class = $class;
$this->identifier = $identifier;
Expand All @@ -43,10 +41,8 @@ public function __construct($class, array $identifier)
* This method allow Doctrine\Common\Cache\PhpFileCache compatibility
*
* @param array<string, mixed> $values array containing property values
*
* @return AssociationCacheEntry
*/
public static function __set_state(array $values)
public static function __set_state(array $values): self
{
return new self($values['class'], $values['identifier']);
}
Expand Down
71 changes: 16 additions & 55 deletions lib/Doctrine/ORM/Cache/CacheConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,49 @@
*/
class CacheConfiguration
{
/** @var CacheFactory|null */
private $cacheFactory;
private ?CacheFactory $cacheFactory = null;
private ?RegionsConfiguration $regionsConfig = null;
private ?CacheLogger $cacheLogger = null;
private ?QueryCacheValidator $queryValidator = null;

/** @var RegionsConfiguration|null */
private $regionsConfig;

/** @var CacheLogger|null */
private $cacheLogger;

/** @var QueryCacheValidator|null */
private $queryValidator;

/**
* @return CacheFactory|null
*/
public function getCacheFactory()
public function getCacheFactory(): ?CacheFactory
{
return $this->cacheFactory;
}

/**
* @return void
*/
public function setCacheFactory(CacheFactory $factory)
public function setCacheFactory(CacheFactory $factory): void
{
$this->cacheFactory = $factory;
}

/**
* @return CacheLogger|null
*/
public function getCacheLogger()
public function getCacheLogger(): ?CacheLogger
{
return $this->cacheLogger;
}

/**
* @return void
*/
public function setCacheLogger(CacheLogger $logger)
public function setCacheLogger(CacheLogger $logger): void
{
$this->cacheLogger = $logger;
}

/**
* @return RegionsConfiguration
*/
public function getRegionsConfiguration()
public function getRegionsConfiguration(): RegionsConfiguration
{
if ($this->regionsConfig === null) {
$this->regionsConfig = new RegionsConfiguration();
}

return $this->regionsConfig;
return $this->regionsConfig ??= new RegionsConfiguration();
}

/**
* @return void
*/
public function setRegionsConfiguration(RegionsConfiguration $regionsConfig)
public function setRegionsConfiguration(RegionsConfiguration $regionsConfig): void
{
$this->regionsConfig = $regionsConfig;
}

/**
* @return QueryCacheValidator
*/
public function getQueryValidator()
public function getQueryValidator(): QueryCacheValidator
{
if ($this->queryValidator === null) {
$this->queryValidator = new TimestampQueryCacheValidator(
$this->cacheFactory->getTimestampRegion()
);
}

return $this->queryValidator;
return $this->queryValidator ??= new TimestampQueryCacheValidator(
$this->cacheFactory->getTimestampRegion()
);
}

/**
* @return void
*/
public function setQueryValidator(QueryCacheValidator $validator)
public function setQueryValidator(QueryCacheValidator $validator): void
{
$this->queryValidator = $validator;
}
Expand Down
40 changes: 2 additions & 38 deletions lib/Doctrine/ORM/Cache/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,13 @@
*/
class CacheException extends LogicException implements ORMException
{
/**
* @param string $sourceEntity
* @param string $fieldName
*
* @return CacheException
*/
public static function updateReadOnlyCollection($sourceEntity, $fieldName)
public static function updateReadOnlyCollection(string $sourceEntity, string $fieldName): self
{
return new self(sprintf('Cannot update a readonly collection "%s#%s"', $sourceEntity, $fieldName));
}

/**
* @deprecated This method is not used anymore.
*
* @param string $entityName
*
* @return CacheException
*/
public static function updateReadOnlyEntity($entityName)
{
return new self(sprintf('Cannot update a readonly entity "%s"', $entityName));
}

/**
* @param string $entityName
*
* @return CacheException
*/
public static function nonCacheableEntity($entityName)
public static function nonCacheableEntity(string $entityName): self
{
return new self(sprintf('Entity "%s" not configured as part of the second-level cache.', $entityName));
}

/**
* @deprecated This method is not used anymore.
*
* @param string $entityName
* @param string $field
*
* @return CacheException
*/
public static function nonCacheableEntityAssociation($entityName, $field)
{
return new self(sprintf('Entity association field "%s#%s" not configured as part of the second-level cache.', $entityName, $field));
}
}
Loading