© 2019 by Sayantan Basu

  • Sayantan Basu

Invalidate DD4T cache using JAVA controller in Tridion Project

“New changes of the content are not reflecting on the site”  – This is a common problem if you are using the DD4T to implement your site. We used to get this type of problem frequently in our project previously.

There may be several causes behind this issue but mostly the reason is strong caching property of DD4T. I know caching is very much important for the web site but sometimes it causes lots of issues.

Say you are in a middle of demo and want to show your customer that how easily you can handle your content from the CMS or your content editor team has updated some content on Production environment in these scenarios your changes should reflect immediately after the action performed.

To handle this situation in our project we have created a controller class in Java which is used to manually invalidate the DD4T and its dependency cache.

Here is the code –

@Autowired public CacheController(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @RequestMapping(value = “/dd4t.cache.invalidate.form”, method = GET) public String invalidate() { synchronized (mutex) { if (ehCacheProvider == null) { ehCacheProvider = applicationContext.getBean(EHCacheProvider.class); } ehCacheProvider.flush(); applicationContext.getBean(LabelExtractService.class).clearCache(); applicationContext.getBean(KeywordLabelService.class).clearCache(); applicationContext.getBean(FAQLabelService.class).clearCache(); } return “cache/invalidate”; } private void instantiatePropertiesService() { this.propertiesService = this.propertiesService == null ? applicationContext.getBean(PropertiesService.class) : this.propertiesService; } @RequestMapping(value = “/showLabels.form”, method = GET) public void showLabels(@RequestParam(required = true) int pubId, HttpServletResponse response) { try { if (StringUtils.isEmpty(String.valueOf(pubId))) { pubId = <Defqault Publication ID>;// Default english } PrintWriter out = response.getWriter(); labelExtractService = labelExtractService == null ? applicationContext.getBean(LabelExtractService.class) : labelExtractService; Map<String, String> springMap = labelExtractService.getLabelMap(pubId); out.println(); out.println(); out.println(); out.println(“##################### Label List From Spring ########################”); for (Map.Entry<String, String> entry : springMap.entrySet()) { out.println(entry.getKey() + ”  = ” + entry.getValue()); } out.println(); out.println(); out.println(); out.println(“##################### Label List from DD4T  ########################”); Map<String, String> dd4tMap = LabelServiceFactoryImpl.getInstance().getLabelService().load(pubId); for (Map.Entry<String, String> entry : dd4tMap.entrySet()) { out.println(entry.getKey() + ”  = ” + entry.getValue()); } cacheProvider = cacheProvider == null ? applicationContext.getBean(CacheProvider.class) : cacheProvider; cacheProvider.loadFromLocalCache(“LabelMap-“+pubId).setExpired(true);

Just add this controller in your project and run it from your browser.

The facility of using this code is you don’t have to edit the dd4t.properties file every time. So without disabling the caching functionality of your project, you can easily invalidate all the cache of you application in a single click. That’s all.

Check this link for full source code.

#Cache #DD4T #Tridion2013SP1

3 views