Disabling dispatcher cache for some pages with HTTP header

Avatar

Avatar

MaynorSong

Avatar

MaynorSong

MaynorSong

04-02-2021

This document tells that but on JSP. I wonder how to do it on a java model?

Dispatcher

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Manjunath_K

MVP

Avatar

Manjunath_K

MVP

Manjunath_K
MVP

04-02-2021

Hi @MaynorSong,

If you want to disable caching your pages in dispatcher, you can add rule in dispatcher config file as mentioned here.

https://experienceleague.adobe.com/docs/experience-manager-dispatcher/using/configuring/dispatcher-c...

 

If you want handle this through AEM backend then below are the 2 options.

1. To disable dispatcher caching the pages in which specific component is added, set response header in that specific component model class.

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ComponentModel {

@Inject
private SlingHttpServletResponse response;

@PostConstruct
protected void init() {
response.setHeader("Dispatcher", "no-cache");
}
}

 

2. If you have this use case for specific pages not by specific component basis, then create common cache control model class & include call to that model class in page footer level based on page condition check.

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CacheControlModel {

@Inject
private SlingHttpServletResponse response;

@PostConstruct
protected void init() {
response.setHeader("Dispatcher", "no-cache");
}
}

 

HTML

<sly data-sly-test="${condition}" data-sly-use="com.project.core.models.CacheControlModel"/>

 

 

Hope this helps!

Answers (3)

Answers (3)

Avatar

Avatar

raj_mandalapu

MVP

Avatar

raj_mandalapu

MVP

raj_mandalapu
MVP

04-02-2021

You can check the below approaches.

Enable it for the page:

If you want to disable cache for only a specific set of pages, then you can follow the below approach. in the page, properties put a checkbox, and on the selection of checkbox just load the below JSP file.

<sly data-sly-include="no-cache.jsp" />

Inside the .jsp file call no-cache code.

Enable it for components:

I recommend you to use sling dynamic include for the dynamic components, it gives a lot of advantages when we compare with other approaches. you don't need to set response headers in the sling model, all you need to do is just configuration, check below article for more information.

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/set-up-sling...

 

 

 

Avatar

Avatar

Umesh_Thakur

MVP

Avatar

Umesh_Thakur

MVP

Umesh_Thakur
MVP

04-02-2021

One top of @Manjunath_K, if you wanted it for page then you can also set a servlet filter based on page type and then from filter you can add or remove headers add needed.

Can you please chek below article from Preeti

https://medium.com/@preeti.bhaya/dispatcher-caching-based-on-resource-types-afc712e6f5ef

 

Hope this will help.

Umesh Thakur

Avatar

Avatar

SureshDhulipudi

MVP

Avatar

SureshDhulipudi

MVP

SureshDhulipudi
MVP

04-02-2021

you can use the same with in java method-  SlingHttpServletResponse response

 

response.setHeader("Dispatcher", "no-cache");