Download the PHP package liip/cache-control-bundle without Composer
On this page you can find all versions of the php package liip/cache-control-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cache-control-bundle
UNMAINTAINED
This bundle is no longer maintained. Feel free to fork it if needed.
CacheControlBundle
This Bundle provides a way to set path based cache expiration headers via the app configuration and provides a helper to control the reverse proxy varnish.
This Bundle is Deprecated!
The LiipCacheControlBundle went into maintenance only mode. It is replaced by the FOSHttpCacheBundle.
See our migration guide for help how to transition to the new bundle.
This repository will stay available to not break existing installations, but there will only be minimal maintenance at most.
Installation with composer
Just add the following line to your projects composer.json require section:
Enable the module
Add this bundle to your application's kernel:
Cache control
Simply configure as many paths as needed with the given cache control rules:
The matches are tried from top to bottom, the first match is taken and applied.
Run app/console config:dump-reference liip_cache_control
to get the full list of configuration options.
About the path parameter
The path
, host
and controller
parameter of the rules represent a regular
expression that a page must match to use the rule.
For this reason, and it's probably not the behaviour you'd have expected, the
path ^/
will match any page.
If you just want to match the homepage you need to use the path ^/$
.
To match pages URLs with caching rules, this bundle uses the class
Symfony\Component\HttpFoundation\RequestMatcher
.
The unless_role
makes it possible to skip rules based on if the current
authenticated user has been granted the provided role.
Debug information
The debug parameter adds a X-Cache-Debug
header to each response that you
can use in your Varnish configuration.
Add the following code to your Varnish configuration to have debug headers added to the response if it is enabled:
Custom Varnish Parameters
Additionally to the default supported headers, you may want to set custom caching headers for varnish.
Custom Varnish Time-Outs
Varnish checks the Cache-Control
header of your response to set the TTL.
Sometimes you may want that varnish should cache your response for a longer
time than the browser. This way you can increase the performance by reducing
requests to the backend.
To achieve this you can set the reverse_proxy_ttl
option for your rule:
This example will add the header X-Reverse-Proxy-TTL: 300
to your response.
But by default, varnish will not know anything about it. To get it to work
you have to extend your varnish vcl_fetch
configuration:
Varnish will then look for the X-Reverse-Proxy-TTL
header and if it exists,
varnish will use the found value as TTL and then remove the header.
There is a beresp.ttl field in VCL but unfortunately it can only be set to
absolute values and not dynamically. Thus we have to use a C code fragment.
Note that if you are using this, you should have a good purging strategy.
Varnish helper
This helper can be used to talk back to varnish to invalidate cached URLs. Configure the location of the varnish reverse proxies (be sure not to forget any, as each varnish must be notified separately):
- host: This must match the web host clients are using when connecting to varnish. You will not notice if this is mistyped, but cache invalidation will never happen. You can also add a regexp here like "." to clear all host entries. The regexp will be surrounded by "^(" and ")$" ending in "^(.)$" in this example.
- ips: List of IP adresses of your varnish servers. Comma separated.
- port: The port varnish is listening on for incoming web connections.
- headers: (optional) If you want to send special headers with each request sent to varnish, you can add them here (as array)
To use the varnish cache helper you must inject the
liip_cache_control.varnish
service or fetch it from the service container:
When using ESI, you will want to purge individual fragments. To generate the
corresponding _internal
route, inject the http_kernel
into your controller and
use HttpKernel::generateInternalUri with the parameters as in the twig
render
tag.
Purging
Add the following code to your Varnish configuration to have it handle PURGE requests (make sure to uncomment the appropiate line(s))
varnish 3.x
In Varnish 2, the purge
action is actually just marking caches as invalid.
This is called ban
in Varnish 3.
Varnish 2.x
NOTE: this code invalidates the url for all domains. If your varnish serves multiple domains, you should improve this configuration.
The varnish path invalidation is about equivalent to doing this:
netcat localhost 6081 << EOF
PURGE /url/to/purge HTTP/1.1
Host: webapp-host.name
EOF
Banning
Since varnish 3 banning can be used to invalidate the cache. Banning invalidates whole section with regular expressions, so you will need to be careful to not invalidate too much.
Configure the varnish reverse proxies to use ban as purge instruction:
This will do a purge request and will add X-Purge headers which can be used by your Varnish configuration:
varnish 3.x
Force refresh
Alternatively one can also force a refresh using the approach
The vanish path force refresh is about equivalent to doing this:
netcat localhost 6081 << EOF
GET /url/to/refresh HTTP/1.1
Host: webapp-host.name
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
EOF
To use the varnish cache helper you must inject the
liip_cache_control.varnish
service or fetch it from the service container:
Banning from the console
You can also ban URLs from the console
will ban (invalidate) all entries in your configured varnish servers (matching varnish.host)
will ban (invalidate) all entries in your configured varnish servers, where the URL starts with "/posts". Any regular expression understood by varnish can be used here.
It uses the Varnish Helper class, therefore if you defined more than one varnish server in the config file (in varnish.ips), the entries will be deleted in all servers.
Cache authorization listener
Enable the authorization listener:
This listener makes it possible to stop a request with a 200 "OK" for HEAD requests right after the security firewall has finished. This is useful when one uses Varnish while handling content that is not available for all users.
In this scenario on a cache hit, Varnish can be configured to issue a HEAD request when this content is accessed. This way Symfony2 can be used to validate the authorization, but no work needs to be made to regenerate the content that is already in the Varnish cache.
Note this obviously means that it only works with path based Security. Any additional security implemented inside the Controller will be ignored.
Note further that a HEAD response is supposed to contain the same HTTP header meta data as the GET response to the same URL. However for the purpose of this use case we have no other choice but to assume a 200.
Flash message listener
The Response flash message listener moves all flash messages currently set into a cookie. This way it becomes possible to better handle flash messages in combination with ESI. The ESI configuration will need to ignore the configured cookie. It will then be up to the client to read out the cookie, display the flash message and remove the flash message via javascript.
If you do not want the flash message listener, you can disable it: