Skip to content

Commit

Permalink
Merge pull request #794 from tentwofour/feature_seo_twig_extension_ca…
Browse files Browse the repository at this point in the history
…che_3.3

[SeoBundle] Add cache to SeoTwigExtension
  • Loading branch information
Roderik van der Veer committed Oct 31, 2015
2 parents 0535fff + 93ad075 commit 7a0a453
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Kunstmaan/SeoBundle/Twig/SeoTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class SeoTwigExtension extends Twig_Extension
*/
private $websiteTitle;

/**
* Saves querying the db multiple times, if you happen to use any of the defined
* functions more than once in your templates
* @var array
*/
private $seoCache = [];

/**
* @param EntityManager $em
*/
Expand Down Expand Up @@ -61,7 +68,15 @@ public function getFunctions()
*/
public function getSeoFor(AbstractPage $entity)
{
return $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($entity);
$key = md5(get_class($entity).$entity->getId());

if (!array_key_exists($key, $this->seoCache))
{
$seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($entity);
$this->seoCache[$key] = $seo;
}

return $this->seoCache[$key];
}

/**
Expand Down

0 comments on commit 7a0a453

Please sign in to comment.