Developed with love by KnpLabs Hire us for your project!
90

DoctrineCacheBundle

by doctrine

Symfony2 Bundle for Doctrine Cache

DoctrineCacheBundle

Symfony2 Bundle for Doctrine Cache

Master: Build Status

Master: Coverage Status

Installation

Installing this bundle can be done through these simple steps:

  1. Add this bundle to your project as a composer dependency:

    composer require doctrine/doctrine-cache-bundle
    
  2. Add this bundle in your application kernel:

    // app/AppKernel.php
    public function registerBundles()
    {
        // ...
        $bundles[] = new \Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle();
    
        return $bundles;
    }
    
  3. Check if the bundle is configured correctly:

    <!-- app/config/config.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:doctrine-cache="http://doctrine-project.org/schemas/symfony-dic/cache"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
            http://doctrine-project.org/schemas/symfony-dic/cache http://doctrine-project.org/schemas/symfony-dic/cache/doctrine_cache-1.0.xsd">
    
        <doctrine-cache:doctrine-cache>
             <doctrine-cache:provider name="my_apc_metadata_cache">
                <doctrine-cache:type>apc</doctrine-cache:type>
                <doctrine-cache:namespace>metadata_cache_ns</doctrine-cache:namespace>
             </doctrine-cache:provider>
            <doctrine-cache:provider name="my_apc_query_cache" namespace="query_cache_ns">
                <doctrine-cache:apc/>
            <doctrine-cache:provider>
        </doctrine-cache:doctrine-cache>
    </container>
    
    # app/config/config.yml
    
    doctrine_cache:
        providers:
            my_apc_metadata_cache:
                type: apc
                namespace: metadata_cache_ns
            my_apc_query_cache:
                namespace: query_cache_ns
                apc: ~
    

Usage

Simply use doctrine_cache.providers.{provider_name} to inject it into the desired service.

Check the following sample:

$apcCache   = $this->container->get('doctrine_cache.providers.my_apc_cache');
$arrayCache = $this->container->get('doctrine_cache.providers.my_array_cache');

Provider configuration

<!-- app/config/doctrine_cache.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<dic:container xmlns="http://doctrine-project.org/schemas/symfony-dic/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
        http://doctrine-project.org/schemas/symfony-dic/cache http://doctrine-project.org/schemas/symfony-dic/cache/doctrine_cache-1.0.xsd">

<srv:container>
    <doctrine-cache>
         <provider name="my_memcached_cache">
             <memcache>
                 <server host="memcached01.ss" port="11211"/>
                 <server>
                    <host>memcached01.ss</host>
                    <port>11211</port>
                 </server>
             </memcache>
         </provider>

         <provider name="my_riak_cache">
             <riak host="localhost" port="8087">
                 <bucket-name>my_bucket</bucket-name>
                 <bucket-property-list>
                     <allow-multiple>false</allow-multiple>
                     <n-value>1</n-value>
                 </bucket-property-list>
             </riak>
         </provider>
    </doctrine-cache>
</srv:container>
# app/config/doctrine_cache.yml

doctrine_cache:
    providers:
        my_memcached_cache:
            memcached:
                servers:
                    memcached01.ss: 11211
                    memcached02.ss:
                        port: 11211
        my_riak_cache:
            riak:
                host: localhost
                port: 8087
                bucket_name: my_bucket
                bucket_property_list:
                    allow_multiple: false
                    n_value: 1
See Cache providers for all supported cache provider and its specific configurations

Service aliases

<!-- app/config/doctrine_cache.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<dic:container xmlns="http://doctrine-project.org/schemas/symfony-dic/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
        http://doctrine-project.org/schemas/symfony-dic/cache http://doctrine-project.org/schemas/symfony-dic/cache/doctrine_cache-1.0.xsd">

<srv:container>
    <doctrine-cache>
        <alias key="cache_apc">my_apc_cache</alias>

        <provider name="my_apc_cache">
            <type>apc</type>
            <namespace>my_apc_cache_ns</namespace>
            <alias>apc_cache</alias>
        </provider>
    </doctrine-cache>
</srv:container>
# app/config/doctrine_cache.yml

doctrine_cache:
    aliases:
        cache_apc: my_apc_cache

    providers:
        my_apc_cache:
            type: apc
            namespace: my_apc_cache_ns
            aliases:
                - apc_cache

You can access the cache providers by using created aliases:

$apcCache  = $this->container->get('apc_cache');
$cacheApc  = $this->container->get('cache_apc');

Custom providers

