1
0
mirror of synced 2024-12-05 03:06:05 +03:00

#954 DDC-2982 - Making cache Region always a MultiGetRegion (no need to segregate the interface here)

This commit is contained in:
Marco Pivetta 2015-01-17 23:10:49 +01:00
parent 3f64f3252b
commit 3c5a794691
13 changed files with 98 additions and 15 deletions

View File

@ -31,12 +31,12 @@ class CollectionCacheEntry implements CacheEntry
/**
* READ-ONLY: Public only for performance reasons, it should be considered immutable.
*
* @var array The list of entity identifiers hold by the collection
* @var CacheKey[] The list of entity identifiers hold by the collection
*/
public $identifiers;
/**
* @param array $identifiers List of entity identifiers hold by the collection
* @param CacheKey[] $identifiers List of entity identifiers hold by the collection
*/
public function __construct(array $identifiers)
{
@ -46,9 +46,11 @@ class CollectionCacheEntry implements CacheEntry
/**
* Creates a new CollectionCacheEntry
*
* This method allow Doctrine\Common\Cache\PhpFileCache compatibility
* This method allows for Doctrine\Common\Cache\PhpFileCache compatibility
*
* @param array $values array containing property values
*
* @return self
*/
public static function __set_state(array $values)
{

View File

@ -34,8 +34,9 @@ interface MultiGetRegion
* Get all items from the cache indentifed by $keys.
* It returns NULL if some elements can not be found.
*
* @param CollectionCacheEntry[] $collection The collection of the items to be retrieved.
* @return array The cached entries or NULL if one or more entries can not be found
* @param CollectionCacheEntry $collection The collection of the items to be retrieved.
*
* @return CacheEntry[]|null The cached entries or NULL if one or more entries can not be found
*/
public function getMulti(CollectionCacheEntry $collection);
public function getMultiple(CollectionCacheEntry $collection);
}

View File

@ -26,7 +26,7 @@ namespace Doctrine\ORM\Cache;
* @since 2.5
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
interface Region
interface Region extends MultiGetRegion
{
/**
* Retrieve the name of this region.

View File

@ -34,12 +34,12 @@ use Doctrine\ORM\Cache\CollectionCacheEntry;
* @since 2.5
* @author Asmir Mustafic <goetas@gmail.com>
*/
class DefaultMultiGetRegion extends DefaultRegion implements MultiGetRegion
class DefaultMultiGetRegion extends DefaultRegion
{
/**
* {@inheritdoc}
*/
public function getMulti(CollectionCacheEntry $collection)
public function getMultiple(CollectionCacheEntry $collection)
{
$keysToRetrieve = array();
foreach ($collection->identifiers as $index => $key) {

View File

@ -20,6 +20,7 @@
namespace Doctrine\ORM\Cache\Region;
use Doctrine\ORM\Cache\CollectionCacheEntry;
use Doctrine\ORM\Cache\Lock;
use Doctrine\ORM\Cache\Region;
use Doctrine\ORM\Cache\CacheKey;
@ -93,6 +94,37 @@ class DefaultRegion implements Region
return $this->cache->fetch($this->name . '_' . $key->hash) ?: null;
}
/**
* {@inheritdoc}
*/
public function getMultiple(CollectionCacheEntry $collection)
{
$keysToRetrieve = array();
foreach ($collection->identifiers as $index => $key) {
$keysToRetrieve[$index] = $this->name . '_' . $key->hash;
}
$items = array_filter(
array_map([$this->cache, 'fetch'], $keysToRetrieve),
function ($retrieved) {
return false !== $retrieved;
}
);
if (count($items) !== count($keysToRetrieve)) {
return null;
}
$returnableItems = array();
foreach ($keysToRetrieve as $index => $key) {
$returnableItems[$index] = $items[$key];
}
return $returnableItems;
}
/**
* {@inheritdoc}
*/

View File

@ -20,6 +20,7 @@
namespace Doctrine\ORM\Cache\Region;
use Doctrine\ORM\Cache\CollectionCacheEntry;
use Doctrine\ORM\Cache\Lock;
use Doctrine\ORM\Cache\Region;
use Doctrine\ORM\Cache\CacheKey;
@ -172,6 +173,18 @@ class FileLockRegion implements ConcurrentRegion
return $this->region->get($key);
}
/**
* {@inheritdoc}
*/
public function getMultiple(CollectionCacheEntry $collection)
{
if (array_filter(array_map([$this, 'isLocked'], $collection->identifiers))) {
return null;
}
return $this->region->getMultiple($collection);
}
/**
* {inheritdoc}
*/

View File

@ -4,6 +4,7 @@ namespace Doctrine\Tests\Mocks;
use Doctrine\ORM\Cache\CacheEntry;
use Doctrine\ORM\Cache\CacheKey;
use Doctrine\ORM\Cache\CollectionCacheEntry;
use Doctrine\ORM\Cache\Lock;
use Doctrine\ORM\Cache\Region;
@ -94,6 +95,16 @@ class CacheRegionMock implements Region
return $this->getReturn(__FUNCTION__, null);
}
/**
* {@inheritdoc}
*/
public function getMultiple(CollectionCacheEntry $collection)
{
$this->calls[__FUNCTION__][] = array('collection' => $collection);
return $this->getReturn(__FUNCTION__, null);
}
/**
* {@inheritdoc}
*/

View File

@ -3,6 +3,7 @@
namespace Doctrine\Tests\Mocks;
use Doctrine\ORM\Cache\CollectionCacheEntry;
use Doctrine\ORM\Cache\ConcurrentRegion;
use Doctrine\ORM\Cache\LockException;
use Doctrine\ORM\Cache\CacheEntry;
@ -131,6 +132,18 @@ class ConcurrentRegionMock implements ConcurrentRegion
return $this->region->get($key);
}
/**
* {@inheritdoc}
*/
public function getMultiple(CollectionCacheEntry $collection)
{
$this->calls[__FUNCTION__][] = array('collection' => $collection);
$this->throwException(__FUNCTION__);
return $this->region->getMultiple($collection);
}
/**
* {@inheritdoc}
*/

View File

@ -33,7 +33,7 @@ class MultiGetRegionTest extends AbstractRegionTest
$this->region->put($key1, $value1);
$this->region->put($key2, $value2);
$actual = $this->region->getMulti(new CollectionCacheEntry(array($key1, $key2)));
$actual = $this->region->getMultiple(new CollectionCacheEntry(array($key1, $key2)));
$this->assertEquals($value1, $actual[0]);
$this->assertEquals($value2, $actual[1]);

View File

@ -38,6 +38,7 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
'getName',
'contains',
'get',
'getMultiple',
'put',
'evict',
'evictAll'
@ -56,6 +57,7 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
'removeElement',
'removeKey',
'get',
'getMultiple',
'loadCriteria'
);
@ -65,7 +67,7 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
* @param \Doctrine\ORM\Cache\Region $region
* @param array $mapping
*
* @return Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister
* @return \Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister
*/
abstract protected function createPersister(EntityManager $em, CollectionPersister $persister, Region $region, array $mapping);
@ -77,7 +79,10 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
$this->em = $this->_getTestEntityManager();
$this->region = $this->createRegion();
$this->collectionPersister = $this->getMock('Doctrine\ORM\Persisters\Collection\CollectionPersister', $this->collectionPersisterMockMethods);
$this->collectionPersister = $this->getMock(
'Doctrine\ORM\Persisters\Collection\CollectionPersister',
$this->collectionPersisterMockMethods
);
}
/**

View File

@ -19,6 +19,7 @@ class ReadWriteCachedCollectionPersisterTest extends AbstractCollectionPersister
'getName',
'contains',
'get',
'getMultiple',
'put',
'evict',
'evictAll',

View File

@ -41,6 +41,7 @@ abstract class AbstractEntityPersisterTest extends OrmTestCase
'getName',
'contains',
'get',
'getMultiple',
'put',
'evict',
'evictAll'
@ -85,7 +86,7 @@ abstract class AbstractEntityPersisterTest extends OrmTestCase
* @param \Doctrine\ORM\Cache\Region $region
* @param \Doctrine\ORM\Mapping\ClassMetadata $metadata
*
* @return Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister
* @return \Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister
*/
abstract protected function createPersister(EntityManager $em, EntityPersister $persister, Region $region, ClassMetadata $metadata);
@ -97,7 +98,10 @@ abstract class AbstractEntityPersisterTest extends OrmTestCase
$this->em = $this->_getTestEntityManager();
$this->region = $this->createRegion();
$this->entityPersister = $this->getMock('Doctrine\ORM\Persisters\Entity\EntityPersister', $this->entityPersisterMockMethods);
$this->entityPersister = $this->getMock(
'Doctrine\ORM\Persisters\Entity\EntityPersister',
$this->entityPersisterMockMethods
);
}
/**
@ -109,7 +113,7 @@ abstract class AbstractEntityPersisterTest extends OrmTestCase
}
/**
* @return Doctrine\ORM\Cache\Persister\AbstractEntityPersister
* @return \Doctrine\ORM\Cache\Persister\AbstractEntityPersister
*/
protected function createPersisterDefault()
{

View File

@ -20,6 +20,7 @@ class ReadWriteCachedEntityPersisterTest extends AbstractEntityPersisterTest
'getName',
'contains',
'get',
'getMultiple',
'put',
'evict',
'evictAll',