sfHttpHeaderCachePlugin
0.1.0beta
for sf 1.4sf 1.3sf 1.2 MIT
sfHttpHeaderCachePlugin
Provides a means to facilitate caching by reverse proxies like nginx or Varnish, using cache.yml for configuration, as well as using an alternative cache (like sfApcCache or sfMemcacheCache) for partials
Credits
This plugin started life as a post on Symfony Snippets by Christian Schaefer.
Developers
License
Copyright (c) 2010 Ben Lancaster <benlanc@ster.me.uk>, Christian Schaefer <christian.schaefer@gmail.com>
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.
sfHttpHeaderCachePlugin
Goal
This plugin aims to sit on top of Symfony's built-in caching configuration to provide a means to configure reverse proxy (e.g. nginx or Varnish) caching through the use of sending good caching headers with the response. It also provides an alternative cache for template fragments (partials and components) using existing caching mechanisms native to Symfony or compatible with Symfony, like sfApcCache, for example.
A word of warning
With a caching reverse proxy in place (or even a proxy at an ISP level), this plugin will cache the whole response when with_layout is set to true in your factories.yml. This may result in unexpected results if you rely on the user session, so use it with caution, as you could end up spewing other people's data out for all other users infront of your caching reverse proxy (or indeed, behind a ISP's proxy).
Configuration
Firstly, install the plugin in the usual way (either on with the cli or using svn:externals).
Next, set your factories.yml to use the sfHttpHeaderCache class provided by the plugin:
all:
view_cache:
class: sfHttpHeaderCache
param:
headers:
Expires: "%EXPIRE_TIME%"
Last-Modified: "%LAST_MODIFIED%"
Cache-Control: "public, max_age=%LIFETIME%"
ETag: '%ETAG%'
Then, make sure settings.yml is set to enable caching:
prod:
cache: true
For all actions that you'd like to cache, you should then configure your project's, app's or modules's cache.yml to do so:
default:
enabled: true
with_layout: true # Headers are only sent for caches with layout
lifetime: 3600
The nature of this plugin is that it only caches whole-page responses with the layout and all. If you'd like to use the partial cache as well, you can embellish the factories.yml with an alternative cache like so:
all:
view_cache:
class: sfHttpHeaderCache
param:
headers:
Expires: "%EXPIRE_TIME%"
Last-Modified: "%LAST_MODIFIED%"
Cache-Control: "public, max_age=%LIFETIME%"
ETag: '%ETAG%'
alt:
cache:
# This could be any of the Symfony API caches, defaults to sfNoCache
class: sfMemcacheCache
# Any valid options for your selected alt-cache
param:
servers: # Array of servers
localhost:
host: 127.0.0.1
Credits
This plugin started life as a post on Symfony Snippets by Christian Schaefer.