public class BOCache extends java.lang.Object implements CacheFetcherI, DBManEventListenerI, BOCacheI, DependencyCalculatorI
This cache utilizes soft references, allowing the garbage collector to reclaim memory when needed, and supports dynamic resizing to adapt to varying loads.
Key features:
Recommended use cases:
Potential drawbacks:
Avoid using in:
Future enhancements:
Usage in clients:
CacheFetcherI
,
DBManEventListenerI
,
BOCacheI
,
DependencyCalculatorI
Constructor and Description |
---|
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader)
Initializes a new instance of the BOCache class with the provided title, BO class, and cache loader.
|
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
CacheFetcherI _fetcher)
Initializes a new instance of the BOCache class with advanced configuration options.
|
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
CacheFetcherI _fetcher,
DependencyCalculatorI _dependencyCalculator) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
CacheFetcherI _fetcher,
DependencyCalculatorI _dependencyCalculator,
java.lang.Class[] _dependentClasses) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
CacheFetcherI _fetcher,
DependencyCalculatorI _dependencyCalculator,
java.lang.Class[] _dependentClasses,
CacheKeyCalculatorI _cacheKeyCalculator) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
CacheFetcherI _fetcher,
DependencyCalculatorI _dependencyCalculator,
java.lang.Class[] _dependentClasses,
CacheKeyCalculatorI _cacheKeyCalculator,
java.util.concurrent.ConcurrentMap map) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query)
Initializes a new instance of the BOCache class with a query for fetching missing BOs
and optional dependency calculation.
|
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query,
CacheKeyCalculatorI _cacheKeyCalculator)
Initializes a new instance of the BOCache class with a query for fetching missing BOs
and a cache key calculator.
|
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query,
CacheKeyCalculatorI _cacheKeyCalculator,
java.util.concurrent.ConcurrentMap map) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query,
DependencyCalculatorI _dependencyCalculator) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query,
DependencyCalculatorI _dependencyCalculator,
java.lang.Class[] _dependentClasses) |
BOCache(java.lang.String _title,
java.lang.Class<? extends BOI> _clazz,
BOLoaderI _cacheLoader,
java.lang.String _query,
DependencyCalculatorI _dependencyCalculator,
java.lang.Class[] _dependentClasses,
java.util.concurrent.ConcurrentMap map) |
Modifier and Type | Method and Description |
---|---|
long[] |
calculateDependencies(java.lang.Object key,
java.lang.Object value)
This method calculates for a given object the ids of objects, which can invalidate
the cached state.
|
void |
clear() |
java.lang.Object |
computeIfAbsent(java.lang.Object key,
java.util.function.Function computeFunction) |
boolean |
contains(java.lang.Object key) |
java.lang.Object |
fetch(java.lang.Object key)
Fetches an object corresponding to the given key from the cache or the database
using the cache loader.
|
java.lang.Object |
fetch(java.lang.Object key,
boolean silent) |
java.lang.Object[] |
fetchAll(java.lang.Object[] keys)
Returns an array of objects fetched from the cache, given an array of keys.
|
static void |
flushCaches() |
BOI |
get(java.lang.Object key) |
BOI[] |
get(java.lang.Object[] keys) |
BOI[] |
get(java.lang.Object[] keys,
boolean dontCacheNulls) |
BOI[] |
get(java.lang.Object[] keys,
boolean dontCacheNulls,
boolean dontFetch) |
BOI |
get(java.lang.Object key,
boolean dontCacheNulls) |
BOI |
get(java.lang.Object key,
boolean dontCacheNulls,
boolean dontFetch) |
static BO |
getCachedInstance(java.lang.Class<? extends BOI> c,
java.lang.String keyAttrName,
java.lang.Object key,
java.lang.String q,
BOLoaderI makeLoadedBy)
Retrieves a Business Object (BO) instance from the cache or the database, based on the given parameters.
|
static BO |
getCachedInstance(java.lang.Class<? extends BOI> c,
java.lang.String keyAttrName,
java.lang.Object key,
java.lang.String q,
BOLoaderI makeLoadedBy,
boolean dontCacheNulls) |
static BO |
getCachedInstance(java.lang.Class<? extends BOI> c,
java.lang.String keyAttrName,
java.lang.Object key,
java.lang.String q,
BOLoaderI makeLoadedBy,
boolean dontCacheNulls,
java.lang.String cacheName) |
static BO |
getCachedInstance(java.lang.Class<? extends BOI> c,
java.lang.String keyAttrName,
java.lang.Object key,
java.lang.String q,
BOLoaderI makeLoadedBy,
boolean dontCacheNulls,
java.lang.String cacheName,
BOLoaderI fallbackCacheLoader) |
BOLoaderI |
getCacheLoader()
Returns the `BOLoaderI` object used for loading objects into this cache
Caution: Use with care, intended for specific scenarios where a different `CacheFetcherI` might need it.
|
java.lang.Class |
getExpectedKeyClass() |
static void |
initStaticCacheLoader(BOLoaderI bol) |
void |
processDBManEvent(DBManEvent e)
Invoked when a subscribed DBManEvent occurs.
|
void |
put(java.lang.Object key,
java.lang.Object value) |
void |
remove(java.lang.Object key) |
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader)
_title
- The title or name of the cache._clazz
- The `Class` object of the BO that the cache is designed to handle_cacheLoader
- The cache loader to be used for loading BOs into the cachepublic BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, CacheFetcherI _fetcher)
_title
- The title or name of the cache._clazz
- The `Class` object of the BO that the cache is designed to handle_cacheLoader
- The cache loader to be used for loading BOs into the cache_fetcher
- The cache fetcher providing a callback to fetch missing values_dependencyCalculator
- The dependency calculator to extend the change listener to additional BOs
a cached value depends on and invalidate the cached state depending on the
calculated dependencies between BOs_dependentClasses
- An array of classes dependent on the BO class, used by the dependency calculator_cacheKeyCalculator
- The cache key calculator to be used for calculating cache keyspublic BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, CacheFetcherI _fetcher, DependencyCalculatorI _dependencyCalculator)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, CacheFetcherI _fetcher, DependencyCalculatorI _dependencyCalculator, java.lang.Class[] _dependentClasses)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, CacheFetcherI _fetcher, DependencyCalculatorI _dependencyCalculator, java.lang.Class[] _dependentClasses, CacheKeyCalculatorI _cacheKeyCalculator)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, CacheFetcherI _fetcher, DependencyCalculatorI _dependencyCalculator, java.lang.Class[] _dependentClasses, CacheKeyCalculatorI _cacheKeyCalculator, java.util.concurrent.ConcurrentMap map)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query)
_title
- The title or name of the cache_clazz
- The `Class` object of the BO that the cache is designed to handle_cacheLoader
- The cache loader to be used for loading BOs into the cache_query
- The query to be used for fetching missing BOs_dependencyCalculator
- The dependency calculator to extend the change listener to additional BOs
a cached value depends on and invalidate the cached state depending on the
calculated dependencies between BOs_dependentClasses
- An array of classes dependent on the BO class, used by the dependency calculatorpublic BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query, DependencyCalculatorI _dependencyCalculator)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query, DependencyCalculatorI _dependencyCalculator, java.lang.Class[] _dependentClasses)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query, DependencyCalculatorI _dependencyCalculator, java.lang.Class[] _dependentClasses, java.util.concurrent.ConcurrentMap map)
public BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query, CacheKeyCalculatorI _cacheKeyCalculator)
_title
- The title or name of the cache._clazz
- The `Class` object of the BO that the cache is designed to handle_cacheLoader
- The cache loader to be used for loading BOs into the cache_query
- The query to be used for fetching missing BOs_cacheKeyCalculator
- The cache key calculator to be used for calculating cache keyspublic BOCache(java.lang.String _title, java.lang.Class<? extends BOI> _clazz, BOLoaderI _cacheLoader, java.lang.String _query, CacheKeyCalculatorI _cacheKeyCalculator, java.util.concurrent.ConcurrentMap map)
public static void initStaticCacheLoader(BOLoaderI bol)
public static void flushCaches()
public static BO getCachedInstance(java.lang.Class<? extends BOI> c, java.lang.String keyAttrName, java.lang.Object key, java.lang.String q, BOLoaderI makeLoadedBy)
This method efficiently fetches a BO instance of the specified class matching the provided "primary key". It first attempts to retrieve the BO from the cache using the specified `cacheName`. If not found in the cache, it falls back to loading the BO from the database using the given query (`q`) and the `cacheLoader` or `fallbackCacheLoader`. The returned BO instance can optionally be associated with a different `BOLoaderI` using `makeLoadedBy`.
c
- The `Class` object of the BO to be loaded.keyAttrName
- The name of the "primary key" attribute used to identify the BO in the cache
and for cache invalidation.key
- The value of the "primary key" attribute that uniquely identifies the BO instance.q
- The query used to fetch the BO instance from the database if not found in the cache.makeLoadedBy
- (Optional) The `BOLoaderI` to associate with the returned BO instance, overriding
the cache loader.dontCacheNulls
- If `true`, null values will not be cached.cacheName
- (Optional) A custom name for the cache to use. If `null`, a default cache name will
be derived from the class' simple name and the `keyAttrName`.fallbackCacheLoader
- (Optional) A fallback `BOLoaderI` used when no static cache loader is available.
Primarily for special cases like update scripts.java.lang.IllegalStateException
- If no suitable cache loader is found to retrieve the BO.public static BO getCachedInstance(java.lang.Class<? extends BOI> c, java.lang.String keyAttrName, java.lang.Object key, java.lang.String q, BOLoaderI makeLoadedBy, boolean dontCacheNulls)
public static BO getCachedInstance(java.lang.Class<? extends BOI> c, java.lang.String keyAttrName, java.lang.Object key, java.lang.String q, BOLoaderI makeLoadedBy, boolean dontCacheNulls, java.lang.String cacheName)
public static BO getCachedInstance(java.lang.Class<? extends BOI> c, java.lang.String keyAttrName, java.lang.Object key, java.lang.String q, BOLoaderI makeLoadedBy, boolean dontCacheNulls, java.lang.String cacheName, BOLoaderI fallbackCacheLoader)
public void processDBManEvent(DBManEvent e)
DBManEventListenerI
DBManBOChangeEvent
for events triggered after modification on BOs.processDBManEvent
in interface DBManEventListenerI
public BOLoaderI getCacheLoader()
Caution: Use with care, intended for specific scenarios where a different `CacheFetcherI` might need it.
public long[] calculateDependencies(java.lang.Object key, java.lang.Object value)
DependencyCalculatorI
calculateDependencies
in interface DependencyCalculatorI
key
- The cache key of the object, does not have to be a Long, FIXME is it nullable or nonnull?value
- The currently cached value which might depend on other objects. Nullablepublic java.lang.Class getExpectedKeyClass()
getExpectedKeyClass
in interface CacheFetcherI
public java.lang.Object fetch(java.lang.Object key)
This method is synchronized to prevent overloading the server with too many initial cache-filling queries. A different `CacheFetcherI` may be used if one was passed in the constructor.
fetch
in interface CacheFetcherI
key
- The key corresponding to the object to be fetchedsilent
- A flag indicating whether to suppress any error messagesjava.lang.IllegalArgumentException
- if more than one object for the given key existspublic java.lang.Object fetch(java.lang.Object key, boolean silent)
public java.lang.Object[] fetchAll(java.lang.Object[] keys)
Note: This method only works if no query is given, and the keys are interpreted as Ids.
fetchAll
in interface CacheFetcherI
keys
- an array of keys of the objects to fetchpublic BOI get(java.lang.Object key, boolean dontCacheNulls)
public BOI get(java.lang.Object key, boolean dontCacheNulls, boolean dontFetch)
public BOI[] get(java.lang.Object[] keys, boolean dontCacheNulls)
public BOI[] get(java.lang.Object[] keys, boolean dontCacheNulls, boolean dontFetch)
public void put(java.lang.Object key, java.lang.Object value)
public java.lang.Object computeIfAbsent(java.lang.Object key, java.util.function.Function computeFunction)
computeIfAbsent
in interface CacheI
Copyright © 2000-2025 OAshi S.à r.l. All Rights Reserved.