Skip to content

Instantly share code, notes, and snippets.

@liuggio
Created January 29, 2013 15:48
Show Gist options
  • Save liuggio/4665238 to your computer and use it in GitHub Desktop.
Save liuggio/4665238 to your computer and use it in GitHub Desktop.
Cached repository
<?php
namespace Tvision\Bundle\BookingCoreBundle\Repository;
/**
*
* usage
* Class MyRepository extends CachedRepository
* $query->setMaxResults(1);
* $this->setCache($query, __METHOD__);
*/
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\AbstractQuery;
abstract class CachedRepository extends EntityRepository
{
CONST DEFAULT_LIFETIME = 240;
public function setCache(AbstractQuery $query, $prefix, $lifetime = self::DEFAULT_LIFETIME)
{
$key = serialize($query->getParameters());
$query->useResultCache(true, $lifetime , $prefix.$key);
$query->useQueryCache(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment