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

Merge pull request #964 from FabioBatSilva/DDC-2943

[SLC][DDC-2943] Disable slc for pagination queries
This commit is contained in:
Benjamin Eberlei 2014-03-01 12:46:52 +01:00
commit f0a6eb8723
3 changed files with 101 additions and 0 deletions

View File

@ -729,3 +729,10 @@ should be used in conjunction with distributed caching system such as memcached,
Caches should be used with care when using a load-balancer if you don't share the cache.
While using APC or any file based cache update occurred in a specific machine would not reflect to the cache in other machines.
Paginator
~~~~~~~~~
Count queries generated by ``Doctrine\ORM\Tools\Pagination\Paginator`` are not cached by second-level cache.
Although entities and query result are cached count queries will hit the database every time.

View File

@ -182,12 +182,14 @@ class Paginator implements \Countable, \IteratorAggregate
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids));
$whereInQuery->setFirstResult(null)->setMaxResults(null);
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids);
$whereInQuery->setCacheable($this->query->isCacheable());
$result = $whereInQuery->getResult($this->query->getHydrationMode());
} else {
$result = $this->cloneQuery($this->query)
->setMaxResults($length)
->setFirstResult($offset)
->setCacheable($this->query->isCacheable())
->getResult($this->query->getHydrationMode())
;
}
@ -208,6 +210,7 @@ class Paginator implements \Countable, \IteratorAggregate
$cloneQuery = clone $query;
$cloneQuery->setParameters(clone $query->getParameters());
$cloneQuery->setCacheable(false);
foreach ($query->getHints() as $name => $value) {
$cloneQuery->setHint($name, $value);

View File

@ -0,0 +1,91 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\ORM\Cache;
/**
* @group DDC-2943
*/
class DDC2943Test extends OrmFunctionalTestCase
{
public function setUp()
{
$this->enableSecondLevelCache();
$this->useModelSet('cache');
parent::setUp();
}
private function loadFixtures()
{
$this->_em->persist(new Country("Brazil"));
$this->_em->persist(new Country("Canada"));
$this->_em->persist(new Country("Germany"));
$this->_em->persist(new Country("France"));
$this->_em->flush();
$this->_em->clear();
}
public function testIssue()
{
$this->loadFixtures();
$region = $this->_em->getCache()->getEntityCacheRegion(Country::CLASSNAME);
$dql = 'SELECT c FROM Doctrine\Tests\Models\Cache\Country c';
$query = $this->_em->createQuery($dql)
->setCacheable(true)
->setFirstResult(0)
->setMaxResults(2);
$this->assertPaginatorQueryPut(new Paginator(clone $query), $region->getName(), 4, 2);
$this->_em->clear();
$this->secondLevelCacheLogger->clearStats();
$this->assertPaginatorQueryHit(new Paginator(clone $query), $region->getName(), 4, 2);
}
public function testIssueNonFetchJoin()
{
$this->loadFixtures();
$region = $this->_em->getCache()->getEntityCacheRegion(Country::CLASSNAME);
$dql = 'SELECT c FROM Doctrine\Tests\Models\Cache\Country c';
$query = $this->_em->createQuery($dql)
->setCacheable(true)
->setFirstResult(0)
->setMaxResults(2);
$this->assertPaginatorQueryPut(new Paginator(clone $query, false), $region->getName(), 4, 2);
$this->_em->clear();
$this->secondLevelCacheLogger->clearStats();
$this->assertPaginatorQueryHit(new Paginator(clone $query, false), $region->getName(), 4, 2);
}
public function assertPaginatorQueryPut(Paginator $paginator, $regionName, $count, $pageSize)
{
$this->assertCount($count, $paginator);
$this->assertCount($pageSize, $paginator->getIterator());
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionHitCount(Cache::DEFAULT_QUERY_REGION_NAME));
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionPutCount(Cache::DEFAULT_QUERY_REGION_NAME));
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionHitCount($regionName));
$this->assertEquals($count, $this->secondLevelCacheLogger->getRegionPutCount($regionName));
}
public function assertPaginatorQueryHit(Paginator $paginator, $regionName, $count, $pageSize)
{
$this->assertCount($count, $paginator);
$this->assertCount($pageSize, $paginator->getIterator());
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionHitCount(Cache::DEFAULT_QUERY_REGION_NAME));
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionPutCount(Cache::DEFAULT_QUERY_REGION_NAME));
$this->assertEquals($pageSize, $this->secondLevelCacheLogger->getRegionHitCount($regionName));
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionPutCount($regionName));
}
}