Question

I was trying to use private/public caching in order to avoid retrieving all the data that is used to build the homepage content. which is quiet static content.

I try to do it like that.

    $response->setMaxAge(600);
    // mark the response as either public or private
    $response->setPublic();
    $response->setPrivate();

    // set the private or shared max age
    $response->setMaxAge(2000);
    $response->setSharedMaxAge(2000);
    $response->setETag(md5($response->getContent()));
    $response->isNotModified($this->getRequest());
    // set a custom Cache-Control directive
    $response->headers->addCacheControlDirective('must-revalidate', true);

BUT, I have some small information that can change really dynamically like language&currency selection.

How to make the content change if the user decide to change the language or currency ?

It's currently working for language as the URL is then different home.site/fr or home.site/en.

But the information of currency is not in the url, and other information like authentified or not is not in the url either.

So how to use Http cache and keep things fit the really of the user actions ?

Thanks for your help.

Yoni

Was it helpful?

Solution

When using HTTP cache you've to think your application as a whole.

Depending on your reverse-proxy, the default strategy is to cache a page based on its URI. The ETag is used to make conditional requests to avoid fetching actual data from the backend but it doesn't change the behavior of the caching strategy.

Therefore, using your current setup, every time an user will access, let's say example.net/fr, within the defined 2000 seconds, it will get the first cached content which has been retrieved from your backend, no matter the currency he chosen.

If you want to optimize your performance, you need to reduce to remove the less common information.

In your case, you'll probably want to cache at least 2 representation of your content:

  1. FR + EUR
  2. EN + EUR

And so.

Why? Because you don't want to cache all possible representation or your cache would become useless.

To make your caching strategy efficient, you need to be able to cache data for a maximum of people, so if you have some block like "Welcome {username}" it will fail, because you're likely to have to cache a representation of your content for every users and that's wrong.

The best way to keep some sort of "Welcome {username}" block is to use ESI, it allows you to split your layout in several blocks with each a different caching strategy (or no caching at all).

The problem in your case is I bet the currency is stored in some sort of cookie. By default most reverse proxy (like varnish) doesn't cache response with cookies because it could be a major security hole.

Therefore you have basically two solutions, define your custom strategy, it is really easy when using Varnish or use HTTP capabilities (Custom header + Vary).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top