Symfony2: HTTP Cache [part3] – ETags


ETags allows you to set a cached content invalid when it has non-matching ETags (the etags form the cached content and the new etag generated by the server)

public function indexAction()
{
    $response = new Response();
    $response->setETag(md5( ...whatever you want...));
     if( $response->isNotModified($this->get('request')) )
     {
         return $response; //THis will automaticall return aNULL content with a 304 header (not modified)
     }
     else
     {
         //Generate the contents and responde. THis response will have the ETage generated above.
         return  $this->renderView('MyBundle:Main:index.html.twig', array(), $response);
     }
}

So the Etags in combination with the Max Age header can allow you to cache data but let you make last minute changed and still let the server responde the correct response.

I would say that a cool Etag value could be MD5( the name of the resource+the revision number (if you use svn) )

read more

Advertisements