Search code examples
cachingsymfonyprivate

Use caching in private context with Symfony 2


I'm trying to cache a profile user page. For this, I use :

$response = new Response();
$response->setETag(md5($response->getContent()));

if ($response->isNotModified($this->getRequest())) {
    return $response;
}

But, the application never use the cache because $this->getRequest()->getEtags() (which use in isNotModified function) is always empty. If I set

$response->setPublic()

All is ok. So, how can I use caching in Private context ?

Thx !


Solution

  • When using private response in Symfony 2, it sets the following header:

    Cache-Control: private
    

    According to the HTTP 1/1 specification:

    private

    Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. A private (non-shared) cache MAY cache the response.

    That means that you CAN'T use ETag header with Cache-Control: private.