• New Feature
  • Status: Open
  • 2 Major
  • Resolution:
  • ehcache-core
  • prodmgmt
  • Reporter: jkronegg
  • August 26, 2011
  • 1
  • Watchers: 3
  • May 06, 2013

Attachments

Description

It would be very practical to have the notion of cache template (or model) in ehcache configuration XML file.

By now, we write something like this in ehcache.xml :

<ehcache>
  <cache name="myCache1" maxElementsInMemory="0" maxElementsOnDisk="0" eternal="true" copyOnWrite="false" overflowToDisk="false" diskPersistent="true">
  <cache name="myCache2" maxElementsInMemory="0" maxElementsOnDisk="0" eternal="true" copyOnWrite="false" overflowToDisk="false" diskPersistent="true">
  ...
  <cache name="myCacheN" maxElementsInMemory="0" maxElementsOnDisk="0" eternal="true" copyOnWrite="false" overflowToDisk="false" diskPersistent="true">
</ehcache>

The idea is to have something like:

<ehcache>
  <cache name="myCache1" maxElementsInMemory="0" maxElementsOnDisk="0" eternal="true" copyOnWrite="false" overflowToDisk="false" diskPersistent="true">
  <cache name="myCache2" template="myCache1">
  ...
  <cache name="myCacheN" template="myCache1">
</ehcache>

This would make the ehcache.xml much more readable and will make configuration much easier. You can imagine having a few cache template (e.g. one for ehcache standalone, one for ehcache with jgroups replication, one for ehcache with Terracotta) and a lot of cache stores relying on these templates.

The benefits:

  • easier configuration file maintenance (changing a template will change all cache store configuration that use it)
  • faster configuration understanding by new developpers (less configuration to read means faster understanding)
  • lower memory usage (if all cache use the same CacheConfiguration and not a clone; I don’t know it this is possible, especially regarding listeners, peers, threads and so on)

This would only require to modify *net.sf.ehcache.config.CacheConfiguration* as such:

  • add a “copyParameters(CacheConfiguration)” method (based on the “clone()” method)
  • add a “template” field
  • add a “template” setter which do something like:
public CacheConfiguration template(String templateName) {
  CacheConfiguration templateConfig = CacheManager.getConfiguration().getCacheConfigurations().get(templateName);
  if (templateConfig==null) {
    throw new CacheException("Cache with name '"+templateName+"' does not exist and cannot be used as template");
  }
  copyParameters(templateConfig);
  return this;
}

Sharing the template cache CacheConfiguration for all caches that use it may be complex, so it would probably be a nice-to-have (e.g. boolean field “sharedConfiguration”).

Note that the feature request CDV-1328 has a similar title but requests a different feature: having pre-configured templates.

Comments

Julien Kronegg 2011-09-30

I implemented the template cache configuration and provided the patch.

The {{net.sf.ehcache.config.CacheConfiguration}} class was changed as such:

  • added a {{template}} String field with its getter/setter
  • added a {{copyParametersTo()}} method which copies the parameters from one {{CacheConfiguration}} to the another
  • refactored the {{clone()}} method in order to remove common code with the {{copyParametersTo()}} method

The {{net.sf.ehcache.config.Configuration}} class was changed as such:

  • modified {{addCache()}} method to copy the template {{CacheConfiguration}} parameters

The {{ehcache.xsd}} was changed as such:

  • added {{template}} field in {{\}\} tag

Shared template (i.e. having one single {{CacheConfiguration}} instead of N for N cache that are using template) was not implemented. This would require to :

  • split the {{CacheConfiguration}} class into an interface {{CacheConfigurationI}} and an implementation {{CacheConfigurationImpl}}
  • add a {{SharedCacheConfiguration}} class which implements {{CacheConfigurationI}} and has a {{CacheConfigurationImpl}} delegate
  • modify all classes that refer to the {{CacheConfiguration}} class to reference the {{CacheConfigurationI}} interface instead (includes changing references to fields by calls to getters)
  • modify the {{net.sf.ehcache.config.BeanHandler}} class in order to create a new instance of {{CacheConfigurationImpl}} (i.e. find the implementation from the interface) This was a lot of API changes for a relatively few advantages (dynamic configuration single modification point, lower memory usage), so I was IMHO not worth the effort/risk/change costs.

The zip file (ehcache-EHC878-patch-1.zip) contains the patch (diff file) and the full files.

Julien Kronegg 2011-10-07

Updated patch (XSD file now validate the ehcache.xml properly), as ehcache-EHC878-patch-2.zip