Is possible to register a custom cache driver
```xml
<!-- app/config/doctrine_cache.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

srv:container
srv:services

<!-- ... -->
/srv:service
/srv:services

<doctrine-cache>
    <!-- register your custom cache provider -->
    <custom-provider type="my_custom_type">
        <prototype>my_custom_provider_service</prototype>
        <definition-class>MyCustomTypeDefinition</definition-class> <!-- optional configuration -->
    </custom-provider>

     <provider name="my_custom_type_provider">
        <my_custom_type>
             <config-foo>foo</config-foo>
             <config-bar>bar</config-bar>
         </my_custom_type>
     </provider>
</doctrine-cache>

/srv:container
```

# app/config/doctrine_cache.yml

services:
    my_custom_provider_service:
        class: "MyCustomType"
        # ...

doctrine_cache:
    custom_providers:
        my_custom_type:
            prototype:  "my_custom_provider_service"
            definition_class: "MyCustomTypeDefinition" # optional configuration

    providers:
        my_custom_type_provider:
            my_custom_type:
                config_foo: "foo"
                config_bar: "bar"
Definition class is a optional configuration that will parse option arguments given to your custom cache driver See CacheDefinition

Service parameter

Is possible to configure a cache provider using a specific connection/bucket/collection
```xml
<!-- app/config/doctrine_cache.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

srv:container
srv:services

<!-- ... -->
/srv:service

    <srv:service id="my_riak_bucket_service" class="Riak\Bucket">
        <!-- ... -->
    </srv:service>

    <srv:service id="my_memcached_connection_service" class="Memcached">
        <!-- ... -->
    </srv:service>
 </srv:services>

<doctrine-cache>
     <provider  name="service_bucket_riak_provider">
         <riak bucket-id="my_riak_bucket_service"/>
     </provider>

     <provider name="service_connection_riak_provider">
         <riak connection-id="my_riak_connection_service">
             <bucket-name>my_bucket_name</bucket-name>
         </riak>
     </provider>

     <provider name="service_connection_memcached_provider">
         <memcached connection-id="my_memcached_connection_service"/>
     </provider>
</doctrine-cache>

/srv:container
```

# app/config/doctrine_cache.yml

services:
    my_riak_connection_service:
        class: "Riak\Connection"
        # ...

    my_riak_bucket_service:
        class: "Riak\Bucket"
        # ...

    my_memcached_connection_service:
        class: "Memcached"
        # ...

doctrine_cache:
    providers:
        service_bucket_riak_provider:
            riak:
                bucket_id : "my_riak_bucket_service"

        service_connection_riak_provider:
            riak:
                connection_id: "my_riak_connection_service"
                bucket_name: "my_bucket_name"

        service_connection_memcached_provider:
            memcached:
                connection_id: "my_memcached_connection_service"

See Cache providers for all specific configurations

Symfony acl cache

<!-- app/config/doctrine_cache.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<dic:container xmlns="http://doctrine-project.org/schemas/symfony-dic/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
        http://doctrine-project.org/schemas/symfony-dic/cache http://doctrine-project.org/schemas/symfony-dic/cache/doctrine_cache-1.0.xsd">

<srv:container>
    <doctrine-cache>
        <acl-cache id="doctrine_cache.providers.acl_apc_provider"/>

        <provider name="acl_apc_provider" type="apc"/>
    </doctrine-cache>
</srv:container>
# app/config/doctrine_cache.yml

doctrine_cache:
    acl_cache:
        id: 'doctrine_cache.providers.acl_apc_provider'
    providers:
        acl_apc_provider:
            type: 'apc'

