public class WeakLevel2Cache extends Object implements Level2Cache
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.
Modifier and Type | Field and Description |
---|---|
protected ApiAdapter |
apiAdapter |
protected Map<Object,CachedPC> |
pinnedCache
Pinned objects cache.
|
protected Collection<org.datanucleus.cache.WeakLevel2Cache.PinnedClass> |
pinnedClasses
Collection of pinned classes whose objects should be pinned if they ever reach the cache.
|
protected Collection |
pinnedIds
Collection of ids whose objects should be pinned if they ever reach the cache.
|
protected Map<CacheUniqueKey,CachedPC> |
uniqueKeyCache
Unique Key cache.
|
protected Map<Object,CachedPC> |
unpinnedCache
Unpinned objects cache.
|
Modifier | Constructor and Description |
---|---|
protected |
WeakLevel2Cache() |
|
WeakLevel2Cache(NucleusContext nucleusCtx)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Method to close the cache when no longer needed.
|
boolean |
containsOid(Object oid)
Method to check if an object with the specified id is in the cache
|
void |
evict(Object oid)
Method to evict an object from the cache.
|
void |
evictAll()
Method to evict all objects from the L2 cache.
|
void |
evictAll(Class pcClass,
boolean subclasses)
Method to evict all objects of the given types from the cache.
|
void |
evictAll(Collection oids)
Method to evict the objects with the specified ids.
|
void |
evictAll(Object[] oids)
Method to evict the objects with the specified ids.
|
CachedPC |
get(Object oid)
Accessor for an object from the cache.
|
Map<Object,CachedPC> |
getAll(Collection oids)
Accessor for a collection of objects from the cache.
|
int |
getNumberOfPinnedObjects()
Accessor for the number of pinned objects in the cache.
|
int |
getNumberOfUnpinnedObjects()
Accessor for the number of unpinned objects in the cache.
|
int |
getSize()
Accessor for the total number of objects in the L2 cache.
|
CachedPC |
getUnique(CacheUniqueKey key)
Method to retrieve the id represented by the specified unique key.
|
boolean |
isEmpty()
Accessor for whether the cache is empty.
|
void |
pin(Object oid)
Method to pin an object to the cache.
|
void |
pinAll(Class cls,
boolean subs)
Method to pin all objects of the given types.
|
void |
pinAll(Collection oids)
Method to pin all of the supplied objects
|
void |
pinAll(Object[] oids)
Method to pin all of the supplied objects
|
CachedPC |
put(Object oid,
CachedPC pc)
Method to put an object in the cache.
|
void |
putAll(Map<Object,CachedPC> objs)
Method to put several objects into the cache.
|
CachedPC |
putUnique(CacheUniqueKey key,
CachedPC pc)
Method to store a persistable object for this unique key.
|
void |
putUniqueAll(Map<CacheUniqueKey,CachedPC> objs)
Method to put several objects into the cache.
|
void |
removeUnique(CacheUniqueKey key)
Method to remove any object cached against the provided unique key.
|
void |
unpin(Object oid)
Method to unpin an object
|
void |
unpinAll(Class cls,
boolean subs)
Method to unpin all objects of the specified types.
|
void |
unpinAll(Collection oids)
Method to unpin all of the supplied objects
|
void |
unpinAll(Object[] oids)
Method to unpin all of the specified objects
|
protected Collection<org.datanucleus.cache.WeakLevel2Cache.PinnedClass> pinnedClasses
protected Collection pinnedIds
protected transient Map<CacheUniqueKey,CachedPC> uniqueKeyCache
protected ApiAdapter apiAdapter
protected WeakLevel2Cache()
public WeakLevel2Cache(NucleusContext nucleusCtx)
nucleusCtx
- Contextpublic void close()
close
in interface Level2Cache
public void evict(Object oid)
evict
in interface Level2Cache
oid
- The id of the object to evictpublic void evictAll()
evictAll
in interface Level2Cache
public void evictAll(Class pcClass, boolean subclasses)
evictAll
in interface Level2Cache
pcClass
- The class to evictsubclasses
- Whether to also evict subclassespublic void evictAll(Collection oids)
evictAll
in interface Level2Cache
oids
- The ids of the objects to evictpublic void evictAll(Object[] oids)
evictAll
in interface Level2Cache
oids
- The ids of the objects to evictpublic CachedPC get(Object oid)
get
in interface Level2Cache
oid
- The Object IDpublic Map<Object,CachedPC> getAll(Collection oids)
Level2Cache
getAll
in interface Level2Cache
oids
- The Object IDspublic int getNumberOfPinnedObjects()
getNumberOfPinnedObjects
in interface Level2Cache
public int getNumberOfUnpinnedObjects()
getNumberOfUnpinnedObjects
in interface Level2Cache
public int getSize()
getSize
in interface Level2Cache
public void putAll(Map<Object,CachedPC> objs)
Level2Cache
putAll
in interface Level2Cache
objs
- Map of cacheable object keyed by its oid.public CachedPC put(Object oid, CachedPC pc)
put
in interface Level2Cache
oid
- The Object id for this objectpc
- The cacheable objectpublic boolean containsOid(Object oid)
containsOid
in interface Level2Cache
oid
- The object IDpublic boolean isEmpty()
isEmpty
in interface Level2Cache
public CachedPC getUnique(CacheUniqueKey key)
Level2Cache
getUnique
in interface Level2Cache
key
- Unique keypublic CachedPC putUnique(CacheUniqueKey key, CachedPC pc)
Level2Cache
putUnique
in interface Level2Cache
key
- The unique keypc
- The representation of the persistable object to cachepublic void putUniqueAll(Map<CacheUniqueKey,CachedPC> objs)
Level2Cache
putUniqueAll
in interface Level2Cache
objs
- Map of cacheable object keyed by the unique keys.public void removeUnique(CacheUniqueKey key)
Level2Cache
removeUnique
in interface Level2Cache
key
- Unique keypublic void pin(Object oid)
pin
in interface Level2Cache
oid
- The id of the object to pinpublic void pinAll(Class cls, boolean subs)
pinAll
in interface Level2Cache
cls
- The classsubs
- Whether to include subclassespublic void pinAll(Collection oids)
pinAll
in interface Level2Cache
oids
- The Object ids to pinpublic void pinAll(Object[] oids)
pinAll
in interface Level2Cache
oids
- The object ids to pinpublic void unpin(Object oid)
unpin
in interface Level2Cache
oid
- The object idpublic void unpinAll(Class cls, boolean subs)
unpinAll
in interface Level2Cache
cls
- Base classsubs
- Whether to include subclassespublic void unpinAll(Collection oids)
unpinAll
in interface Level2Cache
oids
- The object ids to unpinpublic void unpinAll(Object[] oids)
unpinAll
in interface Level2Cache
oids
- The object ids to unpinCopyright © 2019. All rights reserved.