Code snippets for symfony 1.x

Navigation

Controlling HTTP Caching (Squid, Varnish, ..) with cache.yml files

In this snippet I will demonstrate how to extend the sfCache interface in order to configure your template cache to use a HTTP cache proxy like Squid or Varnish.

The goal

Setting configurable headers for HTTP caches using the symfony caching system.

The implementation

somehow < and > get escaped in the following code...

  1 &lt;?php
  2
  3 /**
  4  * Cache class that sets HTTP cache headers.
  5  *
  6  * @package    symfony-snippets
  7  * @subpackage cache
  8  * @author     Christian Schaefer &lt;caefer@ical.ly&gt;
  9  * @version    SVN: $Id: $
 10  */
 11 class csHttpCacheHeaderCache extends sfCache
 12 {
 13   /**
 14    * @var headers HTTP headers to be set
 15    */
 16   private $headers = array();
 17
 18  /**
 19   * Initializes this sfCache instance.
 20   *
 21   * Available options:
 22   *
 23   * * headers:  HTTP headers to be set (array)
 24   *
 25   * * see sfCache for options available for all drivers
 26   *
 27   * @see sfCache
 28   */
 29   public function initialize($options = array())
 30   {
 31     parent::initialize($options);
 32
 33     if (!$this-&gt;getOption(&#039;headers&#039;))
 34     {
 35       throw new sfInitializationException(&#039;You must pass a &quot;headers&quot; option to initialize a gujSquidCache object.&#039;);
 36     }
 37
 38     $this-&gt;headers = $this-&gt;getOption(&#039;headers&#039;);
 39   }
 40
 41   /**
 42    * @see sfCache
 43    */
 44   public function get($key, $default = null)
 45   {
 46     return $default;
 47   }
 48
 49   /**
 50    * @see sfCache
 51    */
 52   public function has($key)
 53   {
 54     return false;
 55   }
 56
 57   /**
 58    * @see sfCache
 59    */
 60   public function set($key, $data, $lifetime = null)
 61   {
 62     if(false === strpos($key, &#039;/_partial/&#039;))  // don&#039;t set cache headers for partials
 63     {
 64       if(is_object(unserialize($data)))       // don&#039;t set cache headers for pages without layout
 65       {
 66         $response = sfContext::getInstance()-&gt;getResponse();
 67
 68         foreach($this-&gt;headers as $key =&gt; $value)
 69         {
 70           $value = str_replace(&#039;%EXPIRE_TIME%&#039;, gmdate(&quot;D, d M Y H:i:s&quot;, time() + $lifetime), $value);
 71           $value = str_replace(&#039;%LAST_MODIFIED%&#039;, gmdate(&quot;D, d M Y H:i:s&quot;, time()), $value);
 72           $value = str_replace(&#039;%LIFETIME%&#039;, $lifetime, $value);
 73           $response-&gt;setHttpHeader($key, $value, true);
 74         }
 75       }
 76     }
 77     return true;
 78   }
 79
 80   /**
 81    * @see sfCache
 82    */
 83   public function remove($key)
 84   {
 85     return true;
 86   }
 87
 88   /**
 89    * @see sfCache
 90    */
 91   public function removePattern($pattern)
 92   {
 93     return true;
 94   }
 95
 96   /**
 97    * @see sfCache
 98    */
 99   public function clean($mode = sfCache::ALL)
100   {
101     return true;
102   }
103
104   /**
105    * @see sfCache
106    */
107   public function getLastModified($key)
108   {
109     return 0;
110   }
111
112   /**
113    * @see sfCache
114    */
115   public function getTimeout($key)
116   {
117     return 0;
118   }
119 }
 

The cache class csHttpCacheHeaderCache is basically a copy of sfNoCache exept for the method set() which sets the configured headers for action templates with layout.

The configuration

In your /apps/&lt;appname&gt;/config/factories.yml configure csHttpCacheHeaderCache as your view cache class and customize any number of headers:

1 all:
2   view_cache:
3     class:    csHttpCacheHeaderCache
4     param:
5       headers:
6         Expires:        &quot;%EXPIRE_TIME%&quot;
7         Last-Modified:  &quot;%LAST_MODIFIED%&quot;
8         Cache-Control:  &quot;public&quot;
 

the placeholders %EXPIRE_TIME%, %LAST_MODIFIED% and %LIFETIME% will be dynamically replaced.

Now you can follow the symfony caching chapter in the documentation to enable caching per site or globally.

Have fun!

/Christian

by Christian Schaefer on 2009-10-16, tagged cache