Check the following sample:
```php
/** @var $aclCache Symfony\Component\Security\Acl\Model\AclCacheInterface */
$aclCache = $this->container->get('security.acl.cache');



## Cache providers

#### apc
#### array
#### chain
    - providers - List of cache providers
#### couchbase
    - connection_id - Couchbase connection service id
    - hostnames     - couchbase hostname list
    - bucket_name   - couchbase bucket name
    - username      - couchbase username
    - password      - couchbase password
#### file_system
    - extension    - file extension
    - directory    - cache directory
#### mongodb
    - connection_id     - MongoClient service id
    - collection_id     - MongoCollection service id
    - server            - mongodb server uri
    - database_name     - mongodb database name
    - collection_name   - mongodb collection name
#### memcache
    - connection_id - Memcache connection service id
    - servers       - Server list
        - server
            - host - memcache host
            - port - memcache port
#### memcached
    - connection_id - Memcache connection service id
    - servers       - Server list
        - server
            - host - memcached host
            - port - memcached port
#### php_file
    - extension    - file extension
    - directory    - cache directory
#### redis
    - connection_id - Redis connection service id
    - host          - redis host
    - port          - redis port
#### riak
    - connection_id                 - Riak\Connection service id
    - bucket_id                     - Riak\Bucket service id
    - host                          - riak host
    - port                          - riak port
    - bucket_name                   - riak bucket name
    - bucket_property_list          - riak bucket configuration (property list)
        - allow_multiple: false     - riak bucket allow multiple configuration
        - n_value: 1                - riak bucket n-value configuration
#### sqlite3
    - connection_id - SQLite3 connection service id
    - file_name     - SQLite3 database file name
    - table_name    - Cache table name
#### void
#### xcache
#### wincache
#### zenddata

Check the [doctrine-cache documentation Page](http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html) for a better understanding.
Copyright (c) 2006-2012 Doctrine Project

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
doctrine_cache:
acl_cache:
id: ~
custom_providers:

# Prototype
type:
prototype: ~ # Required
definition_class: ~
aliases:

# Prototype
key: []
providers:

# Prototype
name:
namespace: ~
type: ~
apc: []
array: []
void: []
wincache: []
xcache: []
zenddata: []
custom_provider:
type: ~ # Required
options:

# Prototype
name: []
couchbase:
connection_id: ~
hostnames:

# Default:
- %doctrine_cache.couchbase.hostnames%
username: ~
password: ~
bucket_name: doctrine_cache
chain:
providers: []
memcached:
connection_id: ~
servers:

# Prototype
host:
host: %doctrine_cache.memcached.host%
port: %doctrine_cache.memcached.port%
memcache:
connection_id: ~
servers:

# Prototype
host:
host: %doctrine_cache.memcache.host%
port: %doctrine_cache.memcache.port%
file_system:
directory: %kernel.cache_dir%/doctrine/cache/file_system
extension: ~
php_file:
directory: %kernel.cache_dir%/doctrine/cache/phpfile
extension: ~
mongodb:
connection_id: ~
collection_id: ~
database_name: doctrine_cache
collection_name: doctrine_cache
server: %doctrine_cache.mongodb.server%
redis:
connection_id: ~
host: %doctrine_cache.redis.host%
port: %doctrine_cache.redis.port%
password: ~
database: ~
riak:
host: %doctrine_cache.riak.host%
port: %doctrine_cache.riak.port%
bucket_name: doctrine_cache
connection_id: ~
bucket_id: ~
bucket_property_list:
allow_multiple: ~
n_value: ~
sqlite3:
connection_id: ~
file_name: ~
table_name: ~
aliases: []
  • Merge pull request #42 from EmanueleMinotto/master
    By guilhermeblanco, 2 years ago
  • Merge pull request #43 from doctrine/missing-cache-providers
    By guilhermeblanco, 2 years ago
  • replaced some old configs with cache providers
    By EmanueleMinotto, 2 years ago
  • Updated documentation.
    By guilhermeblanco, 2 years ago
  • Added chain, void and sqlite3 cache providers. Improved couchbase provider support to accept a possible connection service id.
    By guilhermeblanco, 2 years ago
  • Merge pull request #41 from EllisV/master
    By guilhermeblanco, 2 years ago
  • typo
    By EmanueleMinotto, 2 years ago
  • Starting documentation
    By EmanueleMinotto, 2 years ago
  • Solving issue #40: Getting rid of symfony/framework-bundle dependency
    By , 2 years ago
  • Merge pull request #39 from dogiedog/master
    By guilhermeblanco, 3 years ago
  • Always add namespace to config
    By , 3 years ago
  • Merge pull request #34 from dbu/cleanup-xml-namespace
    By guilhermeblanco, 3 years ago
  • Use the correct XML namespace
    By dbu, 3 years ago
  • Merge pull request #33 from stof/patch-2
    By Ocramius, 3 years ago
  • Remove IDE-specific files from the gitignore
    By stof, 3 years ago
  • Merge pull request #31 from stof/patch-1
    By Ocramius, 3 years ago
  • Merge pull request #32 from stof/patch-2
    By Ocramius, 3 years ago
  • Merge pull request #25 from jrobeson/patch-1
    By Ocramius, 3 years ago
  • Update the README instruction
    By stof, 3 years ago
  • Run the testsuite with stable dependencies
    By stof, 3 years ago
  • Change the PHPUnit version to allow using current versions
    By stof, 3 years ago
  • Merge pull request #27 from stof/default_ports
    By beberlei, 3 years ago
  • Fix the default ports in the SymfonyBridge layer
    By stof, 3 years ago
  • Merge pull request #16 from dragonwize/Add_commands
    By guilhermeblanco, 3 years ago
  • use psr-4 autoloader
    By jrobeson, 3 years ago
  • Changed command namespace to doctrine:cache.
    By dragonwize, 3 years ago
  • Merge pull request #22 from cdaguerre/redis-conf
    By guilhermeblanco, 3 years ago
  • fixed cs
    By cdaguerre, 3 years ago
  • fixed redis configuration
    By cdaguerre, 3 years ago
  • added redis auth and database parameters
    By cdaguerre, 3 years ago