Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Disabling dispatcher cache for some pages with HTTP header

MaynorSong
Level 2
Level 2

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

Dispatcher
1 Accepted Solution
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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!

View solution in original post

4 Replies
SureshDhulipudi
Community Advisor
Community Advisor

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

 

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

Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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!

View solution in original post

Umesh_Thakur
Community Advisor
Community Advisor

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

raj_mandalapu
Community Advisor
Community Advisor

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...