net.dontdrinkandroot.cache.impl

## Class AbstractMapBackedCache<K,V,M extends MetaData>

- java.lang.Object
- - net.dontdrinkandroot.cache.impl.AbstractCache<K,V>
  - - net.dontdrinkandroot.cache.impl.AbstractMapBackedCache<K,V,M>

All Implemented Interfaces:
  Cache<K,V>

Direct Known Subclasses:
  AbstractMapBackedCustomTtlCache, FileCache

    public abstract class AbstractMapBackedCache<K,V,M extends MetaData>
    extends AbstractCache<K,V>
    implements Cache<K,V>

Author:
  Philip W. Sorst <philip@sorst.net>

### Field Summary

### Fields inherited from interface net.dontdrinkandroot.cache.Cache

`UNLIMITED_IDLE_TIME`

### Constructor Summary

Constructors
Constructor and Description
`AbstractMapBackedCache(String name, long defaultTimeToLive, ExpungeStrategy expungeStrategy)` Construct a new AbstractCache.
`AbstractMapBackedCache(String name, long defaultTimeToLive, long defaultMaxIdleTime, ExpungeStrategy expungeStrategy)` Construct a new AbstractCache.

### Method Summary

Methods
`void` `cleanUp()` Cleanup the cache.
`void` `delete(K key)` Manually remove an entry from the cache.
`void` `delete(K key, M metaData)`
`protected abstract void` `doDelete(K key, M metaData)` Performs deletion of the data belonging to the metadata.
`protected abstract <T extends V> T` `doGet(K key, M metaData)` Performs retrieval of the data belonging to the metadata.
`protected abstract <T extends V> T` `doPut(K key, T data)` Performs storage of the given data and adds new metadata to the map.
`protected void` `expunge(Collection<Map.Entry<K,M>> expungeEntriesMetaData)` Performs the actual expunging of entries with the given metadata.
`<T extends V> T` `get(K key)` Retrieve an entry from the cache if it is available.
`List<M>` `getEntriesMetaData()` Returns a copy of the List of all metadata entries.
`protected Map<K,M>` `getEntriesMetaDataMap()`
`ExpungeStrategy` `getExpungeStrategy()`
`MetaData` `getMetaData(K key)` Retrieve the `MetaData` of a cached entry if it is available.
`SimpleCacheStatistics` `getStatistics()` Get statistics of the cache like hitrate, or size.
`<T extends V> T` `getWithErrors(K key)` Retrieve an entry from the cache if it is available.
`<T extends V> T` `put(K key, T data)` Store an entry in the cache with the default time to live.
`<T extends V> T` `putWithErrors(K key, T data)` Store an entry in the cache with the default time to live.
`protected void` `setEntriesMetaDataMap(Map<K,M> map)`
`void` `setExpungeStrategy(ExpungeStrategy expungeStrategy)`

### Methods inherited from class net.dontdrinkandroot.cache.impl.AbstractCache

`getCleanUpLogger, getDefaultMaxIdleTime, getDefaultTtl, getLogger, getName, setDefaultMaxIdleTime, setDefaultTtl`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface net.dontdrinkandroot.cache.Cache

`getDefaultMaxIdleTime, getDefaultTtl, getName, setDefaultTtl`

### Constructor Detail

#### AbstractMapBackedCache

    public AbstractMapBackedCache(String name,
                          long defaultTimeToLive,
                          ExpungeStrategy expungeStrategy)

Construct a new AbstractCache.

Parameters:
  `name` - The name of the cache.
  `defaultTimeToLive` - The default time to live for Cache entries.
  `expungeStrategy` - The `ExpungeStrategy` to use.

#### AbstractMapBackedCache

    public AbstractMapBackedCache(String name,
                          long defaultTimeToLive,
                          long defaultMaxIdleTime,
                          ExpungeStrategy expungeStrategy)

Construct a new AbstractCache.

Parameters:
  `name` - The name of the cache.
  `defaultTimeToLive` - The default time to live for Cache entries.
  `defaultMaxIdleTime` - The default max idle time for cache entries.
  `expungeStrategy` - The `ExpungeStrategy` to use.

### Method Detail

#### put

    public <T extends V> T put(K key,
                      T data)

Description copied from interface: `Cache`
Store an entry in the cache with the default time to live. Any errors are swallowed, use `Cache.putWithErrors(Object, Object)` if you want to handle them.

Specified by:
  `put` in interface `Cache<K,V>`
Parameters:
  `key` - A unique identifier.
  `data` - The data to store. Make sure that you don't alter the data after it has been put to cache as (depending on the implementation) this might lead to altering the entry in the cache. Use the returned data instead.
Returns:
  The entry that has been stored in the cache. It is save to alter this as implementations as implementations make sure that this is always (at least) a copy.

