intaro / memcached-tags-bundle
Native query builder for doctrine
Installs: 52
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.3.0
- doctrine/orm: 2.4.*
- leaseweb/memcache-bundle: *
This package is not auto-updated.
Last update: 2024-11-19 04:37:08 UTC
README
About
The Memcached Tags Bundle allows to add tags for doctrine query result cache and clear result cache by tags. Based on LswMemcacheBundle
Installation
Require the bundle in your composer.json file:
{
"require": {
"intaro/memcached-tags-bundle": "dev-master"
}
}
```
Register the bundle:
```php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
new Intaro\MemcachedTagsBundle\IntaroMemcachedTagsBundle(),
);
}
```
Install the bundle:
```
$ composer update intaro/memcached-tags-bundle
```
## Usage ##
### Query ###
Create new Query with necessary cache life time and cache tags:
```php
$em = $container->get('doctrine')->getManager();
$em->createQuery('SELECT book FROM AcmeHelloBundle:Book book', 3600, [
'Acme\HelloBundle\Entity\Book'
]);
```
### NativeQuery ###
NativeQuery with cache tags works same as Query.
```php
$em = $container->get('doctrine')->getManager();
$em->createNativeQuery('SELECT * FROM book', $rsm, 3600, [
'Acme\HelloBundle\Entity\Book'
]);
```
### QueryBuilder ###
```php
$em = $container->get('doctrine')->getManager();
$builder = $em->createQueryBuilder()
->select('book')->from('AcmeHelloBundle:Book', 'book')
->useResultCache(true, 3600) //enable result cache and set cache life time
->setCacheTags(['Acme\HelloBundle\Entity\Book'])
->join('book.author', 'author')
->addCacheTag('Acme\HelloBundle\Entity\Author');
if ($disableTags) {
$builder->clearCacheTags();
}
$builder->getQuery()->getResult();
```
### Clear cache ###
```php
$em = $container->get('doctrine')->getManager();
$em->getRepository('AcmeHelloBundle:Book')->clearEntityCache();
// or
$em->tagsClear('Acme\HelloBundle\Entity\Book');
$book = $em->getRepository('AcmeHelloBundle:Book')->find($id);
$em->getRepository('AcmeHelloBundle:Book')->clearEntityCache($book->getId());
// or
$em->tagsClear('Acme\HelloBundle\Entity\Book[id="' . $book->getId() . '"]');
```
On entity insertions, update and deletes automatically clears cache for changed class names and changed entity id.
```php
$em = $container->get('doctrine')->getManager();
$book = $em->getRepository('AcmeHelloBundle:Book')->find(25);
$book->setName('New book');
$em->merge($book);
$em->flush();
// Tags Acme\HelloBundle\Entity\Book and Acme\HelloBundle\Entity\Book[id="25"] are cleared
```
### ManyToOne association cache ###
```php
use Intaro\MemcachedTagsBundle\Doctrine\Annotation\AssociationCache;
/**
* @ORM\Entity
* @ORM\Table(name="shelf")
* @AssociationCache(lifetime=100)
*/
class Shelf
{
...
}
/**
* @ORM\Entity
* @ORM\Table(name="shelf")
* @AssociationCache(lifetime=100, tags={"Acme\HelloBundle\Entity\Author", "Acme\HelloBundle\Entity\Book"})
*/
class Author
{
...
}
```
```php
$em = $container->get('doctrine')->getManager();
$book = $em->getRepository('AcmeHelloBundle:Book')->find(25);
$shelf = $book->getShelf();
// Cache with tag Acme\HelloBundle\Entity\Shelf[id="13"] will be loaded
```