Enable cache for queries

This commit is contained in:
Nicolas Lœuillet 2016-09-01 20:20:12 +02:00
parent f997ae6afb
commit 0b0233b1ec
No known key found for this signature in database
GPG key ID: 5656BE27E1E34D0A
2 changed files with 36 additions and 8 deletions

View file

@ -3,6 +3,7 @@
namespace Wallabag\CoreBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Wallabag\CoreBundle\Entity\Tag;
@ -279,4 +280,20 @@ class EntryRepository extends EntityRepository
return $qb->getQuery()->getSingleScalarResult();
}
/**
* Enable cache for a query
*
* @param Query $query
*
* @return Query
*/
public function enableCache(Query $query)
{
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime(5);
return $query;
}
}

View file

@ -36,16 +36,27 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
return array();
}
$unreadEntries = $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()->getResult();
$starredEntries = $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()->getResult();
$archivedEntries = $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()->getResult();
$allEntries = $this->repository->getBuilderForAllByUser($user->getId())->getQuery()->getResult();
$unreadEntries = $this->repository->enableCache(
$this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
);
$starredEntries = $this->repository->enableCache(
$this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
);
$archivedEntries = $this->repository->enableCache(
$this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
);
$allEntries = $this->repository->enableCache(
$this->repository->getBuilderForAllByUser($user->getId())->getQuery()
);
return array(
'unreadEntries' => count($unreadEntries),
'starredEntries' => count($starredEntries),
'archivedEntries' => count($archivedEntries),
'allEntries' => count($allEntries),
'unreadEntries' => count($unreadEntries->getResult()),
'starredEntries' => count($starredEntries->getResult()),
'archivedEntries' => count($archivedEntries->getResult()),
'allEntries' => count($allEntries->getResult()),
);
}