Last active
August 29, 2015 14:03
-
-
Save mustafaileri/4f3933353f4962bdd676 to your computer and use it in GitHub Desktop.
Cache Invalidation with Doctrine2 EventListeners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Joker\Bundle\CoreBundle\EventListener; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
class DoctrineEventListener | |
{ | |
/** @var \Joker\Bundle\CatalogBundle\DoctrineEventListener\Listener */ | |
private $catalogDoctrineEventListener; | |
/** @var \Joker\Bundle\CartBundle\DoctrineEventListener\Listener */ | |
private $cartDoctrineEventListener; | |
/** @var\Joker\Bundle\CartBundle\DoctrineEventListener\Listener */ | |
private $cmsDoctrineEventListener; | |
/** @var \Joker\Bundle\CampaignBundle\DoctrineEventListener\Listener; */ | |
private $campaignDoctrineEventListener; | |
private $entityServiceMap = array(); | |
public function __construct($productListener, $cartListener, $cmsListener, $campaignListener) | |
{ | |
$this->catalogDoctrineEventListener = $productListener; | |
$this->cartDoctrineEventListener = $cartListener; | |
$this->cmsDoctrineEventListener = $cmsListener; | |
$this->campaignDoctrineEventListener = $campaignListener; | |
$this->mapEntityToService(); | |
} | |
private function mapEntityToService() | |
{ | |
$this->entityServiceMap['\ProductBundle\Entity\Product'] = $this->catalogDoctrineEventListener; | |
$this->entityServiceMap['\CatalogBundle\Entity\Catalog'] = $this->catalogDoctrineEventListener; | |
$this->entityServiceMap['\CartBundle\Entity\Cart'] = $this->cartDoctrineEventListener; | |
$this->entityServiceMap['\CmsBundle\Entity\Folder'] = $this->cmsDoctrineEventListener; | |
$this->entityServiceMap['\CmsBundle\Entity\Item'] = $this->cmsDoctrineEventListener; | |
$this->entityServiceMap['\CampaignBundle\Entity\Campaign'] = $this->campaignDoctrineEventListener; | |
} | |
public function postPersist(LifecycleEventArgs $args) | |
{ | |
$entityName = $this->getEntityName($args); | |
if (array_key_exists($entityName, $this->entityServiceMap) && | |
method_exists($this->entityServiceMap[$entityName], 'postPersist') | |
) { | |
$this->entityServiceMap[$entityName]->postUpdate($args); | |
} | |
} | |
public function postUpdate(LifecycleEventArgs $args) | |
{ | |
$entityName = $this->getEntityName($args); | |
if (array_key_exists($entityName, $this->entityServiceMap) && | |
method_exists($this->entityServiceMap[$entityName], 'postUpdate') | |
) { | |
$this->entityServiceMap[$entityName]->postUpdate($args); | |
} | |
} | |
public function preUpdate(LifecycleEventArgs $args) | |
{ | |
$entityName = $this->getEntityName($args); | |
if ($entityName && array_key_exists($entityName, $this->entityServiceMap) && | |
method_exists($this->entityServiceMap[$entityName], 'preUpdate') | |
) { | |
$this->entityServiceMap[$entityName]->preUpdate($args); | |
} | |
} | |
private function getEntityName(LifecycleEventArgs $args) | |
{ | |
$entityArray = explode("Joker\\Bundle", get_class($args->getEntity())); | |
return (isset($entityArray[1])) ? $entityArray[1] : false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Joker\Bundle\CatalogBundle\DoctrineEventListener; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
use Joker\Bundle\CatalogBundle\Entity\Catalog; | |
use Joker\Bundle\CatalogBundle\Service\CatalogService; | |
use Joker\Bundle\ProductBundle\Entity\Product; | |
use OldSound\RabbitMqBundle\RabbitMq\Producer; | |
class Listener | |
{ | |
private $cacheDriver; | |
/** @var Producer */ | |
private $producer; | |
public function __construct($cacheDriver, $producer) | |
{ | |
$this->cacheDriver = $cacheDriver; | |
$this->producer = $producer; | |
} | |
public function postUpdate(LifecycleEventArgs $args) | |
{ | |
if ($args->getEntity() instanceof Product) { | |
/** @var Catalog $entity */ | |
$entity = $args->getEntity()->getCatalog(); | |
} else { | |
/** @var Catalog $entity */ | |
$entity = $args->getEntity(); | |
} | |
$catalogId = $entity->getId(); | |
$this->invalidateCache($catalogId); | |
$this->sendSolrQueue($catalogId); | |
} | |
public function postPersist(LifecycleEventArgs $args) | |
{ | |
$this->postUpdate($args); | |
} | |
/** | |
* @param $catalogId | |
*/ | |
private function invalidateCache($catalogId) | |
{ | |
$cacheKey = CatalogService::CACHE_KEY; | |
$this->cacheDriver->delete($cacheKey . $catalogId); | |
$this->cacheDriver->delete($cacheKey . 'breadcrumb_' . $catalogId); | |
$this->cacheDriver->delete($cacheKey . 'grouped_' . $catalogId); | |
} | |
private function sendSolrQueue($catalogId) | |
{ | |
try { | |
$this->producer->publish($catalogId, "Solr"); | |
} catch (\Exception $e) { | |
//@TODO Log this state | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
joker.core.event_listener.doctrine_event_listener: | |
class: Joker\Bundle\CoreBundle\EventListener\DoctrineEventListener | |
arguments: | |
- '@joker.catalog.doctrine_event_listener.listener' | |
- '@joker.cart.doctrine_event_listener.listener' | |
- '@joker.folder.doctrine_event_listener.listener' | |
- '@joker.campaign.doctrine_event_listener.listener' | |
tags: | |
- { name: doctrine.event_listener, event: postPersist, method: postPersist } | |
- { name: doctrine.event_listener, event: postUpdate, method: postUpdate } | |
- { name: doctrine.event_listener, event: preUpdate, method: preUpdate } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment