Package org.apache.openjpa.datacache
Class AbstractDataCache
java.lang.Object
org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
org.apache.openjpa.datacache.AbstractDataCache
- All Implemented Interfaces:
Serializable,DataCache,Configurable,Clearable,Closeable,EventManager
- Direct Known Subclasses:
ConcurrentDataCache
public abstract class AbstractDataCache
extends AbstractConcurrentEventManager
implements DataCache, Configurable
Abstract
DataCache implementation that provides various
statistics, logging, and timeout functionality common across cache
implementations.- Author:
- Patrick Linskey, Abe White
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected CacheStatisticsSPIprotected OpenJPAConfigurationThe configuration set by the system.protected LogThe log to use.Fields inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
_listenersFields inherited from interface org.apache.openjpa.datacache.DataCache
NAME_DEFAULT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a new expiration event listener to this cache.voidafterCommit(RemoteCommitEvent event) This method is part of theRemoteCommitListenerinterface.voidclear()Remove all data from this cache.protected abstract voidClear the cache.voidclose()Free the resources used by this cache.protected voidclose(boolean clear) voidcommit(Collection<DataCachePCData> additions, Collection<DataCachePCData> newUpdates, Collection<DataCachePCData> existingUpdates, Collection<Object> deletes) Perform a batch update of the cache.booleanReturnstrueif this cache contains data corresponding tooid; otherwise returnsfalse.containsAll(Collection<Object> keys) Returns the indexes of the oids in this cache.voidInvoked upon completion of bean property configuration for this object.protected voidImplement this method to fire the given event to the given listener.Return the cached object for the given oid.Returns the objects for the given key List.voidbooleanReturns whether the the cache needs to be updated when bulk updates as executed.protected abstract DataCachePCDatagetInternal(Object oid) Return the object for the given oid.getName()Returns a string name that can be used by end-user-visible code to identify this cache.getPartition(String name, boolean create) Gets the named partition.Gets the name of the known partitions.Returns number of read/write request and cache hit ratio data.getTypes()voidinitialize(DataCacheManager manager) Initialize any resources associated with the givenDataCacheManager.booleanisClosed()booleanAffirms if this cache maintains partitions.protected voidkeyRemoved(Object key, boolean expired) Invoke when a key is removed from this cache.booleanPin the value stored underoidinto the cache.voidPin all oids for the given type.pinAll(Collection<Object> keys) Pin all oids to the cache.protected abstract booleanpinInternal(Object oid) Pin an object to the cache.put(DataCachePCData data) Set the cached value for the given instance.protected voidAdd all of the given objects to the cache.protected abstract DataCachePCDataputInternal(Object oid, DataCachePCData pc) Add the given object to the cache, returning the old object under the given oid.protected booleanReturntrueif updates to data already in the cache (either incommit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)or theupdate(org.apache.openjpa.datacache.DataCachePCData)) should be put back into the cache.Remove the value stored under the given oid.voidRemove the objects of the given class from the cache.removeAll(Collection<Object> keys) Remove the values stored under the given oids.protected abstract voidremoveAllInternal(Class<?> cls, boolean subclasses) Evict objects in cache by class.protected voidremoveAllInternal(Collection<Object> oids) Remove all objects under the given oids from the cache.protected voidremoveAllTypeNamesInternal(Collection<String> classNames) Remove all objects of the given class names from the cache.booleanRemove an expiration event listener from this cache.protected abstract DataCachePCDataremoveInternal(Object oid) Remove the object under the given oid from the cache.voidInvoked prior to setting bean properties.voidsetEnableStatistics(boolean enable) voidvoidsetEvictOnBulkUpdate(boolean b) voidsetExcludedTypes(String types) voidsetExcludedTypes(Set<String> types) voidSets a string name to be used to identify this cache to end-user needs.voidvoidvoidInvoked before bean property configuration is begun on this object.toString()booleanUnpin the value stored underoidfrom the cache.voidUnpin all oids associaed with the given type from the cache.unpinAll(Collection<Object> keys) Unpin all oids from the cache.protected abstract booleanunpinInternal(Object oid) Unpin an object from the cache.voidupdate(DataCachePCData data) Update the cached value for the given instance.Methods inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
addListener, fireEvent, getListeners, hasListener, hasListeners, isFailFast, newListenerCollection, removeListener, setFailFastMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.openjpa.datacache.DataCache
writeLock, writeUnlock
-
Field Details
-
_stats
-
conf
The configuration set by the system. -
log
The log to use. -
_includedTypes
-
_excludedTypes
-
_evictOnBulkUpdate
protected boolean _evictOnBulkUpdate
-
-
Constructor Details
-
AbstractDataCache
public AbstractDataCache()
-
-
Method Details
-
getName
Description copied from interface:DataCacheReturns a string name that can be used by end-user-visible code to identify this cache. -
setName
Description copied from interface:DataCacheSets a string name to be used to identify this cache to end-user needs. -
setEnableStatistics
public void setEnableStatistics(boolean enable) -
getEnableStatistics
public void getEnableStatistics() -
getEvictionSchedule
-
setEvictionSchedule
-
initialize
Description copied from interface:DataCacheInitialize any resources associated with the givenDataCacheManager.- Specified by:
initializein interfaceDataCache
-
commit
public void commit(Collection<DataCachePCData> additions, Collection<DataCachePCData> newUpdates, Collection<DataCachePCData> existingUpdates, Collection<Object> deletes) Description copied from interface:DataCachePerform a batch update of the cache. Add allDataCachePCDataobjects inadditionsand innewUpdates, make the appropriate modifications to all DataCachePCDatas inexistingUpdates, and delete all OIDs indeletes. All changes made to cached data must be made via this method. It is this method that is responsible for performing any side-effects that should happen on meaningful cache changes. Implementations should bear in mind that thedeletescollection may contain oids that are also in theadditionsmap. This is possible because it is valid for a user to delete an object with a particular oid and then add that object in the same batch.- Specified by:
commitin interfaceDataCache- Parameters:
additions- A collection ofDataCachePCDataobjects. These represent data that have been newly created, and thus must be added to the cache.newUpdates- A collection ofDataCachePCDataobjects. These represent data that have been modified but were not originally in the cache, and thus must be added to the cache.existingUpdates- A collection ofDataCachePCDataobjects. These represent data that have been modified and were originally loaded from the cache. It is up to the cache implementation to decide if these values must be re-enlisted in the cache. Some caches may return live data fromDataCache.get(java.lang.Object)invocations, in which case these values need not be re-enlisted.deletes- A collection of object IDs that have been deleted and must therefore be dropped from the cache.
-
contains
Description copied from interface:DataCacheReturnstrueif this cache contains data corresponding tooid; otherwise returnsfalse. -
containsAll
Description copied from interface:DataCacheReturns the indexes of the oids in this cache.- Specified by:
containsAllin interfaceDataCache
-
get
Description copied from interface:DataCacheReturn the cached object for the given oid. Modifying the returned object may or may not change the cached value; theDataCache.update(org.apache.openjpa.datacache.DataCachePCData)method should be used to re-cache any changed objects. -
getAll
Returns the objects for the given key List. -
put
Description copied from interface:DataCacheSet the cached value for the given instance. This does not result in an update of other caches. Rather, it should only be used for loading clean data into the cache. Meaningful changes to the state of the cache should be made via theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)method.- Specified by:
putin interfaceDataCache- Returns:
- The previously cached value, or
nullif the value was not previously cached. SeeMap.put(K, V)for more information.
-
update
Description copied from interface:DataCacheUpdate the cached value for the given instance. This does not result in an update of other caches. Rather, it should only be used for loading clean data into the cache. Meaningful changes to the state of the cache should be made via theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)method. A cache implementation may or may not return a live object fromDataCache.get(java.lang.Object)invocations. If an object retrieved from aDataCache.get(java.lang.Object)operation needs to be updated, this method can be invoked instead of invokingDataCache.put(org.apache.openjpa.datacache.DataCachePCData). The DataCache implementation can then make optimizations based on how itsDataCache.get(java.lang.Object)method works. -
remove
Description copied from interface:DataCacheRemove the value stored under the given oid. This does not result in an update of other caches. Rather, it should only be used for removing data in the cache. Meaningful changes to the state of the cache should be made via theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)method.- Specified by:
removein interfaceDataCache- Returns:
- The previously cached value, or
nullif the oid was not previously cached. SeeMap.remove(java.lang.Object)for more information.
-
removeAll
Description copied from interface:DataCacheRemove the values stored under the given oids. -
removeAll
Remove the objects of the given class from the cache. -
pin
Description copied from interface:DataCachePin the value stored underoidinto the cache. This method guarantees thatoid's value will not be dropped by the caching algorithm. This method does not affect the behavior ofDataCache.remove(java.lang.Object). -
pinAll
Description copied from interface:DataCachePin all oids to the cache. -
pinAll
Description copied from interface:DataCachePin all oids for the given type. -
unpin
Description copied from interface:DataCacheUnpin the value stored underoidfrom the cache. This method reverses a previous invocation ofDataCache.pin(java.lang.Object). This method does not remove anything from the cache; it merely makesoid's value a candidate for flushing from the cache. -
unpinAll
Description copied from interface:DataCacheUnpin all oids from the cache. -
unpinAll
Description copied from interface:DataCacheUnpin all oids associaed with the given type from the cache. -
clear
public void clear()Description copied from interface:DataCacheRemove all data from this cache. This does not result in an update of other caches. Rather, it should only be used for clearing the cache. Meaningful changes to the state of the cache should be made via theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)method. -
close
public void close()Description copied from interface:DataCacheFree the resources used by this cache. -
close
protected void close(boolean clear) -
isClosed
public boolean isClosed() -
addExpirationListener
Description copied from interface:DataCacheAdd a new expiration event listener to this cache.- Specified by:
addExpirationListenerin interfaceDataCache
-
removeExpirationListener
Description copied from interface:DataCacheRemove an expiration event listener from this cache.- Specified by:
removeExpirationListenerin interfaceDataCache
-
toString
-
afterCommit
This method is part of theRemoteCommitListenerinterface. If your cache subclass relies on OpenJPA for clustering support, make it implementRemoteCommitListener. This method will take care of invalidating entries from remote commits. -
keyRemoved
Invoke when a key is removed from this cache. Propagates the expiration event on to all expiration listeners registered with this class. -
recacheUpdates
protected boolean recacheUpdates()Returntrueif updates to data already in the cache (either incommit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)or theupdate(org.apache.openjpa.datacache.DataCachePCData)) should be put back into the cache. Returns false by default. -
getInternal
Return the object for the given oid. -
putInternal
Add the given object to the cache, returning the old object under the given oid. -
putAllInternal
Add all of the given objects to the cache. -
removeInternal
Remove the object under the given oid from the cache. -
removeAllInternal
Evict objects in cache by class. -
removeAllInternal
Remove all objects under the given oids from the cache. -
removeAllTypeNamesInternal
Remove all objects of the given class names from the cache. -
clearInternal
protected abstract void clearInternal()Clear the cache. -
pinInternal
Pin an object to the cache. -
unpinInternal
Unpin an object from the cache. -
getPartition
Description copied from interface:DataCacheGets the named partition. Note that a partition itself is another cache.- Specified by:
getPartitionin interfaceDataCache- Parameters:
name- name of the given partition.create- if true optionally create a new partition.- Returns:
- a partition of the given name. Or null, if either no such partition exists or can not be created.
-
getPartitionNames
Description copied from interface:DataCacheGets the name of the known partitions.- Specified by:
getPartitionNamesin interfaceDataCache- Returns:
- empty set if no partition exists.
-
isPartitioned
public boolean isPartitioned()Description copied from interface:DataCacheAffirms if this cache maintains partitions.- Specified by:
isPartitionedin interfaceDataCache
-
getStatistics
Description copied from interface:DataCacheReturns number of read/write request and cache hit ratio data.- Specified by:
getStatisticsin interfaceDataCache
-
setConfiguration
Description copied from interface:ConfigurableInvoked prior to setting bean properties.- Specified by:
setConfigurationin interfaceConfigurable
-
startConfiguration
public void startConfiguration()Description copied from interface:ConfigurableInvoked before bean property configuration is begun on this object.- Specified by:
startConfigurationin interfaceConfigurable
-
endConfiguration
public void endConfiguration()Description copied from interface:ConfigurableInvoked upon completion of bean property configuration for this object.- Specified by:
endConfigurationin interfaceConfigurable
-
fireEvent
Description copied from class:AbstractConcurrentEventManagerImplement this method to fire the given event to the given listener.- Specified by:
fireEventin classAbstractConcurrentEventManager
-
getTypes
-
getExcludedTypes
-
setTypes
-
setTypes
-
setExcludedTypes
-
setExcludedTypes
-
selectCache
-
getEvictOnBulkUpdate
public boolean getEvictOnBulkUpdate()Description copied from interface:DataCacheReturns whether the the cache needs to be updated when bulk updates as executed. Defaults to true.- Specified by:
getEvictOnBulkUpdatein interfaceDataCache
-
setEvictOnBulkUpdate
public void setEvictOnBulkUpdate(boolean b)
-