jfCachePlugin: two Filters that might help to make your pages cacheable (client-side)

Today I packaged two filters I am using to manage client side caching:

  • jfCacheHeadersFilter: sets the HTTP headers required to server pages from client side (or proxy) cache
  • jfCacheInvalidateStaticSourceUrlsFilter: append a cache key to the URLs of referenced Javascript and CSS files so that you can configure a very long cache expire time for those files

Installation Instructions

Check out the source code


$ svn co http://jfcacheplugin.googlecode.com/svn/branches/1.4.0 plugins/jfCachePlugin

Publish assets


$ ./symfony plugin:publish-assets

Enable filters in your filters.yml

jfCacheHeadersFilter

This filter automatically sets the correct cache headers so that web browsers and proxy servers can cache your pages.
The corresponding cache headers will only be set if
a) you call jfCacheHeadersFilter::enable() or
b) you have enabled the symfony file cache with the “with_layout” option for the pages you want to cache (see http://www.symfony-project.org/gentle-introduction/1_4/en/12-Caching)

In either case you can avoid the cache headers if you call jfCacheHeadersFilter::disable()

Put these settings in your filters.yml file:

jfCacheHeaders:
  class: jfCacheHeadersFilter
  param:
    cacheableEnvironments: [prod]
    cacheableHttpStatusCodes: [200]
    expireTime: 300

jfCacheHeadersFilter

This filter automatically appends a cache=TIMESTAMP_OF_LAST_CLEAR_CACHE parameter to referenced .css and .js files.
Therefore you can cache your css/js files for a very long time without worrying that your users might use old versions of your files.
Clearing the symfony cache will automatically “clear” all js/css files cached in Browsers or Proxys.

Put these settings in your filters.yml file:

jfCacheInvalidateStaticSourceUrls:
  class: jfCacheInvalidateStaticSourceUrlsFilter
  param:
    cacheKeyName: cache #name of the url parameter the gets appended to each css/js URL

optional: modify vhost/.htaccess settings to server js/css files from cache

Here is a sample vhost/.htaccess config to make sure that js and css files get cached properly (for 30 days in this example):

<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresByType application/x-javascript A2592000
  ExpiresByType application/javascript A2592000
  ExpiresByType text/javascript A2592000
  ExpiresByType text/css A2592000
</ifmodule>

<ifmodule mod_headers.c>
  <filesmatch "\.(css|js)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesmatch>
</ifmodule>

Tags: , , ,

Leave a Reply