Class AbstractReferencedLevel2Cache
java.lang.Object
org.datanucleus.cache.AbstractReferencedLevel2Cache
- All Implemented Interfaces:
Serializable, Level2Cache
- Direct Known Subclasses:
SoftLevel2Cache, WeakLevel2Cache
Abstract referenced implementation of a Level 2 cache, to be used as the base for weak/soft variants.
Operates with 3 maps internally. One stores all pinned objects that have been selected to be retained by user's application. The second stores all other objects, and is the default location where objects are placed when being added here, using weak references meaning that they can get garbage collected as necessary by the JVM. The third stores objects keyed by the unique key that they relate to.
Maintains collections of the classes and the identities that are to be pinned if they ever are put into the cache. These are defined by the pinAll(), pin() methods.
All mutating methods, and the get method have been synchronized to prevent conflicts.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classRepresentation of a class whose objects will be pinned when put into the L2 cache. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ApiAdapterprivate intPinned objects cache.Collection of pinned classes whose objects should be pinned if they ever reach the cache.protected CollectionCollection of ids whose objects should be pinned if they ever reach the cache.private static final longprotected Map<CacheUniqueKey, CachedPC> Unique Key cache.Unpinned objects cache. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractReferencedLevel2Cache(NucleusContext nucleusCtx) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Method to close the cache when no longer needed.booleancontainsOid(Object oid) Method to check if an object with the specified id is in the cachevoidMethod to evict an object from the cache.voidevictAll()Method to evict all objects from the L2 cache.voidMethod to evict all objects of the given types from the cache.voidMethod to evict the objects with the specified ids.voidevictAll(Collection oids) Method to evict the objects with the specified ids.Accessor for an object from the cache.getAll(Collection oids) Accessor for a collection of objects from the cache.intAccessor for the number of pinned objects in the cache.intAccessor for the number of unpinned objects in the cache.intgetSize()Accessor for the total number of objects in the L2 cache.getUnique(CacheUniqueKey key) Method to retrieve the id represented by the specified unique key.protected abstract voidbooleanisEmpty()Accessor for whether the cache is empty.voidMethod to pin an object to the cache.voidMethod to pin all objects of the given types.voidMethod to pin all of the supplied objectsvoidpinAll(Collection oids) Method to pin all of the supplied objectsMethod to put an object in the cache.voidMethod to put several objects into the cache.putUnique(CacheUniqueKey key, CachedPC pc) Method to store a persistable object for this unique key.voidputUniqueAll(Map<CacheUniqueKey, CachedPC> objs) Method to put several objects into the cache.private voidvoidMethod to remove any object cached against the provided unique key.voidMethod to unpin an objectvoidMethod to unpin all objects of the specified types.voidMethod to unpin all of the specified objectsvoidunpinAll(Collection oids) Method to unpin all of the supplied objectsprivate void
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
pinnedClasses
Collection of pinned classes whose objects should be pinned if they ever reach the cache. -
pinnedIds
Collection of ids whose objects should be pinned if they ever reach the cache. -
pinnedCache
-
unpinnedCache
-
uniqueKeyCache
Unique Key cache. -
apiAdapter
-
maxSize
private int maxSize
-
-
Constructor Details
-
AbstractReferencedLevel2Cache
protected AbstractReferencedLevel2Cache() -
AbstractReferencedLevel2Cache
Constructor.- Parameters:
nucleusCtx- Context
-
-
Method Details
-
initialiseCaches
protected abstract void initialiseCaches() -
close
public void close()Method to close the cache when no longer needed. Provides a hook to release resources etc.- Specified by:
closein interfaceLevel2Cache
-
evict
Method to evict an object from the cache.- Specified by:
evictin interfaceLevel2Cache- Parameters:
oid- The id of the object to evict
-
evictAll
public void evictAll()Method to evict all objects from the L2 cache.- Specified by:
evictAllin interfaceLevel2Cache
-
evictAll
Method to evict all objects of the given types from the cache.- Specified by:
evictAllin interfaceLevel2Cache- Parameters:
pcClass- The class to evictsubclasses- Whether to also evict subclasses
-
evictAll
Method to evict the objects with the specified ids.- Specified by:
evictAllin interfaceLevel2Cache- Parameters:
oids- The ids of the objects to evict
-
evictAll
Method to evict the objects with the specified ids.- Specified by:
evictAllin interfaceLevel2Cache- Parameters:
oids- The ids of the objects to evict
-
get
Accessor for an object from the cache. The returned object will not have a StateManager connected. This is because data stored in the Level 2 cache is StateManager and PersistenceManager independent.- Specified by:
getin interfaceLevel2Cache- Parameters:
oid- The Object ID- Returns:
- The L2 cacheable object
-
getAll
Description copied from interface:Level2CacheAccessor for a collection of objects from the cache.- Specified by:
getAllin interfaceLevel2Cache- Parameters:
oids- The Object IDs- Returns:
- Map of the objects, keyed by the oids that are found
-
getNumberOfPinnedObjects
public int getNumberOfPinnedObjects()Accessor for the number of pinned objects in the cache.- Specified by:
getNumberOfPinnedObjectsin interfaceLevel2Cache- Returns:
- Number of pinned objects
-
getNumberOfUnpinnedObjects
public int getNumberOfUnpinnedObjects()Accessor for the number of unpinned objects in the cache.- Specified by:
getNumberOfUnpinnedObjectsin interfaceLevel2Cache- Returns:
- Number of unpinned objects
-
getSize
public int getSize()Accessor for the total number of objects in the L2 cache.- Specified by:
getSizein interfaceLevel2Cache- Returns:
- Number of objects
-
putAll
Description copied from interface:Level2CacheMethod to put several objects into the cache.- Specified by:
putAllin interfaceLevel2Cache- Parameters:
objs- Map of cacheable object keyed by its oid.
-
put
Method to put an object in the cache.- Specified by:
putin interfaceLevel2Cache- Parameters:
oid- The Object id for this objectpc- The cacheable object- Returns:
- The value previously associated with this oid
-
containsOid
Method to check if an object with the specified id is in the cache- Specified by:
containsOidin interfaceLevel2Cache- Parameters:
oid- The object ID- Returns:
- Whether it is present
-
isEmpty
public boolean isEmpty()Accessor for whether the cache is empty.- Specified by:
isEmptyin interfaceLevel2Cache- Returns:
- Whether it is empty.
-
getUnique
Description copied from interface:Level2CacheMethod to retrieve the id represented by the specified unique key.- Specified by:
getUniquein interfaceLevel2Cache- Parameters:
key- Unique key- Returns:
- The "identity" of the object that this unique key represents
-
putUnique
Description copied from interface:Level2CacheMethod to store a persistable object for this unique key.- Specified by:
putUniquein interfaceLevel2Cache- Parameters:
key- The unique keypc- The representation of the persistable object to cache- Returns:
- The previous object for this unique key if one was present, otherwise null
-
putUniqueAll
Description copied from interface:Level2CacheMethod to put several objects into the cache.- Specified by:
putUniqueAllin interfaceLevel2Cache- Parameters:
objs- Map of cacheable object keyed by the unique keys.
-
removeUnique
Description copied from interface:Level2CacheMethod to remove any object cached against the provided unique key.- Specified by:
removeUniquein interfaceLevel2Cache- Parameters:
key- Unique key
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-
pin
Method to pin an object to the cache.- Specified by:
pinin interfaceLevel2Cache- Parameters:
oid- The id of the object to pin
-
pinAll
Method to pin all objects of the given types.- Specified by:
pinAllin interfaceLevel2Cache- Parameters:
cls- The classsubs- Whether to include subclasses
-
pinAll
Method to pin all of the supplied objects- Specified by:
pinAllin interfaceLevel2Cache- Parameters:
oids- The Object ids to pin
-
pinAll
Method to pin all of the supplied objects- Specified by:
pinAllin interfaceLevel2Cache- Parameters:
oids- The object ids to pin
-
unpin
Method to unpin an object- Specified by:
unpinin interfaceLevel2Cache- Parameters:
oid- The object id
-
unpinAll
Method to unpin all objects of the specified types.- Specified by:
unpinAllin interfaceLevel2Cache- Parameters:
cls- Base classsubs- Whether to include subclasses
-
unpinAll
Method to unpin all of the supplied objects- Specified by:
unpinAllin interfaceLevel2Cache- Parameters:
oids- The object ids to unpin
-
unpinAll
Method to unpin all of the specified objects- Specified by:
unpinAllin interfaceLevel2Cache- Parameters:
oids- The object ids to unpin
-