1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Clarify state-changes, replace array_key_exists() with isset() for speed

This commit is contained in:
Darien Hager 2015-05-05 15:28:23 -07:00 committed by Marco Pivetta
parent 1659fab44f
commit 6f1107c4ee

View File

@ -13,6 +13,9 @@ class CacheMetadataListener
/** /**
* Tracks which entities we have already forced caching enabled on. This is * Tracks which entities we have already forced caching enabled on. This is
* important to avoid some potential infinite-recursion issues. * important to avoid some potential infinite-recursion issues.
*
* Key is the name of the entity, payload is unimportant.
*
* @var array * @var array
*/ */
protected $enabledItems = array(); protected $enabledItems = array();
@ -33,13 +36,28 @@ class CacheMetadataListener
$this->enableCaching($metadata, $em); $this->enableCaching($metadata, $em);
} }
/**
* @param ClassMetadata $metadata
* @return bool
*/
private function isVisited(ClassMetaData $metadata) {
return isset($this->enabledItems[$metadata->getName()]);
}
/**
* @param ClassMetadata $metadata
*/
private function recordVisit(ClassMetaData $metadata) {
$this->enabledItems[$metadata->getName()] = true;
}
/** /**
* @param ClassMetadata $metadata * @param ClassMetadata $metadata
* @param EntityManager $em * @param EntityManager $em
*/ */
protected function enableCaching(ClassMetadata $metadata, EntityManager $em) { protected function enableCaching(ClassMetadata $metadata, EntityManager $em) {
if (array_key_exists($metadata->getName(), $this->enabledItems)) { if ($this->isVisited($metadata)) {
return; // Already handled in the past return; // Already handled in the past
} }
@ -53,7 +71,7 @@ class CacheMetadataListener
$metadata->enableCache($cache); $metadata->enableCache($cache);
$this->enabledItems[$metadata->getName()] = $metadata; $this->recordVisit($metadata);
/* /*
* Only enable association-caching when the target has already been * Only enable association-caching when the target has already been
@ -64,7 +82,7 @@ class CacheMetadataListener
$targetMeta = $em->getClassMetadata($mapping['targetEntity']); $targetMeta = $em->getClassMetadata($mapping['targetEntity']);
$this->enableCaching($targetMeta, $em); $this->enableCaching($targetMeta, $em);
if (array_key_exists($targetMeta->getName(), $this->enabledItems)) { if ($this->isVisited($targetMeta)) {
$metadata->enableAssociationCache($mapping['fieldName'], $cache); $metadata->enableAssociationCache($mapping['fieldName'], $cache);
} }
} }