From 50ef1369c3bde5d511f68b1e643b1d25fd92d6d7 Mon Sep 17 00:00:00 2001 From: shaddag Date: Mon, 29 Dec 2014 18:03:20 +0100 Subject: [PATCH 1/5] [Minor] Refactoring to avoid duplicate code --- ...onStrictReadWriteCachedEntityPersister.php | 104 +++++++++--------- 1 file changed, 51 insertions(+), 53 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php index 3d6004e96..2d3e0e4b7 100644 --- a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php @@ -20,74 +20,33 @@ namespace Doctrine\ORM\Cache\Persister; -use Doctrine\ORM\Cache\EntityCacheKey; - use Doctrine\Common\Util\ClassUtils; +use Doctrine\ORM\Cache\EntityCacheKey; /** * Specific non-strict read/write cached entity persister * * @author Fabio B. Silva - * @since 2.5 + * @since 2.5 */ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister { + + /** + * @var bool + */ + private $isChanged = false; + /** * {@inheritdoc} */ public function afterTransactionComplete() { - $isChanged = false; - - if (isset($this->queuedCache['insert'])) { - foreach ($this->queuedCache['insert'] as $entity) { - $class = $this->class; - $className = ClassUtils::getClass($entity); - - if ($className !== $this->class->name) { - $class = $this->metadataFactory->getMetadataFor($className); - } - - $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); - $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); - $cached = $this->region->put($key, $entry); - $isChanged = $isChanged ?: $cached; - - if ($this->cacheLogger && $cached) { - $this->cacheLogger->entityCachePut($this->regionName, $key); - } - } + foreach (array('insert', 'update', 'delete') as $type) { + $this->handleCache($type); } - if (isset($this->queuedCache['update'])) { - foreach ($this->queuedCache['update'] as $entity) { - $class = $this->class; - $className = ClassUtils::getClass($entity); - - if ($className !== $this->class->name) { - $class = $this->metadataFactory->getMetadataFor($className); - } - - $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); - $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); - $cached = $this->region->put($key, $entry); - $isChanged = $isChanged ?: $cached; - - if ($this->cacheLogger && $cached) { - $this->cacheLogger->entityCachePut($this->regionName, $key); - } - } - } - - if (isset($this->queuedCache['delete'])) { - foreach ($this->queuedCache['delete'] as $key) { - $this->region->evict($key); - - $isChanged = true; - } - } - - if ($isChanged) { + if ($this->isChanged) { $this->timestampRegion->update($this->timestampKey); } @@ -109,7 +68,10 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister { $this->persister->delete($entity); - $this->queuedCache['delete'][] = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity)); + $this->queuedCache['delete'][] = new EntityCacheKey( + $this->class->rootEntityName, + $this->uow->getEntityIdentifier($entity) + ); } /** @@ -121,4 +83,40 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister $this->queuedCache['update'][] = $entity; } + + private function handleCache($type) + { + if (!isset($this->queuedCache[$type])) { + return; + } + + if (in_array($type, array('insert', 'update'))) { + foreach ($this->queuedCache[$type] as $entity) { + $class = $this->class; + $className = ClassUtils::getClass($entity); + + if ($className !== $this->class->name) { + $class = $this->metadataFactory->getMetadataFor($className); + } + + $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); + $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); + $cached = $this->region->put($key, $entry); + $this->isChanged = $this->isChanged ?: $cached; + if ($this->cacheLogger && $cached) { + $this->cacheLogger->entityCachePut($this->regionName, $key); + } + } + + return; + } + + if ('delete' === $type) { + foreach ($this->queuedCache['delete'] as $key) { + $this->region->evict($key); + + $this->isChanged = true; + } + } + } } From 12b5a0cdd714b9b59b4eb924a27e088e315e91a4 Mon Sep 17 00:00:00 2001 From: shaddag Date: Tue, 30 Dec 2014 14:01:29 +0100 Subject: [PATCH 2/5] Extracted ony the common logic --- ...onStrictReadWriteCachedEntityPersister.php | 76 +++++++++---------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php index 2d3e0e4b7..9a6f5c489 100644 --- a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php @@ -31,22 +31,34 @@ use Doctrine\ORM\Cache\EntityCacheKey; */ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister { - - /** - * @var bool - */ - private $isChanged = false; - /** * {@inheritdoc} */ public function afterTransactionComplete() { - foreach (array('insert', 'update', 'delete') as $type) { - $this->handleCache($type); + $isChanged = false; + + if (isset($this->queuedCache['insert'])) { + foreach ($this->queuedCache['insert'] as $entity) { + $isChanged = $this->putCache($entity, $isChanged); + } } - if ($this->isChanged) { + if (isset($this->queuedCache['update'])) { + foreach ($this->queuedCache['update'] as $entity) { + $isChanged = $this->putCache($entity, $isChanged); + } + } + + if (isset($this->queuedCache['delete'])) { + foreach ($this->queuedCache['delete'] as $key) { + $this->region->evict($key); + + $isChanged = true; + } + } + + if ($isChanged) { $this->timestampRegion->update($this->timestampKey); } @@ -68,10 +80,7 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister { $this->persister->delete($entity); - $this->queuedCache['delete'][] = new EntityCacheKey( - $this->class->rootEntityName, - $this->uow->getEntityIdentifier($entity) - ); + $this->queuedCache['delete'][] = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity)); } /** @@ -84,39 +93,24 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister $this->queuedCache['update'][] = $entity; } - private function handleCache($type) + private function putCache($entity, $isChanged) { - if (!isset($this->queuedCache[$type])) { - return; + $class = $this->class; + $className = ClassUtils::getClass($entity); + + if ($className !== $this->class->name) { + $class = $this->metadataFactory->getMetadataFor($className); } - if (in_array($type, array('insert', 'update'))) { - foreach ($this->queuedCache[$type] as $entity) { - $class = $this->class; - $className = ClassUtils::getClass($entity); + $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); + $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); + $cached = $this->region->put($key, $entry); + $isChanged = $isChanged ?: $cached; - if ($className !== $this->class->name) { - $class = $this->metadataFactory->getMetadataFor($className); - } - - $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); - $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); - $cached = $this->region->put($key, $entry); - $this->isChanged = $this->isChanged ?: $cached; - if ($this->cacheLogger && $cached) { - $this->cacheLogger->entityCachePut($this->regionName, $key); - } - } - - return; + if ($this->cacheLogger && $cached) { + $this->cacheLogger->entityCachePut($this->regionName, $key); } - if ('delete' === $type) { - foreach ($this->queuedCache['delete'] as $key) { - $this->region->evict($key); - - $this->isChanged = true; - } - } + return $isChanged; } } From ccb09ad0ace92917ed2475614e4a1f7ca9cfbb94 Mon Sep 17 00:00:00 2001 From: shaddag Date: Tue, 30 Dec 2014 14:10:56 +0100 Subject: [PATCH 3/5] Rename the private method --- .../Persister/NonStrictReadWriteCachedEntityPersister.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php index 9a6f5c489..dc9e7d1c2 100644 --- a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php @@ -40,13 +40,13 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister if (isset($this->queuedCache['insert'])) { foreach ($this->queuedCache['insert'] as $entity) { - $isChanged = $this->putCache($entity, $isChanged); + $isChanged = $this->handleCache($entity, $isChanged); } } if (isset($this->queuedCache['update'])) { foreach ($this->queuedCache['update'] as $entity) { - $isChanged = $this->putCache($entity, $isChanged); + $isChanged = $this->handleCache($entity, $isChanged); } } @@ -93,7 +93,7 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister $this->queuedCache['update'][] = $entity; } - private function putCache($entity, $isChanged) + private function handleCache($entity, $isChanged) { $class = $this->class; $className = ClassUtils::getClass($entity); From 8d91f7de60ab9da8e10424216c9635b1b386d71f Mon Sep 17 00:00:00 2001 From: Sofiane HADDAG Date: Tue, 30 Dec 2014 14:49:12 +0100 Subject: [PATCH 4/5] Rename handleCache to updateCache --- .../Persister/NonStrictReadWriteCachedEntityPersister.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php index dc9e7d1c2..c3ce33128 100644 --- a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php @@ -40,13 +40,13 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister if (isset($this->queuedCache['insert'])) { foreach ($this->queuedCache['insert'] as $entity) { - $isChanged = $this->handleCache($entity, $isChanged); + $isChanged = $this->updateCache($entity, $isChanged); } } if (isset($this->queuedCache['update'])) { foreach ($this->queuedCache['update'] as $entity) { - $isChanged = $this->handleCache($entity, $isChanged); + $isChanged = $this->updateCache($entity, $isChanged); } } @@ -93,7 +93,7 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister $this->queuedCache['update'][] = $entity; } - private function handleCache($entity, $isChanged) + private function updateCache($entity, $isChanged) { $class = $this->class; $className = ClassUtils::getClass($entity); From eb22db0dcf8d01cfed9544e84805ba18dfdea94c Mon Sep 17 00:00:00 2001 From: SofHad Date: Sun, 4 Jan 2015 20:36:54 +0100 Subject: [PATCH 5/5] remove the unnecessary check --- .../Persister/NonStrictReadWriteCachedEntityPersister.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php index c3ce33128..ca7f8bc2d 100644 --- a/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/NonStrictReadWriteCachedEntityPersister.php @@ -95,13 +95,7 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister private function updateCache($entity, $isChanged) { - $class = $this->class; - $className = ClassUtils::getClass($entity); - - if ($className !== $this->class->name) { - $class = $this->metadataFactory->getMetadataFor($className); - } - + $class = $this->metadataFactory->getMetadataFor(get_class($entity)); $key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity)); $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); $cached = $this->region->put($key, $entry);