diff --git a/lib/Doctrine/ORM/Cache/MultiGetRegion.php b/lib/Doctrine/ORM/Cache/MultiGetRegion.php index 6de9c888d..be32b08f0 100644 --- a/lib/Doctrine/ORM/Cache/MultiGetRegion.php +++ b/lib/Doctrine/ORM/Cache/MultiGetRegion.php @@ -23,7 +23,7 @@ namespace Doctrine\ORM\Cache; /** * Defines a region that supports multi-get reading. * - * With one method call we can get multipe items. + * With one method call we can get multiple items. * * @since 2.5 * @author Asmir Mustafic @@ -31,7 +31,7 @@ namespace Doctrine\ORM\Cache; interface MultiGetRegion { /** - * Get all items from the cache indentifed by $keys. + * Get all items from the cache identified by $keys. * It returns NULL if some elements can not be found. * * @param CollectionCacheEntry $collection The collection of the items to be retrieved. diff --git a/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php b/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php index 39bf9c9e8..7ecf73311 100644 --- a/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php @@ -57,8 +57,9 @@ class DefaultMultiGetRegion extends DefaultRegion public function getMultiple(CollectionCacheEntry $collection) { $keysToRetrieve = array(); + foreach ($collection->identifiers as $index => $key) { - $keysToRetrieve[$index] = $this->name . '_' . $key->hash; + $keysToRetrieve[$index] = $this->getCacheEntryKey($key); } $items = $this->cache->fetchMultiple($keysToRetrieve); @@ -70,6 +71,7 @@ class DefaultMultiGetRegion extends DefaultRegion foreach ($keysToRetrieve as $index => $key) { $returnableItems[$index] = $items[$key]; } + return $returnableItems; } } diff --git a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php index f4525ee11..3f214d0b0 100644 --- a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php @@ -36,6 +36,8 @@ use Doctrine\ORM\Cache\Region; */ class DefaultRegion implements Region { + const REGION_KEY_SEPARATOR = '_'; + /** * @var CacheAdapter */ @@ -84,7 +86,7 @@ class DefaultRegion implements Region */ public function contains(CacheKey $key) { - return $this->cache->contains($this->name . '_' . $key->hash); + return $this->cache->contains($this->getCacheEntryKey($key)); } /** @@ -92,7 +94,7 @@ class DefaultRegion implements Region */ public function get(CacheKey $key) { - return $this->cache->fetch($this->name . '_' . $key->hash) ?: null; + return $this->cache->fetch($this->getCacheEntryKey($key)) ?: null; } /** @@ -100,30 +102,29 @@ class DefaultRegion implements Region */ public function getMultiple(CollectionCacheEntry $collection) { - $keysToRetrieve = array(); + $result = array(); - foreach ($collection->identifiers as $index => $key) { - $keysToRetrieve[$index] = $this->name . '_' . $key->hash; - } + foreach ($collection->identifiers as $key) { + $entryKey = $this->getCacheEntryKey($key); + $entryValue = $this->cache->fetch($entryKey); - $items = array_filter( - array_map([$this->cache, 'fetch'], $keysToRetrieve), - function ($retrieved) { - return false !== $retrieved; + if ($entryValue === false) { + return null; } - ); - if (count($items) !== count($keysToRetrieve)) { - return null; + $result[] = $entryValue; } - $returnableItems = array(); + return $result; + } - foreach ($keysToRetrieve as $index => $key) { - $returnableItems[$index] = $items[$key]; - } - - return $returnableItems; + /** + * @param CacheKey $key + * @return string + */ + protected function getCacheEntryKey(CacheKey $key) + { + return $this->name . self::REGION_KEY_SEPARATOR . $key->hash; } /** @@ -131,7 +132,7 @@ class DefaultRegion implements Region */ public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null) { - return $this->cache->save($this->name . '_' . $key->hash, $entry, $this->lifetime); + return $this->cache->save($this->getCacheEntryKey($key), $entry, $this->lifetime); } /** @@ -139,7 +140,7 @@ class DefaultRegion implements Region */ public function evict(CacheKey $key) { - return $this->cache->delete($this->name . '_' . $key->hash); + return $this->cache->delete($this->getCacheEntryKey($key)); } /** diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php index 94cc99e35..914bcf191 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php @@ -3,10 +3,12 @@ namespace Doctrine\Tests\ORM\Cache; use Doctrine\Common\Cache\ArrayCache; +use Doctrine\ORM\Cache\CollectionCacheEntry; use Doctrine\ORM\Cache\Region\DefaultRegion; use Doctrine\Tests\Mocks\CacheEntryMock; use Doctrine\Tests\Mocks\CacheKeyMock; + /** * @group DDC-2183 */ @@ -72,4 +74,27 @@ class DefaultRegionTest extends AbstractRegionTest $region->evictAll(); } + + public function testGetMulti() + { + $key1 = new CacheKeyMock('key.1'); + $value1 = new CacheEntryMock(array('id' => 1, 'name' => 'bar')); + + $key2 = new CacheKeyMock('key.2'); + $value2 = new CacheEntryMock(array('id' => 2, 'name' => 'bar')); + + $this->assertFalse($this->region->contains($key1)); + $this->assertFalse($this->region->contains($key2)); + + $this->region->put($key1, $value1); + $this->region->put($key2, $value2); + + $this->assertTrue($this->region->contains($key1)); + $this->assertTrue($this->region->contains($key2)); + + $actual = $this->region->getMultiple(new CollectionCacheEntry(array($key1, $key2))); + + $this->assertEquals($value1, $actual[0]); + $this->assertEquals($value2, $actual[1]); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php index 4c3258a12..091ec672a 100644 --- a/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php @@ -22,10 +22,10 @@ class MultiGetRegionTest extends AbstractRegionTest public function testGetMulti() { $key1 = new CacheKeyMock('key.1'); - $value1 = new CacheEntryMock(array('id'=>1, 'name' => 'bar')); + $value1 = new CacheEntryMock(array('id' => 1, 'name' => 'bar')); $key2 = new CacheKeyMock('key.2'); - $value2 = new CacheEntryMock(array('id'=>2, 'name' => 'bar')); + $value2 = new CacheEntryMock(array('id' => 2, 'name' => 'bar')); $this->assertFalse($this->region->contains($key1)); $this->assertFalse($this->region->contains($key2)); @@ -33,6 +33,9 @@ class MultiGetRegionTest extends AbstractRegionTest $this->region->put($key1, $value1); $this->region->put($key2, $value2); + $this->assertTrue($this->region->contains($key1)); + $this->assertTrue($this->region->contains($key2)); + $actual = $this->region->getMultiple(new CollectionCacheEntry(array($key1, $key2))); $this->assertEquals($value1, $actual[0]);