In migrating our application’s caching service to use ColdFusion’s Ehcache integration I needed to a way to get all of the caching regions stored in Ehcache. This was easily accomplished using the Ehcache API:
- Get the CacheManager instance
- Get the cache names
- Sort the cache names
Get CacheManager Instance
First, we need to get the CacheManager singleton instance.
var cacheManager = createObject('java', 'net.sf.ehcache.CacheManager').getInstance();
Get Cache Names
Next, we invoke the public String[] getCacheNames()
method.
var regions = cacheManager.getCacheNames();
Sort Cache Names
Last, I wanted to sort the region names to be in alphabetical order. To do this I used the Arrays.sort() utility method.
var Arrays = CreateObject("java", "java.util.Arrays");
Arrays.sort(regions);
Putting it all together
That’s it! Using the array I was able to easily create a select list to enable our development team to have a fine control over clearing the cache regions in Ehcache.
var cacheRegionsSelectList = getFormFieldFactory().getField("MultiSelectList").init(name="regions", label="Remove Cache Region");
var cacheManager = createObject('java', 'net.sf.ehcache.CacheManager').getInstance();
var regions = cacheManager.getCacheNames();
var Arrays = CreateObject("java", "java.util.Arrays");
Arrays.sort(regions);
for (var region in regions) {
cacheRegionsSelectList.addOption(value=region, label=region);
}
addField(cacheRegionsSelectList);
Note, that I am using a custom framework that has a Form
helper class to easily create forms.