intaro / memcached-tags-bundle

Doctrine 的本地查询构建器

安装: 52

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v0.2 2014-08-18 12:08 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:54:25 UTC


README

关于

Memcached Tags Bundle 允许为 doctrine 查询结果缓存添加标签,并通过标签清除缓存。基于 LswMemcacheBundle

安装

在您的 composer.json 文件中要求该包

{
    "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
```