Expand my Community achievements bar.

SOLVED

Disabling dispatcher cache for some pages with HTTP header

Avatar

Level 2

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 8

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

Avatar

Community Advisor

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

 

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

Avatar

Correct answer by
Level 8

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!

Avatar

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

Avatar

Level 8

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