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

TwigCacheBundle

by EmanueleMinotto

Symfony Bundle for asm89/twig-cache-extension

Twig cache extension Bundle

Build Status
SensioLabs Insight
Coverage Status
Scrutinizer Code Quality
Total Downloads

Symfony Bundle for asm89/twig-cache-extension.

API: emanueleminotto.github.io/TwigCacheBundle

Install bundle using Composer

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

$ composer require emanueleminotto/twig-cache-bundle

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Add Bundle to AppKernel

Then, enable the bundle by adding the following line in the app/AppKernel.php
file of your project:

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new EmanueleMinotto\TwigCacheBundle\TwigCacheBundle(),
        );
    }
}

Configure services

This bundle allows to easily configure the caching service the extension will use for the caching. The extension
by default supports instances of Doctrine\Common\Cache\Cache but allows you to use packages that
provide a psr/cache-implementation.

# app/config/config.yml
twig_cache:
    service: cache_service # instance of Doctrine\Common\Cache\Cache or Psr\Cache\CacheItemPoolInterface

Configuring a PSR-6 Cache pool implementation is possible by changing the extension's default adapter class
to the PsrCacheAdapter that is provided with the extension.

# parameters.yml
twig_cache.adapter.class: Asm89\Twig\CacheExtension\CacheProvider\PsrCacheAdapter

After that, you should install a package that provides a psr/cache-implementation. A wide
variety of implementations already can be found at: http://php-cache.readthedocs.io/
After installing an adapter with composer, it can be configured to do the caching for this bundle.
The CacheBundle allows easy configuration by creating a Symfony
service for the cache pool adapter.

# config.yml
cache_adapter:
    providers:
        twig_apcu:
            factory: 'cache.factory.apcu'

twig_cache:
    service: cache.provider.twig_apcu

Usage

The default strategy is the IndexedChainingCacheStrategy so you can use directly this code in your
Twig templates.

{# delegate to lifetime strategy #}
{% cache 'v1/summary' {time: 300} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

{# delegate to generational strategy #}
{% cache 'v1/summary' {gen: item} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

Readings:

License

This bundle is under the MIT license. See the complete license in the bundle:

Resources/meta/LICENSE
The MIT License (MIT)

Copyright (c) 2016 Emanuele Minotto

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.
twig_cache:
profiler: %kernel.debug%
service: ~ # Required
strategy: twig_cache.strategy

# service id that implements KeyGeneratorInterface to generate a key for template cache
key_generator: twig_cache.strategy.spl_object_hash_key_generator
  • Merge pull request #22 from EmanueleMinotto/profiler_fix
    By web-flow, 2 years ago
  • Remove .idea
    By web-flow, 2 years ago
  • Splitted data collector from extension
    By toooni, 3 years ago
  • php-cs-fix
    By toooni, 3 years ago
  • Merge pull request #21 from ahilles107/patch-1
    By web-flow, 3 years ago
  • Implement reset method for ProfilerExtension
    By ahilles107, 3 years ago
  • Removed deprecations
    By EmanueleMinotto, 3 years ago
  • Update unsupported elements
    By EmanueleMinotto, 3 years ago
  • Merge pull request #16 from DenisMedved/master
    By web-flow, 3 years ago
  • fix deprecation message.
    By DenisMedved, 4 years ago
  • Merge pull request #14 from rvanlaak/psr6
    By web-flow, 4 years ago
  • Document PSR-6 cache adapters
    By Rvanlaak, 4 years ago
  • Merge pull request #11 from mente/opt-key-generator
    By EmanueleMinotto, 5 years ago
  • update reference config
    By mente, 5 years ago
  • ability to redefine key generator as service
    By mente, 5 years ago
  • Update README.md
    By EmanueleMinotto, 5 years ago
  • Updated license year
    By EmanueleMinotto, 5 years ago
  • Merge pull request #10 from takeit/patch-2
    By EmanueleMinotto, 5 years ago
  • Remove inline styles from image
    By takeit, 5 years ago
  • get stable satooshi/php-coveralls version
    By EmanueleMinotto, 5 years ago
  • coding style fixes
    By EmanueleMinotto, 5 years ago
  • improved tests
    By EmanueleMinotto, 5 years ago
  • removed tests from production optimization
    By EmanueleMinotto, 5 years ago
  • updated travis configuration
    By EmanueleMinotto, 5 years ago
  • updated composer dependencies
    By EmanueleMinotto, 5 years ago
  • fixed phpdoc param
    By EmanueleMinotto, 5 years ago
  • added php7
    By EmanueleMinotto, 5 years ago
  • Merge pull request #8 from EmanueleMinotto/analysis-zddg9z
    By EmanueleMinotto, 5 years ago
  • Applied fixes from StyleCI
    By StyleCIBot, 5 years ago
  • improved docblock
    By EmanueleMinotto, 5 years ago