Created
December 11, 2015 06:21
-
-
Save justsayantan/ec5a881ac2a585ef8534 to your computer and use it in GitHub Desktop.
CacheController in Tridion DD4T 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Use this controller to manually invalidate the DD4T and its dependency cache. | |
* Please note: this controller for developers. | |
*/ | |
@Controller | |
public class CacheController { | |
private final Logger LOG = LoggerFactory.getLogger(getClass()); | |
private final ApplicationContext applicationContext; | |
private EHCacheProvider ehCacheProvider; | |
private PropertiesService propertiesService; | |
private LabelExtractService labelExtractService; | |
protected CacheProvider cacheProvider; | |
private final int[] mutex = new int[0]; | |
@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); | |
} catch (Exception ex) { | |
LOG.error( | |
String.format("Label service did not find FAQ publishing template for %s and pub id %s.", "faq_template", pubId), ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment