DoctrineCacheInvalidatorBundle
, (*1)
A Symfony bundle to manage doctrine result cache invalidations., (*2)
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:, (*3)
$ composer require ang3/doctrine-cache-invalidator-bundle
This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation., (*4)
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:, (*5)
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Ang3\Bundle\DoctrineCacheInvalidatorBundle\Ang3DoctrineCacheInvalidatorBundle(),
);
// ...
}
// ...
}
No config is required, but you can configure a specific logger:, (*6)
# app/config/config.yml
ang3_doctrine_cache_invalidator:
logger: ~ # An optionnal logger ID (Psr/Log/LoggerInterface)
Usage
Context
Suppose you have a result cache ID registered in the repository class AppBundle\Repository\MyRepository
., (*7)
// src/AppBundle/Repository/MyRepository.php
// ...
$qb
// ...
// Get the query
->getQuery()
// Register an ID to invalidate results
->setResultCacheId('my_custom_id')
// Get the result as you want
->getResult() # Or what ever
;
// ...
Invalidation
The invalidation process is during the "flush" entity operation. On each entity, the resolver is called so as to define potential cache indexes to delete. To do that, you just have to write an annotation on concerned entities and configure it., (*8)
// src/AppBundle/Entity/EntityA.php
// Do not forget the "use" statement
use Ang3\DoctrineCacheInvalidatorBundle\Annotation\CacheInvalidation;
// ...
/**
* @CacheInvalidation(id="my_custom_id")
*/
class EntityA
{
// ...
}
Options
The CacheInvalidation annotation has three parameters :, (*9)
-
id
(required) : result cache id to delete
-
parameters
(optional) : associative array of potential ID parameters (the value is represented by an expression)
-
validation
(optional) : an other expression to validate a specific entity
Dynamic ID and parameters
In case of dynamic result cache ID, you can register variables like PHP (with $
), but you have to specify the related expression under the "parameters" option. I suggest you to use the dot .
to end the variable name:, (*10)
// src/AppBundle/Entity/EntityA.php
// Do not forget the "use" statement
use Ang3\DoctrineCacheInvalidatorBundle\Annotation\CacheInvalidation;
// ...
/**
* @CacheInvalidation(id="my_custom_.$id", parameters={"id":"this.getId()"})
*/
class EntityA
{
// ...
}
Validation
You can also submit the entity to a validation during process. You just have to specify an expression so to as to return a boolean value. The result cache ID is deleted if the expression returns TRUE or equivalent (castable)., (*11)
// src/AppBundle/Entity/EntityA.php
// Do not forget the "use" statement
use Ang3\DoctrineCacheInvalidatorBundle\Annotation\CacheInvalidation;
// ...
/**
* @CacheInvalidation(id="my_custom_.$id", parameters={"id":"this.getId()"}, validation="eventType == 'update'")
*/
class EntityA
{
// ...
}
Expressions
All used expressions are evaluated by the component symfony/expression-language., (*12)
For each expression these variables are passed during the evaluation :, (*13)
-
this
(object) the added/edited/deleted entity
-
eventType
(string) 'insert', 'update' ou 'delete'
-
changeSet
an instance of class Ang3\Bundle\DoctrineCacheInvalidatorBundle\Helper\EntityChangeSetHelper
Todo
- [ ] Use more than the default entity manager (get doctrine registry in the listener, then add 'entity_manager' option in the annotation and check entity manager during the flush operation) !