Version

Documentation » Cache

3. Command Line Tools »

« 1. Introduction

2. Installation

To begin, add the dependent bundles to the vendor directory buy running this command:

composer require sonata-project/cache-bundle

Now, add the new Bundle to bundles.php file:

<?php

// config/bundles.php

return [
    //...
    Sonata\CacheBundle\SonataCacheBundle::class => ['all' => true],
];

Note

If you are not using Symfony Flex, you should enable bundles in your AppKernel.php.

<?php

// app/AppKernel.php

public function registerbundles()
{
    return array(
        // Vendor specifics bundles
        new Sonata\CacheBundle\SonataCacheBundle(),
    );
}

2.1. Configuration

To use the CacheBundle, add the following lines to your application configuration file:

# config/packages/sonata.yaml
sonata_cache:
    caches:
        esi:
            token: an unique security key # a random one is generated by default
            servers:
                - varnishadm -T 127.0.0.1:2000 {{ COMMAND }} "{{ EXPRESSION }}"

        ssi:
            token: an unique security key # a random one is generated by default

        mongo:
            database:   cache
            collection: cache
            servers:
                - {host: 127.0.0.1, port: 27017, user: username, password: pASS'}
                - {host: 127.0.0.2}

        memcached:
            prefix: test     # prefix to ensure there is no clash between instances
            servers:
                - {host: 127.0.0.1, port: 11211, weight: 0}

        predis:
            servers:
                - {host: 127.0.0.1, port: 6379, database: 42}

        apc:
            token:  s3cur3   # token used to clear the related cache
            prefix: test     # prefix to ensure there is no clash between instances
            servers:
                - { domain: kooqit.local, ip: 127.0.0.1, port: 80 }
            timeout:
                RCV: { sec: 5, usec: 0 }
                SND: { sec: 5, usec: 0 }

        symfony:
            token: s3cur3 # token used to clear the related cache
            php_cache_enabled: true # Optional (default: false), clear OPcache
            types: [mytype1, mycustomtype2] # Optional, you can restrict allowed cache types
            servers:
                - { domain: kooqit.local, ip: 127.0.0.1, port: 80 }
            timeout:
                RCV: { sec: 2, usec: 0 }
                SND: { sec: 2, usec: 0 }

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

For APC and Symfony caches, you can specify a basic parameter for servers definition (useful to clear cache for staging area behind this kind of protection)

apc:
    token:  s3cur3   # token used to clear the related cache
    prefix: test     # prefix to ensure there is no clash between instances
    servers:
        - { domain: kooqit.local, ip: 127.0.0.1, port: 80, basic: 'user:login' }

At the end of your routing file, add the following lines:

# config/routes.yaml
sonata_cache_cache:
    resource: '@SonataCacheBundle/Resources/config/routing/cache.xml'
    prefix: /

Note

If you are not using Symfony Flex, routes should be added to app/config/routing.yml.

Found a typo? Something is wrong in this documentation? Just fork and edit it!