1
0
mirror of synced 2025-01-19 15:01:40 +03:00

Merge pull request #1233 from SofHad/refactoring-duplicate-code

[Minor] Refactoring to avoid duplicate code
This commit is contained in:
Marco Pivetta 2015-01-04 22:59:05 +01:00
commit 6448627bc9

View File

@ -20,9 +20,8 @@
namespace Doctrine\ORM\Cache\Persister; namespace Doctrine\ORM\Cache\Persister;
use Doctrine\ORM\Cache\EntityCacheKey;
use Doctrine\Common\Util\ClassUtils; use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Cache\EntityCacheKey;
/** /**
* Specific non-strict read/write cached entity persister * Specific non-strict read/write cached entity persister
@ -41,41 +40,13 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister
if (isset($this->queuedCache['insert'])) { if (isset($this->queuedCache['insert'])) {
foreach ($this->queuedCache['insert'] as $entity) { foreach ($this->queuedCache['insert'] as $entity) {
$class = $this->class; $isChanged = $this->updateCache($entity, $isChanged);
$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['update'])) { if (isset($this->queuedCache['update'])) {
foreach ($this->queuedCache['update'] as $entity) { foreach ($this->queuedCache['update'] as $entity) {
$class = $this->class; $isChanged = $this->updateCache($entity, $isChanged);
$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);
}
} }
} }
@ -121,4 +92,19 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister
$this->queuedCache['update'][] = $entity; $this->queuedCache['update'][] = $entity;
} }
private function updateCache($entity, $isChanged)
{
$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);
$isChanged = $isChanged ?: $cached;
if ($this->cacheLogger && $cached) {
$this->cacheLogger->entityCachePut($this->regionName, $key);
}
return $isChanged;
}
} }