#### putWithErrors

    public final <T extends V> T putWithErrors(K key,
                                T data)
                                    throws CacheException

Description copied from interface: `Cache`
Store an entry in the cache with the default time to live.

Specified by:
  `putWithErrors` in interface `Cache<K,V>`
Parameters:
  `key` - A unique identifier.
  `data` - The data to store. Make sure that you don't alter the data after it has been put to cache as (depending on the implementation) this might lead to altering the entry in the cache. Use the returned data instead.
Returns:
  The entry that has been stored in the cache. It is save to alter this as implementations as implementations make sure that this is always (at least) a copy.
Throws:
  `CacheException` - Thrown on any errors encountered, supposed to include the stacktrace (if any).

#### delete

    public final void delete(K key)
                      throws CacheException

Description copied from interface: `Cache`
Manually remove an entry from the cache.

Specified by:
  `delete` in interface `Cache<K,V>`
Parameters:
  `key` - The key under which the entry was stored.
Throws:
  `CacheException` - Thrown on any errors encountered, supposed to include the stacktrace (if any).

#### cleanUp

    public final void cleanUp()
                       throws CacheException

Description copied from interface: `Cache`
Cleanup the cache. Usually this means that expired entries are deleted but this can also incorporate optimization functions etc. depending on the implementation.

Specified by:
  `cleanUp` in interface `Cache<K,V>`
Throws:
  `CacheException` - Thrown on any errors encountered, supposed to include the stacktrace (if any).

#### getMetaData

    public MetaData getMetaData(K key)
                         throws CacheException

Description copied from interface: `Cache`
Retrieve the `MetaData` of a cached entry if it is available.

Specified by:
  `getMetaData` in interface `Cache<K,V>`
Parameters:
  `key` - The key under which the entry was stored.
Returns:
  The `MetaData` of the entry if found, null otherwise.
Throws:
  `CacheException` - Thrown on any errors encountered, supposed to include the stacktrace (if any).

#### get

    public <T extends V> T get(K key)

Description copied from interface: `Cache`
Retrieve an entry from the cache if it is available. Any errors are swallowed, use `Cache.getWithErrors(Object)` if you want to handle them.

Specified by:
  `get` in interface `Cache<K,V>`
Parameters:
  `key` - The unique key under which the entry was stored.
Returns:
  The cache entry if it is valid and not expired, null otherwise.

#### getWithErrors

    public final <T extends V> T getWithErrors(K key)
                                    throws CacheException

Description copied from interface: `Cache`
Retrieve an entry from the cache if it is available.

Specified by:
  `getWithErrors` in interface `Cache<K,V>`
Parameters:
  `key` - The unique key under which the entry was stored.
Returns:
  The cache entry if it is valid and not expired, null otherwise.
Throws:
  `CacheException` - Thrown on any errors encountered, supposed to include the stacktrace (if any).

#### getStatistics

    public SimpleCacheStatistics getStatistics()

Description copied from interface: `Cache`
Get statistics of the cache like hitrate, or size.

Specified by:
  `getStatistics` in interface `Cache<K,V>`
Returns:
  Statistics of the cache.

#### getExpungeStrategy

    public ExpungeStrategy getExpungeStrategy()

#### setExpungeStrategy

    public void setExpungeStrategy(ExpungeStrategy expungeStrategy)

#### setEntriesMetaDataMap

    protected void setEntriesMetaDataMap(Map<K,M> map)

#### getEntriesMetaDataMap

    protected Map<K,M> getEntriesMetaDataMap()

#### expunge

    protected void expunge(Collection<Map.Entry<K,M>> expungeEntriesMetaData)
                    throws CacheException

Performs the actual expunging of entries with the given metadata. Can be subclassed as they might want to chain or do some other magic, by default the entries will simply be deleted.

Parameters:
  `expungeEntriesMetaData` - A List of the `MetaData` of the entries to expunge.
Throws:
  `CacheException` - Thrown on any errors encountered.

#### delete

    public void delete(K key,
              M metaData)
                throws CacheException

Throws:
  `CacheException`

#### getEntriesMetaData

    public List<M> getEntriesMetaData()

Returns a copy of the List of all metadata entries.

Returns:
  a copy of the List of all metadata entries.

#### doDelete

    protected abstract void doDelete(K key,
                M metaData)
                              throws CacheException

Performs deletion of the data belonging to the metadata. Removal from the map is done by the superclass.

Throws:
  `CacheException`

#### doGet

    protected abstract <T extends V> T doGet(K key,
                        M metaData)
                                  throws CacheException

Performs retrieval of the data belonging to the metadata.

Throws:
  `CacheException`

#### doPut

    protected abstract <T extends V> T doPut(K key,
                        T data)
                                  throws CacheException

Performs storage of the given data and adds new metadata to the map.

Throws:
  `CacheException`