A CacheWriter is an interface used for write-through and write-behind caching to a underlying resource.
If configured for a cache, CacheWriter's methods will be called on a cache operation. A cache put will cause a CacheWriter write and a cache remove will cause a writer delete.
Implementers should create an implementation which handles storing and deleting to an underlying resource.
Write-Through
In write-through mode, the cache operation will occur and the writer operation will occur before CacheEventListeners are notified. If the write operation fails an exception will be thrown. This can result in a cache which is inconsistent with the underlying resource. To avoid this, the cache and the underlying resource should be configured to participate in a transaction. In the event of a failure a rollback can return all components to a consistent state.
Write-Behind
In write-behind mode, writes are written to a write-behind queue. They are written by a separate execution thread in a configurable way. When used with Terracotta Server Array, the queue is highly available. In addition any node in the cluster may perform the write-behind operations.
It's important to note that the operations that are handled by the {@code CacheWriter} don't have any guaranteed ordering in write-behind mode.The processing ordering can be different than the scheduling ordering, so your application needs to be written with this in mind. More information in the CacheWriter chapter of the documentation.
Creation and Configuration
CacheWriters can be created using the CacheWriterFactory or explicitly by instantiating them through Java code, giving you access to local resources.
The manner upon which a CacheWriter is actually called is determined by the {@link net.sf.ehcache.config.CacheWriterConfiguration} that is set up for cachethat is using the CacheWriter.
See the CacheWriter chapter in the documentation for more information on how to use writers.
@author Greg Luck
@author Geert Bevin
@version $Id: CacheWriter.java 2521 2010-06-23 10:53:48Z gbevin $