ehcache

net.sf.ehcache.writer
Class AbstractCacheWriter

java.lang.Object
  extended by net.sf.ehcache.writer.AbstractCacheWriter
All Implemented Interfaces:
CacheWriter

public abstract class AbstractCacheWriter
extends Object
implements CacheWriter

A convenience abstract base class that implements all CacheWriter methods.

The write(net.sf.ehcache.Element), writeAll(java.util.Collection), delete(net.sf.ehcache.CacheEntry) and deleteAll(java.util.Collection) methods throw UnsupportedOperationException unless they're overridden by the class that is extending AbstractCacheWriter. Classes that are extending this abstract base class should make sure that the appropriate cache writer operation methods are implemented with their application functionalities.

Version:
$Id: AbstractCacheWriter.java 5594 2012-05-07 16:04:31Z cdennis $
Author:
Geert Bevin

Constructor Summary
AbstractCacheWriter()
           
 
Method Summary
 CacheWriter clone(Ehcache cache)
          Creates a clone of this writer.
 void delete(CacheEntry entry)
          Delete the cache entry from the store
 void deleteAll(Collection<CacheEntry> entries)
          Remove data and keys from the underlying store for the given collection of keys, if present.
 void dispose()
          Providers may be doing all sorts of exotic things and need to be able to clean up on dispose.
 void init()
          Notifies writer to initialise themselves.
 void throwAway(Element element, SingleOperationType operationType, RuntimeException e)
          This method will be called, whenever an Element couldn't be handled by the writer and all the retryAttempts have been tried.
 void write(Element element)
          Write the specified value under the specified key to the underlying store.
 void writeAll(Collection<Element> elements)
          Write the specified Elements to the underlying store.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractCacheWriter

public AbstractCacheWriter()
Method Detail

write

public void write(Element element)
           throws CacheException
Write the specified value under the specified key to the underlying store. This method is intended to support both key/value creation and value update for a specific key.

Specified by:
write in interface CacheWriter
Parameters:
element - the element to be written
Throws:
CacheException

writeAll

public void writeAll(Collection<Element> elements)
              throws CacheException
Write the specified Elements to the underlying store. This method is intended to support both insert and update. If this operation fails (by throwing an exception) after a partial success, the convention is that entries which have been written successfully are to be removed from the specified mapEntries, indicating that the write operation for the entries left in the map has failed or has not been attempted.

Specified by:
writeAll in interface CacheWriter
Parameters:
elements - the Elements to be written
Throws:
CacheException

delete

public void delete(CacheEntry entry)
            throws CacheException
Delete the cache entry from the store

Specified by:
delete in interface CacheWriter
Parameters:
entry - the cache entry that is used for the delete operation
Throws:
CacheException

deleteAll

public void deleteAll(Collection<CacheEntry> entries)
               throws CacheException
Remove data and keys from the underlying store for the given collection of keys, if present. If this operation fails (by throwing an exception) after a partial success, the convention is that keys which have been erased successfully are to be removed from the specified keys, indicating that the erase operation for the keys left in the collection has failed or has not been attempted.

Specified by:
deleteAll in interface CacheWriter
Parameters:
entries - the entries that have been removed from the cache
Throws:
CacheException

throwAway

public void throwAway(Element element,
                      SingleOperationType operationType,
                      RuntimeException e)
This method will be called, whenever an Element couldn't be handled by the writer and all the retryAttempts have been tried.

When batching is enabled all the elements in the failing batch will be passed to this methods

Try to not throw RuntimeExceptions from this method. Should an Exception occur, it will be logged, but the element will be lost anyways.

Specified by:
throwAway in interface CacheWriter
Parameters:
element - the Element that triggered the failure, or one of the elements part of the batch that failed
operationType - the operation we tried to execute
e - the RuntimeException thrown by the Writer when the last retry attempt was being executed

clone

public CacheWriter clone(Ehcache cache)
                  throws CloneNotSupportedException
Creates a clone of this writer. This method will only be called by ehcache before a cache is initialized.

Implementations should throw CloneNotSupportedException if they do not support clone but that will stop them from being used with defaultCache.

Specified by:
clone in interface CacheWriter
Returns:
a clone
Throws:
CloneNotSupportedException - if the extension could not be cloned.

init

public void init()
Notifies writer to initialise themselves.

This method is called during the Cache's initialise method after it has changed it's status to alive. Cache operations are legal in this method. If you register a cache writer manually after a cache has been initialised already, this method will be called on the cache writer as soon as it has been registered.

Note that if you reuse cache writer instances or create a factory that returns the same cache writer instance as a singleton, your init method should be able to handle that situation. Unless you perform this multiple usage of a cache writer yourself, Ehcache will not do this though. So in the majority of the use cases, you don't need to do anything special.

Specified by:
init in interface CacheWriter

dispose

public void dispose()
             throws CacheException
Providers may be doing all sorts of exotic things and need to be able to clean up on dispose.

Cache operations are illegal when this method is called. The cache itself is partly disposed when this method is called.

Specified by:
dispose in interface CacheWriter
Throws:
CacheException

ehcache

Copyright 2001-2017, Terracotta, Inc.