ang3 / doctrine-cache-invalidator-bundle
Doctrine cache invalidator.
Installs: 73
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Type:symfony-bundle
Requires
- php: ^7.1.3
- doctrine/orm: ^2.5
- psr/log: ^1.0
- symfony/config: ^4.0
- symfony/dependency-injection: ^4.0
- symfony/expression-language: ^4.0
- symfony/http-kernel: ^4.0
Requires (Dev)
- phpstan/phpstan-doctrine: ^0.10
This package is not auto-updated.
Last update: 2024-12-22 07:04:09 UTC
README
A Symfony bundle to manage doctrine result cache invalidations.
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:
$ 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.
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:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Ang3\Bundle\DoctrineCacheInvalidatorBundle\Ang3DoctrineCacheInvalidatorBundle(), ); // ... } // ... }
Step 3: Configure your app (optional)
No config is required, but you can configure a specific logger:
# 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
.
// 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.
// 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 :
id
(required) : result cache id to deleteparameters
(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:
// 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).
// 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.
For each expression these variables are passed during the evaluation :
this
(object) the added/edited/deleted entityeventType
(string) 'insert', 'update' ou 'delete'changeSet
an instance of classAng3\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) !