Package org.apache.openjpa.datacache
Class ConcurrentDataCache
- java.lang.Object
-
- org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
-
- org.apache.openjpa.datacache.AbstractDataCache
-
- org.apache.openjpa.datacache.ConcurrentDataCache
-
- All Implemented Interfaces:
java.io.Serializable
,DataCache
,RemoteCommitListener
,Configurable
,Clearable
,Closeable
,EventManager
- Direct Known Subclasses:
PartitionedDataCache
public class ConcurrentDataCache extends AbstractDataCache implements RemoteCommitListener
ADataCache
implementation that is optimized for concurrent access. When the cache fills up, values to remove from cache are chosen randomly. Due to race conditions, it is possible that a get call might not return a cached instance if that instance is being transferred between internal datastructures.- Since:
- 0.4.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_lru
-
Fields inherited from class org.apache.openjpa.datacache.AbstractDataCache
_evictOnBulkUpdate, _excludedTypes, _includedTypes, _stats, conf, log
-
Fields inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
_listeners
-
Fields inherited from interface org.apache.openjpa.datacache.DataCache
NAME_DEFAULT
-
-
Constructor Summary
Constructors Constructor Description ConcurrentDataCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
clearInternal()
Clear the cache.CacheMap
getCacheMap()
Returns the underlyingCacheMap
that this cache is using.int
getCacheSize()
Returns the maximum number of unpinned objects to keep hard references to.protected DataCachePCData
getInternal(java.lang.Object key)
Return the object for the given oid.boolean
getLru()
int
getSoftReferenceSize()
Returns the maximum number of unpinned objects to keep soft references to.void
initialize(DataCacheManager mgr)
Initialize any resources associated with the givenDataCacheManager
.protected CacheMap
newCacheMap()
Return the map to use as an internal cache; entry expirations must invokeAbstractDataCache.keyRemoved(java.lang.Object, boolean)
.protected boolean
pinInternal(java.lang.Object key)
Pin an object to the cache.protected DataCachePCData
putInternal(java.lang.Object key, DataCachePCData pc)
Add the given object to the cache, returning the old object under the given oid.protected boolean
recacheUpdates()
Returntrue
if updates to data already in the cache (either inAbstractDataCache.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>)
or theAbstractDataCache.update(org.apache.openjpa.datacache.DataCachePCData)
) should be put back into the cache.protected void
removeAllInternal(java.lang.Class<?> cls, boolean subs)
Evict objects in cache by class.protected DataCachePCData
removeInternal(java.lang.Object key)
Remove the object under the given oid from the cache.void
setCacheSize(int size)
Sets the maximum number of unpinned objects to keep hard references to.void
setLru(boolean l)
void
setSoftReferenceSize(int size)
Sets the maximum number of unpinned objects to keep soft references to.void
unpinAll(java.lang.Class<?> cls, boolean subs)
Unpin all oids associaed with the given type from the cache.protected boolean
unpinInternal(java.lang.Object key)
Unpin an object from the cache.void
writeLock()
Obtain a write lock on the cache.void
writeUnlock()
Release the write lock on the cache.-
Methods inherited from class org.apache.openjpa.datacache.AbstractDataCache
addExpirationListener, afterCommit, clear, close, close, commit, contains, containsAll, endConfiguration, fireEvent, get, getAll, getEnableStatistics, getEvictionSchedule, getEvictOnBulkUpdate, getExcludedTypes, getName, getPartition, getPartitionNames, getStatistics, getTypes, isClosed, isPartitioned, keyRemoved, pin, pinAll, pinAll, put, putAllInternal, remove, removeAll, removeAll, removeAllInternal, removeAllTypeNamesInternal, removeExpirationListener, selectCache, setConfiguration, setEnableStatistics, setEvictionSchedule, setEvictOnBulkUpdate, setExcludedTypes, setExcludedTypes, setName, setTypes, setTypes, startConfiguration, toString, unpin, unpinAll, update
-
Methods inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
addListener, fireEvent, getListeners, hasListener, hasListeners, isFailFast, newListenerCollection, removeListener, setFailFast
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.openjpa.event.RemoteCommitListener
afterCommit, close
-
-
-
-
Method Detail
-
getCacheMap
public CacheMap getCacheMap()
Returns the underlyingCacheMap
that this cache is using. This is not an unmodifiable view on the map, so care should be taken with this reference. Implementations should probably not modify the contents of the cache, but should only use this reference to obtain cache metrics.
-
setCacheSize
public void setCacheSize(int size)
Sets the maximum number of unpinned objects to keep hard references to. If the map contains more unpinned objects thansize
, then this method will result in the cache flushing old values.
-
getCacheSize
public int getCacheSize()
Returns the maximum number of unpinned objects to keep hard references to.
-
setSoftReferenceSize
public void setSoftReferenceSize(int size)
Sets the maximum number of unpinned objects to keep soft references to. If the map contains more soft references thansize
, then this method will result in the cache flushing values.
-
getSoftReferenceSize
public int getSoftReferenceSize()
Returns the maximum number of unpinned objects to keep soft references to. Defaults to-1
.
-
initialize
public void initialize(DataCacheManager mgr)
Description copied from interface:DataCache
Initialize any resources associated with the givenDataCacheManager
.- Specified by:
initialize
in interfaceDataCache
- Overrides:
initialize
in classAbstractDataCache
-
unpinAll
public void unpinAll(java.lang.Class<?> cls, boolean subs)
Description copied from interface:DataCache
Unpin all oids associaed with the given type from the cache.- Specified by:
unpinAll
in interfaceDataCache
- Overrides:
unpinAll
in classAbstractDataCache
subs
- Whether to include subclasses.
-
writeLock
public void writeLock()
Description copied from interface:DataCache
Obtain a write lock on the cache.
-
writeUnlock
public void writeUnlock()
Description copied from interface:DataCache
Release the write lock on the cache.- Specified by:
writeUnlock
in interfaceDataCache
-
newCacheMap
protected CacheMap newCacheMap()
Return the map to use as an internal cache; entry expirations must invokeAbstractDataCache.keyRemoved(java.lang.Object, boolean)
.
-
getInternal
protected DataCachePCData getInternal(java.lang.Object key)
Description copied from class:AbstractDataCache
Return the object for the given oid.- Specified by:
getInternal
in classAbstractDataCache
-
putInternal
protected DataCachePCData putInternal(java.lang.Object key, DataCachePCData pc)
Description copied from class:AbstractDataCache
Add the given object to the cache, returning the old object under the given oid.- Specified by:
putInternal
in classAbstractDataCache
-
removeInternal
protected DataCachePCData removeInternal(java.lang.Object key)
Description copied from class:AbstractDataCache
Remove the object under the given oid from the cache.- Specified by:
removeInternal
in classAbstractDataCache
-
removeAllInternal
protected void removeAllInternal(java.lang.Class<?> cls, boolean subs)
Description copied from class:AbstractDataCache
Evict objects in cache by class.- Specified by:
removeAllInternal
in classAbstractDataCache
-
clearInternal
protected void clearInternal()
Description copied from class:AbstractDataCache
Clear the cache.- Specified by:
clearInternal
in classAbstractDataCache
-
pinInternal
protected boolean pinInternal(java.lang.Object key)
Description copied from class:AbstractDataCache
Pin an object to the cache.- Specified by:
pinInternal
in classAbstractDataCache
-
unpinInternal
protected boolean unpinInternal(java.lang.Object key)
Description copied from class:AbstractDataCache
Unpin an object from the cache.- Specified by:
unpinInternal
in classAbstractDataCache
-
recacheUpdates
protected boolean recacheUpdates()
Description copied from class:AbstractDataCache
Returntrue
if updates to data already in the cache (either inAbstractDataCache.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>)
or theAbstractDataCache.update(org.apache.openjpa.datacache.DataCachePCData)
) should be put back into the cache. Returns false by default.- Overrides:
recacheUpdates
in classAbstractDataCache
-
setLru
public void setLru(boolean l)
-
getLru
public boolean getLru()
-
-