Download the PHP package asm89/twig-cache-extension without Composer
On this page you can find all versions of the php package asm89/twig-cache-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download asm89/twig-cache-extension
More information about asm89/twig-cache-extension
Files in asm89/twig-cache-extension
Package twig-cache-extension
Short Description Cache fragments of templates directly within Twig.
License MIT
Homepage https://github.com/asm89/twig-cache-extension
Informations about the package twig-cache-extension
Twig cache extension
This extension moved to the Twig organization. Check out the repository over there instead: https://github.com/twigphp/twig-cache-extension.
The missing cache extension for Twig. The extension allows for caching rendered parts of templates using several cache strategies.
Installation
The extension is installable via composer:
Quick start
Setup
A minimal setup for adding the extension with the LifeTimeCacheStrategy
and
doctrine array cache is as following:
Want to use a PSR-6 cache pool?
Instead of using the default DoctrineCacheAdapter
the extension also has
a PSR-6
compatible adapter. You need to instantiate one of the cache pool
implementations as can be found on: http://php-cache.readthedocs.io/en/latest/
Example: Making use of the ApcuCachePool
via the PsrCacheAdapter
:
Usage
To cache a part of a template in Twig surround the code with a cache
block.
The cache block takes two parameters, first an "annotation" part, second the
"value" the cache strategy can work with. Example:
Cache blocks can be nested:
The annotation can also be an expression:
Cache strategies
The extension ships with a few cache strategies out of the box. Setup and usage of all of them is described below.
Lifetime
See the "Quick start" for usage information of the LifetimeCacheStrategy
.
Generational
Strategy for generational caching.
In theory the strategy only saves fragments to the cache with infinite lifetime. The key of the strategy lies in the fact that the keys for blocks will change as the value for which the key is generated changes.
For example: entities containing a last update time, would include a timestamp in the key. For an interesting blog post about this type of caching see: http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works
Blackhole
Strategy for development mode.
In development mode it often not very useful to cache fragments. The blackhole strategy provides an easy way to not cache anything it all. It always generates a new key and does not fetch or save any fragments.
Setup
In order to use the strategy you need to setup a KeyGenerator
class that is
able to generate a cache key for a given value.
The following naive example always assumes the value is an object with the methods
getId()
and getUpdatedAt()
method. The key then composed from the class
name, the id and the updated time of the object:
Next the GenerationalCacheStrategy
needs to be setup with the keygenerator.
Usage
The strategy expects an object as value for determining the cache key of the block:
Using multiple strategies
Different cache strategies are useful for different usecases. It is possible to
mix multiple strategies in an application with the
IndexedChainingCacheStrategy
. The strategy takes an array of 'name' => $strategy
and delegates the caching to the appropriate strategy.
Setup
Usage
The strategy expects an array with as key the name of the strategy which it needs to delegate to and as value the appropriate value for the delegated strategy.
Implementing a cache strategy
Creating separate caches for different access levels, languages or other
usecases can be done by implementing a custom cache strategy. In order to do so
implement the CacheProviderInterface
. It is recommended to use composition
and wrap a custom strategy around an existing one.
Authors
Alexander [email protected]
License
twig-cache-extension is licensed under the MIT License - see the LICENSE file for details