T
- The type of the key that is used for identifying stored classes per class loader. Such keys must not strongly reference any
types or class loaders without potentially corrupting the garbage eligibility of stored classes. As the storage is segmented
by class loader, it is normally sufficient to store types by their name.public class TypeCache<T> extends ReferenceQueue<ClassLoader>
A cache for storing types without strongly referencing any class loader or type.
Note: In order to clean obsolete class loader references from the map, expungeStaleEntries()
must be called
regularly. This can happen in a different thread, in custom intervals or on every use of the cache by creating an instance of
TypeCache.WithInlineExpunction
. This cache is fully thread-safe.
Important: The behavior of a type cache might not be as expected. A class is only eligible for garbage collection once its class loader is eligible for garbage collection. At the same time, a garbage collector is only eligible for garbage collection once all of its classes are eligible for garbage collection. If this cache referenced the cached type strongly, this would never be the case which is why this cache maintains either strong or weak references. In the latter case, a type is typically retained until the last instance of the type is not eligible for garbage collection. With soft references, the type is typically retained until the next full garbage collection where all instances of the type are eligible for garbage collection.
Modifier and Type | Class and Description |
---|---|
protected static class |
TypeCache.LookupKey
A key used for looking up a previously inserted class loader cache.
|
static class |
TypeCache.SimpleKey
A simple key based on a collection of types where no type is strongly referenced.
|
static class |
TypeCache.Sort
Determines the storage format for a cached type.
|
protected static class |
TypeCache.StorageKey
A key used for storing a class loader cache reference.
|
static class |
TypeCache.WithInlineExpunction<S>
An implementation of a
TypeCache where obsolete references are cleared upon any call. |
Modifier and Type | Field and Description |
---|---|
protected ConcurrentMap<TypeCache.StorageKey,ConcurrentMap<T,Reference<Class<?>>>> |
cache
The underlying map containing cached objects.
|
protected TypeCache.Sort |
sort
The reference type to use for stored types.
|
Constructor and Description |
---|
TypeCache(TypeCache.Sort sort)
Creates a new type cache.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the entire cache.
|
void |
expungeStaleEntries()
Removes any stale class loader entries from the cache.
|
Class<?> |
find(ClassLoader classLoader,
T key)
Finds a stored type or returns
null if no type was stored. |
Class<?> |
findOrInsert(ClassLoader classLoader,
T key,
Callable<Class<?>> lazy)
Finds an existing type or inserts a new one if the previous type was not found.
|
Class<?> |
findOrInsert(ClassLoader classLoader,
T key,
Callable<Class<?>> lazy,
Object monitor)
Finds an existing type or inserts a new one if the previous type was not found.
|
Class<?> |
insert(ClassLoader classLoader,
T key,
Class<?> type)
Inserts a new type into the cache.
|
poll, remove, remove
protected final TypeCache.Sort sort
protected final ConcurrentMap<TypeCache.StorageKey,ConcurrentMap<T,Reference<Class<?>>>> cache
public TypeCache(TypeCache.Sort sort)
sort
- The reference type to use for stored types.public Class<?> find(ClassLoader classLoader, T key)
null
if no type was stored.classLoader
- The class loader for which this type is stored.key
- The key for the type in question.null
if no type was stored.public Class<?> insert(ClassLoader classLoader, T key, Class<?> type)
classLoader
- The class loader for which this type is stored.key
- The key for the type in question.type
- The type to insert of no previous type was stored in the cache.public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy)
classLoader
- The class loader for which this type is stored.key
- The key for the type in question.lazy
- A lazy creator for the type to insert of no previous type was stored in the cache.public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor)
classLoader
- The class loader for which this type is stored.key
- The key for the type in question.lazy
- A lazy creator for the type to insert of no previous type was stored in the cache.monitor
- A monitor to lock before creating the lazy type.public void expungeStaleEntries()
public void clear()
Copyright © 2014–2019. All rights reserved.