isicsHttpCachePlugin - 0.9.3

Backports Symfony2 http cache concepts.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

isicsHttpCachePlugin

Excited by Symfony2 Http Cache but still have heavy load projects to maintain under Symfony 1.4?
Inspired by Symfony2 (concepts and code), this plugin is for you!

isicsHttpCachePlugin fits to our (basic) actual requirements and doesn't already provides all HTTP cache mechanisms. But feel free to contribute!

It comes with :

  • 1 new method for sfWebRequest:
    • getETags()
  • 4 new methods for sfWebResponse:
    • isNotModified(sfWebRequest $request)
    • setETag($etag)
    • setLastModified($date)
    • setMaxAge($duration)
  • ESI support via 2 new helpers:
    • include_component_esi($module_name, $component_name, $vars)
    • include_partial_esi($template_name, $vars)
  • An invalidation method: isicsHttpCacheService::invalidate($url_pattern, $method)

The new methods are unit tested.

Benchmark

We've made a first benchmark with an homepage between 2 versions with exactly the same cache strategy:

  • a classical version using Symfony cache (optimized, 0 SQL queries)

    6 cached components with different TTL and so no cache for action

  • a version using Varnish

    TTL for action and 7 ESI tags (1 userbar without caching and the 6 others with different TTL) Symfony is still called at each request but only for userbar.

The benchmark was made with Siege tool.
It shows a 10 factor between Varnish and Symfony cache (400 req/sec vs 40).

Installation

Install the plugin:

$ symfony plugin:install -stability=beta isicsHttpCachePlugin

Enable the plugin in your /config/ProjectConfiguration.class.php:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins(array(
      ...
      'isicsHttpCachePlugin',
    ));
  }
}

For ESI support, enable module and helpers in your application(s) settings.yml:

all:
  .settings:
    standard_helpers: [..., isicsHttpCache]
    enabled_modules:  [..., isicsHttpCacheESI]

ESI (Edge Side Includes) support

The new helpers include_component_esi, include_partial_esi are very similar to Symfony ones.

If ESI are enabled (true by default in prod env) and only if a gateway handling ESI is installed (we detect that thanks to a dedicated header), an ESI tag is rendered rather than the content.
Rendering the content is the role of a special action.
Else, the content is direclty rendered exactly like with the Symfony helpers.

As of Symfony2, cache expiration and validation mechanisms are supported.

Both helpers accept an optional cache argument.
The value has a mixed type:

  • For expiration, simply give a duration in seconds:

    <?php include_component_esi('news', 'showLatest', array('limit' => 5, 'cache' => 300)) ?>

    In this example, the latest news component will be refresh every 5 minutes.

  • For validation, the argument value must be a string.

    <?php include_component_esi('news', 'showLatest', array('limit' => 5, 'cache' => 'NewsService') ?>  

    It corresponds to a class name that must defines one or both static methods getLastModified() and getETag():

    <?php
     
    static public function getLastModified($vars)
    {
      return NewsPeer::getLastModified();
    }

    and/or :

    <?php
     
    static public function getETag($vars)
    {
      return NewsPeer::computeETag();
    }

    Note the $vars argument. It is the vars you passed to the helpers. Use it as you like ;)

Invalidation ==

The method isicsHttpCachePlugin::invalidate() gives you ability to invalidate one or more url. It uses HTTP BAN and PURGE requests. Prefer PURGE to clear one url cache and BAN to clear many url thanks to regexp.

Note that you have to setup correctly your reverse proxy to handle this requests. For Varnish, you can use those examples: https://www.varnish-cache.org/docs/3.0/tutorial/purging.html

Task

You can test this tool in CLI with a dedicated task:

  • To clear one url (PURGE method implied):

    $ symfony isics-http-cache:invalidate http://www.mydomain.tld/
    

    This will clear homepage cache.

  • Or:

    $ symfony isics-http-cache:invalidate --method=BAN http://www.mydomain.tld/.*
    

    This will clear all the pages under this host.

Configuration

By default, ESI are enabled in prod env. Although, you should have to add the IP of your gateway.

This can be done in your application(s) app.yml:

all:
  isics_http_cache_plugin:
    esi:
      enabled:     false
      allowed_ips: [127.0.0.1]

Varnish configuration

Exactly like Symfony2, you have to setup Varnish to add a particular header telling that ESI are supported:

sub vcl_recv {
    set req.http.Surrogate-Capability = "abc=ESI/1.0";
}

sub vcl_fetch {
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        set beresp.do_esi = true;    
        unset beresp.http.Surrogate-Control;        
    }
}

Recommandations

  • Do not give large vars to ESI helpers since the vars are serialized and passed to the internal action on the url.

Known issues

  • Slots are broken when using ESI.

Roadmap

  • Try to make ESI render as fast as possible.
  • Write functional tests (not so easy with plugins).