Search code examples
cachingnginxspring-bootjava-8ssi

nginx how to cache some block in the middle of the dynamic web page


I have a main page with dynamic content that differs for authorized/unauthorized users. And I need to put a block with "10 Top entities" in the middle of that page.

This block is generated by freemarker and data is fetched some SQL queries that are heavy for my DB, but the results' time-to-live is about 1 hour or even more. So I think that I can generate that block and cache it at nginx level.

I see two ways:

  • Use nginx ssi module which may help to include that block and cache

  • Set onload javascript function which will fetch that block by one more request to HTTP-server

My question: what is the correct way to handle this situation? May be there is another way for such case?


Solution

  • This solely depends on your setup and specific case.

    • SSI is viable if already used in project, if not - introducing additional components into project is not always a good solution, especially when application logic spills onto these.

    • Second request is easiest to implement, and in this case you can also leverage browser caching, which may be of some help

    • Third option is to have it cached at application level (in shared memcached/redis/etc) or even have a materialised db view so that heavy queries become not so heavy.