org.sape.carbon.services.cache.total
Class ReadOnlyCache

java.lang.Object
  |
  +--org.sape.carbon.services.cache.total.AbstractTotalCache
        |
        +--org.sape.carbon.services.cache.total.ReadOnlyCache
All Implemented Interfaces:
Cache, Configurable, FunctionalInterface, Map, Schedulable, Startable, Suspendable

public class ReadOnlyCache
extends AbstractTotalCache
implements Configurable

This Total Cache implementation is intended for fast reading of data which expires periodically as a whole. This implementation is optimized for read operations. All operations on the Map interface that could modify the cache are not supported and throw UnsupportedOperationException.

Two hashmaps are used during the operation of the cache. The activeMap is the active map which is used to service all get requests for the cache. The holdingMap is used to reinitialize the cache. A call to initializeHoldingCache() creates a new holding map. The new map is then filled with all the information from the data loader. Then activateHoldingCache() atomically replaces the activeMap with the holdingMap and dereferences the old activeMap for garbage collection.

Copyright 2003 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.13 $($Author: ghinkl $ / $Date: 2003/09/30 02:43:48 $)
Author:
Douglas Voet, March 2002

Field Summary
private  Map activeMap
          The active map the cache queries against.
private  TotalCacheDataLoader dataLoader
          Holds the dataloader for this cache.
private  Map holdingMap
          A background map used for reloading the cache to minimize synchronization time.
private  org.apache.commons.logging.Log log
          The handle to Apache-commons logger
 
Constructor Summary
ReadOnlyCache()
           
 
Method Summary
private  void activateHoldingCache()
          Replace the activeMap with the holdingMap
 void clear()
          Removes all mappings from this cache.
 void configure(ComponentConfiguration configuration)
          Configure the component.
 Set entrySet()
          Returns a set view of the mappings contained in this cache.
protected  Map getCacheMap()
          Returns a referench to the Map that contains all the cached data.
private  void initializeHoldingCache()
          Initialize the holdingMap
 Set keySet()
          Returns the keys of the cache.
 Object put(Object key, Object value)
          Put the Object value into the cache referenced by the Object key
 void putAll(Map t)
          Copies all of the mappings from the specified map to this cache.
 void refreshAll()
          Refresh the contents of the cache.
 Object remove(Object key)
          Removes the mapping for this key from this cache.
 Collection values()
          Returns a collection view of the values contained in this cache.
 
Methods inherited from class org.sape.carbon.services.cache.total.AbstractTotalCache
containsKey, containsValue, get, isEmpty, resume, runScheduledTask, size, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

log

private org.apache.commons.logging.Log log
The handle to Apache-commons logger


activeMap

private Map activeMap
The active map the cache queries against.


holdingMap

private Map holdingMap
A background map used for reloading the cache to minimize synchronization time.


dataLoader

private TotalCacheDataLoader dataLoader
Holds the dataloader for this cache.

Constructor Detail

ReadOnlyCache

public ReadOnlyCache()
Method Detail

refreshAll

public void refreshAll()
                throws CacheLoadException
Refresh the contents of the cache. First, initialize a holding cache. Then load the holding cache with new data from the data loader. Finally, activate the holding cache.

Specified by:
refreshAll in interface Cache
Specified by:
refreshAll in class AbstractTotalCache
Throws:
CacheLoadException - when its data loader has failed to load data

configure

public void configure(ComponentConfiguration configuration)
Description copied from interface: Configurable
Configure the component. This is preceded and followed by the suspend and resume operations if they are available on the component.

Specified by:
configure in interface Configurable
Parameters:
configuration - the configuration for this component
See Also:
Configurable.configure(ComponentConfiguration)

entrySet

public Set entrySet()
Description copied from class: AbstractTotalCache
Returns a set view of the mappings contained in this cache. Each element in the returned set is a Map.Entry. The set is backed by the cache, so changes to the map are reflected in the set, and vice-versa. If the cache is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the cache, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface Map
Overrides:
entrySet in class AbstractTotalCache
Returns:
an unmodifiable set, this is a read-only cache
See Also:
Map.entrySet()

keySet

public Set keySet()
Description copied from class: AbstractTotalCache
Returns the keys of the cache.

Specified by:
keySet in interface Map
Overrides:
keySet in class AbstractTotalCache
Returns:
an unmodifiable set, this is a read-only cache
See Also:
Map.keySet()

values

public Collection values()
Description copied from class: AbstractTotalCache
Returns a collection view of the values contained in this cache. The collection is backed by the cache, so changes to the cache are reflected in the collection, and vice-versa.

Specified by:
values in interface Map
Overrides:
values in class AbstractTotalCache
Returns:
an unmodifiable collection, this is a read-only cache
See Also:
Map.values()

clear

public void clear()
Description copied from class: AbstractTotalCache
Removes all mappings from this cache.

Specified by:
clear in interface Map
Overrides:
clear in class AbstractTotalCache
Throws:
UnsupportedOperationException - this is a read-only cache, modification methods are not supported
See Also:
Map.clear()

put

public Object put(Object key,
                  Object value)
Description copied from class: AbstractTotalCache
Put the Object value into the cache referenced by the Object key

Specified by:
put in interface Map
Overrides:
put in class AbstractTotalCache
Parameters:
key - the key to add to the map
value - the value to add for the key
Returns:
the old value at the given key
Throws:
UnsupportedOperationException - this is a read-only cache, modification methods are not supported
See Also:
Map.put(Object, Object)

putAll

public void putAll(Map t)
Description copied from class: AbstractTotalCache
Copies all of the mappings from the specified map to this cache. These mappings will replace any mappings that this cache had for any of the keys currently in the specified cache.

Specified by:
putAll in interface Map
Overrides:
putAll in class AbstractTotalCache
Parameters:
t - the map to place into the cache
Throws:
UnsupportedOperationException - this is a read-only cache, modification methods are not supported
See Also:
Map.putAll(Map)

remove

public Object remove(Object key)
Description copied from class: AbstractTotalCache
Removes the mapping for this key from this cache.

Specified by:
remove in interface Map
Overrides:
remove in class AbstractTotalCache
Parameters:
key - the key to remove from this map
Returns:
the value stored at the given key or null
Throws:
UnsupportedOperationException - this is a read-only cache, modification methods are not supported
See Also:
Map.remove(Object)

getCacheMap

protected Map getCacheMap()
Description copied from class: AbstractTotalCache
Returns a referench to the Map that contains all the cached data. This is used by the Map interface methods of this class.

Specified by:
getCacheMap in class AbstractTotalCache
Returns:
Map containing the cached data
See Also:
AbstractTotalCache.getCacheMap()

activateHoldingCache

private void activateHoldingCache()

Replace the activeMap with the holdingMap


initializeHoldingCache

private void initializeHoldingCache()
Initialize the holdingMap



Copyright 1999-2003 Sapient Corporation. All Rights Reserved.