Search code examples
phpsymfonycachinghttp-caching

Private cache in symfony 2 does not generate


I am trying to use HTTP cache for my application but every time i set the cache to private, it does not apear in the app/cache folder.

For public cache everything is ok, the files/folders are being generated.

Is it something that i'm missing? I am trying to generate cache for a section of my website that requires authentication. So the cache should be specific for every username because it countains reports only for that user.

This is what i am doing:

    $response = $this->render('myTemplate.html.twig', array(
        'username' => $username
    ));

    $response->setMaxAge(600);
    $response->setPrivate();

    return $response;

Solution

  • Symfony's http cache acts as a public (shared) reverse proxy server. That means it only caches public responses.

    Read Public vs Private Responses in Symfony docs:

    Both gateway and proxy caches are considered "shared" caches as the cached content is shared by more than one user. If a user-specific response were ever mistakenly stored by a shared cache, it might be returned later to any number of different users. Imagine if your account information were cached and then returned to every subsequent user who asked for their account page!

    To handle this situation, every response may be set to be public or private:

    • public: Indicates that the response may be cached by both private and shared caches;
    • 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.

    Symfony conservatively defaults each response to be private. To take advantage of shared caches (like the Symfony reverse proxy), the response will need to be explicitly set as public.