Configuring a Cache Writer
There are many configuration options for a cache writer. For a full list of configuration properties, see the
Javadoc for the CacheWriterConfiguration class.
Below is an example of how to configure the cache writer in XML:
<cache name="writeThroughCache1" ... >
<cacheWriter writeMode="write-behind" maxWriteDelay="8" rateLimitPerSecond="5"
writeCoalescing="true" writeBatching="true" writeBatchSize="20"
retryAttempts="2" retryAttemptDelaySeconds="2">
<cacheWriterFactory class="com.company.MyCacheWriterFactory"
properties="just.some.property=test; another.property=test2"
propertySeparator=";"/>
</cacheWriter>
</cache>
Further examples:
<cache name="writeThroughCache2" ... >
<cacheWriter/>
</cache>
<cache name="writeThroughCache3" ... >
<cacheWriter writeMode="write-through" notifyListenersOnException="true"
maxWriteDelay="30" rateLimitPerSecond="10" writeCoalescing="true"
writeBatching="true" writeBatchSize="8" retryAttempts="20"
retryAttemptDelaySeconds="60"/>
</cache>
<cache name="writeThroughCache4" ... >
<cacheWriter writeMode="write-through" notifyListenersOnException="false"
maxWriteDelay="0" rateLimitPerSecond="0" writeCoalescing="false"
writeBatching="false" writeBatchSize="1" retryAttempts="0"
retryAttemptDelaySeconds="0">
<cacheWriterFactory class="net.sf.ehcache.writer.WriteThroughTestCacheWriterFactory"/>
</cacheWriter>
</cache>
<cache name="writeBehindCache5" ... >
<cacheWriter writeMode="write-behind" notifyListenersOnException="true"
maxWriteDelay="8" rateLimitPerSecond="5" writeCoalescing="true"
writeBatching="false" writeBatchSize="20"
retryAttempts="2" retryAttemptDelaySeconds="2">
<cacheWriterFactory class="net.sf.ehcache.writer.WriteThroughTestCacheWriterFactory"
properties="just.some.property=test; another.property=test2"
propertySeparator=";"/>
</cacheWriter>
</cache>
As shown below, this configuration can also be achieved through the Cache constructor in Java:
Cache cache = new Cache(
new CacheConfiguration("cacheName", 10)
.cacheWriter(new CacheWriterConfiguration()
.writeMode(CacheWriterConfiguration.WriteMode.WRITE-BEHIND)
.maxWriteDelay(8)
.rateLimitPerSecond(5)
.writeCoalescing(true)
.writeBatching(true)
.writeBatchSize(20)
.retryAttempts(2)
.retryAttemptDelaySeconds(2)
.cacheWriterFactory(new CacheWriterConfiguration.CacheWriterFactoryConfiguration()
.className("com.company.MyCacheWriterFactory")
.properties("just.some.property=test; another.property=test2")
.propertySeparator(";"))));
Instead of relying on a CacheWriterFactoryConfiguration to create a CacheWriter, it is also possible to explicitly register a CacheWriter instance from within Java code. This allows you to refer to local resources like database connections or file handles.
Cache cache = manager.getCache("cacheName");
MyCacheWriter writer = new MyCacheWriter(jdbcConnection);
cache.registerCacheWriter(writer);