Class Cache2kBuilder<K,V>
- java.lang.Object
-
- org.cache2k.Cache2kBuilder<K,V>
-
- All Implemented Interfaces:
ConfigBuilder<Cache2kBuilder<K,V>,Cache2kConfig<K,V>>
,DataAware<K,V>
public class Cache2kBuilder<K,V> extends Object implements ConfigBuilder<Cache2kBuilder<K,V>,Cache2kConfig<K,V>>, DataAware<K,V>
Builder to create aCache
instance. The usage is:Cache<Long, List<String>> c = new Cache2kBuilder<Long, List<String>>() {} .name("myCache") .eternal(true) .build();
Caches belong to a cache manager. If no cache manager is set explicitly via
manager
the default cache manager will be used, as defined byCacheManager.getInstance()
.To create a cache from a known configuration in a specified cache manager, use:
CacheManager manager = ... CacheConfiguration<Long, List<String>> config = ... Cache<Long, List<String>> c = Cache2kBuilder.of(config) .manager(manager) .build();
To create a cache without type parameters or
Cache<Object,Object>
, useforUnknownTypes()
.This builder can also be used to alter and build a configuration object that can be retrieved with
config()
- Since:
- 1.0
- Author:
- Jens Wilke
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Cache2kBuilder()
Constructor to override for the usage pattern:
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Cache2kBuilder<K,V>
addAsyncListener(CacheEntryOperationListener<K,V> listener)
A set of listeners.Cache2kBuilder<K,V>
addCacheClosedListener(CacheClosedListener listener)
Listener that is called after a cache is closed.Cache2kBuilder<K,V>
addListener(CacheEntryOperationListener<K,V> listener)
Add a listener.Cache2kBuilder<K,V>
asyncListenerExecutor(Executor v)
Executor for asynchronous listeners.Cache2kBuilder<K,V>
boostConcurrency(boolean f)
Whentrue
, optimize for high core counts and applications that do lots of mutations in the cache.Cache<K,V>
build()
Builds a cache with the specified configuration parameters.Cache2kBuilder<K,V>
bulkLoader(AsyncBulkCacheLoader<K,V> l)
Cache2kBuilder<K,V>
bulkLoader(BulkCacheLoader<K,V> l)
Cache2kConfig<K,V>
config()
Returns the configuration object this builder operates on.Cache2kBuilder<K,V>
disable(Class<? extends ToggleFeature> feature)
Cache2kBuilder<K,V>
disableMonitoring(boolean f)
Disables reporting of cache metrics to monitoring systems or management.Cache2kBuilder<K,V>
disableStatistics(boolean flag)
By default, statistic gathering is enabled.Cache2kBuilder<K,V>
enable(Class<? extends ToggleFeature> feature)
Enable a feature<B extends ConfigBuilder<B,T>,T extends ToggleFeature & ConfigBean<T,B>>
Cache2kBuilder<K,V>enable(Class<T> featureType, Consumer<B> builderAction)
Enables a toggle feature which has additional parameters and configures it via its builder.<B extends SectionBuilder<B,CFG>,T extends ToggleFeature & WithSection<CFG,B>,CFG extends ConfigSection<CFG,B>>
Cache2kBuilder<K,V>enableWith(Class<T> featureType, Consumer<B> builderAction)
Enables a feature and configures its associated configuration section via its builder.Cache2kBuilder<K,V>
entryCapacity(long v)
The maximum number of entries hold by the cache.Cache2kBuilder<K,V>
eternal(boolean v)
When set totrue
, cached values do not expire and do not need refreshing.Cache2kBuilder<K,V>
exceptionPropagator(ExceptionPropagator<? super K,? super V> ep)
Sets customization for propagating loader exceptions.Cache2kBuilder<K,V>
executor(Executor v)
Executor for asynchronous operations and expiry.Cache2kBuilder<K,V>
expireAfterWrite(long v, TimeUnit u)
Cache2kBuilder<K,V>
expireAfterWrite(Duration v)
Duration after an entry expires after an insert or update.Cache2kBuilder<K,V>
expiryPolicy(ExpiryPolicy<? super K,? super V> c)
Set expiry policy to use.static Cache2kBuilder<Object,Object>
forUnknownTypes()
CacheManager
getManager()
Get the associated cache manager.Cache2kBuilder<K,V>
idleScanTime(long v, TimeUnit u)
Cache2kBuilder<K,V>
idleScanTime(Duration v)
Sets the time for a regular scan of all cache entries which evicts idle entries that are not accessed since the last scan.Cache2kBuilder<K,V>
keepDataAfterExpired(boolean v)
Expired data is kept in the cache until the entry is evicted.<K2> Cache2kBuilder<K2,V>
keyType(Class<K2> t)
The used type of the cache key.<K2> Cache2kBuilder<K2,V>
keyType(CacheType<K2> t)
The used type of the cache key.Cache2kBuilder<K,V>
loader(AdvancedCacheLoader<K,V> l)
Enables read through operation and sets a cache loader.Cache2kBuilder<K,V>
loader(AsyncCacheLoader<K,V> l)
Enables read through operation and sets a cache loader.Cache2kBuilder<K,V>
loader(CacheLoader<K,V> l)
Enables read through operation and sets a cache loader.Cache2kBuilder<K,V>
loaderExecutor(Executor v)
Thread pool / executor service to use for triggered load operations.Cache2kBuilder<K,V>
loaderThreadCount(int v)
If no separate executor is set vialoaderExecutor(Executor)
the cache will create a separate thread pool used exclusively by it.Cache2kBuilder<K,V>
manager(CacheManager manager)
The manager, the created cache will belong to.Cache2kBuilder<K,V>
maximumWeight(long v)
Specifies the maximum weight of entries the cache may contain.Cache2kBuilder<K,V>
name(@Nullable String uniqueName, Class<?> clazz, String fieldName)
Constructs a cache name out of the class name, a field name and a unique name identifying the component in the application.Cache2kBuilder<K,V>
name(Class<?> clazz)
Sets a cache name from the fully qualified class name.Cache2kBuilder<K,V>
name(Class<?> clazz, String fieldName)
Constructs a cache name out of the class name and field name.Cache2kBuilder<K,V>
name(String v)
Sets the name of a cache.static <K,V>
Cache2kBuilder<K,V>of(Class<K> keyType, Class<V> valueType)
Create a new cache builder for key and value types of classes with no generic parameters.static <K,V>
Cache2kBuilder<K,V>of(Cache2kConfig<K,V> c)
Create a builder from the configuration bean.Cache2kBuilder<K,V>
permitNullValues(boolean flag)
Whentrue
,null
values are allowed in the cache.Cache2kBuilder<K,V>
recordModificationTime(boolean flag)
Enables that time of an update (e.g.Cache2kBuilder<K,V>
refreshAhead(boolean f)
Whentrue
, enable background refresh / refresh ahead.Cache2kBuilder<K,V>
refreshExecutor(Executor v)
Thread pool / executor service to use for refresh ahead operations.Cache2kBuilder<K,V>
resiliencePolicy(CustomizationSupplier<? extends ResiliencePolicy<? super K,? super V>> v)
Cache2kBuilder<K,V>
resiliencePolicy(ResiliencePolicy<? super K,? super V> v)
Sets a custom resilience policy to control the cache behavior in the presence of exceptions from the loader.Cache2kBuilder<K,V>
scheduler(Scheduler v)
Use a different scheduler to run timer tasks for.Cache2kBuilder<K,V>
set(Consumer<Cache2kConfig<K,V>> configAction)
Execute on the underlying configuration object.Cache2kBuilder<K,V>
setup(Consumer<Cache2kBuilder<K,V>> consumer)
Call the consumer with this builder.<B extends ConfigBuilder<B,CFG>,CFG extends ConfigBean<CFG,B>>
Cache2kBuilder<K,V>setup(Function<Cache2kBuilder<K,V>,CFG> enabler, Consumer<B> builderAction)
Executes setup code for a feature or customization which has additional parameters and configures it via its builder.<B extends SectionBuilder<B,CFG>,CFG extends ConfigSection<CFG,B>,SUP extends WithSection<CFG,B> & CustomizationSupplier<?>>
Cache2kBuilder<K,V>setupWith(Function<Cache2kBuilder<K,V>,SUP> setupAction, Consumer<B> builderAction)
Execute setup code for a feature or customization and configure its associated configuration section via its builder.Cache2kBuilder<K,V>
sharpExpiry(boolean f)
By default, the time of expiry is not exact, which means, a value might be visible for up to a second longer after the requested time of expiry.Cache2kBuilder<K,V>
storeByReference(boolean v)
Ensure that the cache value is stored via direct object reference and that no serialization takes place.Cache2kBuilder<K,V>
strictEviction(boolean flag)
To increase performance cache2k optimizes the eviction and does eviction in greater chunks.Cache2kBuilder<K,V>
timeReference(TimeReference v)
Use a different time reference.Cache2kBuilder<K,V>
timerLag(long v, TimeUnit u)
Cache2kBuilder<K,V>
timerLag(Duration v)
Change the maximum lag time for timer events.<V2> Cache2kBuilder<K,V2>
valueType(Class<V2> t)
Sets the value type to use.<V2> Cache2kBuilder<K,V2>
valueType(CacheType<V2> t)
Sets the value type to use.Cache2kBuilder<K,V>
weigher(Weigher<K,V> v)
Set the weigher to be used to calculate the entry weight.<B extends SectionBuilder<B,CFG>,CFG extends ConfigSection<CFG,B>>
Cache2kBuilder<K,V>with(Class<CFG> configSectionClass, Consumer<B> builderAction)
Configure a config section.Cache2kBuilder<K,V>
writer(CacheWriter<K,V> w)
Enables write through operation and sets a writer customization that gets called synchronously upon cache mutations.
-
-
-
Constructor Detail
-
Cache2kBuilder
protected Cache2kBuilder()
Constructor to override for the usage pattern:
The builder extracts the generic type parameters from the anonymous subclass.Cache<Long, List<String>> c = new Cache2kBuilder<Long, List<String>>() {} .name("myCache") .eternal(true) .build();
-
-
Method Detail
-
forUnknownTypes
public static Cache2kBuilder<Object,Object> forUnknownTypes()
-
of
public static <K,V> Cache2kBuilder<K,V> of(Class<K> keyType, Class<V> valueType)
Create a new cache builder for key and value types of classes with no generic parameters.- See Also:
keyType(Class)
,valueType(Class)
-
of
public static <K,V> Cache2kBuilder<K,V> of(Cache2kConfig<K,V> c)
Create a builder from the configuration bean. The builder is not assigned to a cache manager and any default configurations, if present, will not be applied.
-
manager
public final Cache2kBuilder<K,V> manager(CacheManager manager)
The manager, the created cache will belong to. If this is set, it must be the first method called.- Parameters:
manager
- The manager the created cache should belong to, ornull
if the default cache manager should be used- Throws:
IllegalStateException
- if the manager is not provided immediately after the builder is created.
-
keyType
public final <K2> Cache2kBuilder<K2,V> keyType(Class<K2> t)
The used type of the cache key. A suitable cache key must provide a usefulequals
andhashCode
method. Arrays are not valid for cache keys.- Throws:
IllegalArgumentException
- in case the type is illegal- See Also:
for a general discussion on types
-
valueType
public final <V2> Cache2kBuilder<K,V2> valueType(Class<V2> t)
Sets the value type to use. Arrays are not supported.- Throws:
IllegalArgumentException
- in case the type is illegal- See Also:
for a general discussion on types
-
keyType
public final <K2> Cache2kBuilder<K2,V> keyType(CacheType<K2> t)
The used type of the cache key. A suitable cache key must provide a usefulequals
andhashCode
method. Arrays are not valid for cache keys.- Throws:
IllegalArgumentException
- in case the type is illegal- See Also:
for a general discussion on types
-
valueType
public final <V2> Cache2kBuilder<K,V2> valueType(CacheType<V2> t)
Sets the value type to use. Arrays are not supported.- Throws:
IllegalArgumentException
- in case the type is illegal- See Also:
for a general discussion on types
-
name
public final Cache2kBuilder<K,V> name(@Nullable @Nullable String uniqueName, Class<?> clazz, String fieldName)
Constructs a cache name out of the class name, a field name and a unique name identifying the component in the application. Result example:webImagePool~com.example.ImagePool.id2Image
See
name(String)
for a general discussion about cache names.- Parameters:
uniqueName
- unique name differentiating multiple components of the same type. May benull
.- See Also:
name(String)
-
name
public final Cache2kBuilder<K,V> name(Class<?> clazz, String fieldName)
Constructs a cache name out of the class name and field name. Result example:com.example.ImagePool.id2Image
See
name(String)
for a general discussion about cache names.- See Also:
name(String)
-
name
public final Cache2kBuilder<K,V> name(Class<?> clazz)
Sets a cache name from the fully qualified class name.See
name(String)
for a general discussion about cache names.- See Also:
name(String)
-
name
public final Cache2kBuilder<K,V> name(String v)
Sets the name of a cache. If a name is specified it must be ensured it is unique within the cache manager. Cache names are used at several places to have a unique ID of a cache. For example, for referencing additional configuration or to register JMX beans.If a name is not specified the cache generates a name automatically. The name is inferred from the call stack trace and contains the simple class name, the method and the line number of the caller to
build()
. The name also contains a random number. Automatically generated names don't allow reliable management, logging and additional configuration of caches. If no name is set,build()
will always create a new cache with a new unique name within the cache manager. Automatically generated cache names start with the character'_'
as prefix to separate the names from the usual class name space.For maximum compatibility cache names should only be composed with the characters
[-_.a-zA-Z0-9]
. The characters{}|\^&=";:<>*?/
are not allowed in a cache name. The reason for restricting the characters in names, is that the names may be used to derive other resource names from it, e.g. for file based storage. The characters*
and?
are used for wildcards in JMX and cannot be used in an object name.The method is overloaded with variants to provide a naming convention of names.
For brevity within log messages and other displays the cache name may be shortened if the manager name is included as prefix.
- See Also:
Cache.getName()
-
keepDataAfterExpired
public final Cache2kBuilder<K,V> keepDataAfterExpired(boolean v)
Expired data is kept in the cache until the entry is evicted. This consumes memory, but if the data is accessed again the previous data can be used by the cache loader for optimizing (e.g. if-modified-since for an HTTP request). Default value:false
- See Also:
AdvancedCacheLoader
-
entryCapacity
public final Cache2kBuilder<K,V> entryCapacity(long v)
The maximum number of entries hold by the cache. When the maximum size is reached, by inserting new entries, the cache eviction algorithm will remove one or more entries to keep the size within the configured limit.The value
Long.MAX_VALUE
means the capacity is not limited.The default value is
Cache2kConfig.DEFAULT_ENTRY_CAPACITY
. The odd value is intentional, to serve as an indicator for SREs that the cache is running on its default size. A low capacity default is different to caches like Guava or Caffeine, that create an unbounded cache by default, which potentially is a memory leak.
-
eternal
public final Cache2kBuilder<K,V> eternal(boolean v)
When set totrue
, cached values do not expire and do not need refreshing. Entries will need to be removed from the cache explicitly, will be evicted if capacity constraints are reached, or can be evicted after a period of no usage viaidleScanTime(long, TimeUnit)
. It also means that no expiry functions are available andMutableCacheEntry.setExpiryTime(long)
` cannot be used. If no expiry is needed it is a good idea to enable eternal, which safes resources used for the timer data structures. Changing when data is considered eternal, will lead to and error.Setting eternal to
false
signals that the data might expire, but there is no predefined expiry value at programmatic level. This value needs to be set by other means, e.g. within a configuration file. This is the default setting.- Throws:
IllegalArgumentException
- in case a previous setting is reset
-
expireAfterWrite
public final Cache2kBuilder<K,V> expireAfterWrite(long v, TimeUnit u)
- See Also:
expireAfterWrite(Duration)
-
expireAfterWrite
public final Cache2kBuilder<K,V> expireAfterWrite(Duration v)
Duration after an entry expires after an insert or update.The expiry happens via a timer event and may lag approximately one second by default, see
timerLag(long, TimeUnit)
. For exact expiry specify anExpiryPolicy
and enablesharpExpiry(boolean)
.If an
ExpiryPolicy
is specified in combination to this value, the maximum expiry duration is capped to the value specified here.A value of
0
orExpiryTimeValues.NOW
means every entry should expire immediately. This can be useful to disable caching by configuration.To switch off time based expiry use
eternal(boolean)
.A value of
Long.MAX_VALUE
milliseconds or more (if another unit is used) is treated as eternal expiry.For removing cache entries based on the last access time and therefor shrinking the cache at times of low activity, the parameter
idleScanTime(long, TimeUnit)
can be used.- Throws:
IllegalArgumentException
- ifeternal(boolean)
was set to true- See Also:
- cache2k user guide - Expiry and Refresh
-
timerLag
public final Cache2kBuilder<K,V> timerLag(long v, TimeUnit u)
- See Also:
timerLag(Duration)
-
timerLag
public final Cache2kBuilder<K,V> timerLag(Duration v)
Change the maximum lag time for timer events. Timer events are used for expiry and refresh operations. The default is approximately one second.If an application needs exact, point in time expiry
sharpExpiry(boolean)
orExpiry.toSharpTime(long)
need to be used.
-
idleScanTime
public final Cache2kBuilder<K,V> idleScanTime(long v, TimeUnit u)
- See Also:
idleScanTime(Duration)
-
idleScanTime
public final Cache2kBuilder<K,V> idleScanTime(Duration v)
Sets the time for a regular scan of all cache entries which evicts idle entries that are not accessed since the last scan. The effect is similar then the setting time to idle or expire after access in other caches when set to about two thirds of the time value. The actual eviction of an idle entry will approximately happen between one or two times the configured idle scan time.Idle scanning works in combination with a cache size limit and expiry.
For entries removed by the idle scanner an eviction event is generated and counted as eviction in the cache statistics.
Rationale: Although a regular scan sounds like a lot of overhead it is the opposite, since there is no additional overhead of bookkeeping when an item is accessed. This is more efficient then a straight forward time to idle implementation via a linked list.
- Since:
- 2.6
- See Also:
- Github issue #39
-
exceptionPropagator
public final Cache2kBuilder<K,V> exceptionPropagator(ExceptionPropagator<? super K,? super V> ep)
Sets customization for propagating loader exceptions. By default, loader exceptions are wrapped into aCacheLoaderException
.
-
loader
public final Cache2kBuilder<K,V> loader(CacheLoader<K,V> l)
Enables read through operation and sets a cache loader. Different loader types are available:CacheLoader
,AdvancedCacheLoader
.- See Also:
for general discussion on cache loaders
-
loader
public final Cache2kBuilder<K,V> loader(AdvancedCacheLoader<K,V> l)
Enables read through operation and sets a cache loader. Different loader types are available:CacheLoader
,AdvancedCacheLoader
.- See Also:
for general discussion on cache loaders
-
loader
public final Cache2kBuilder<K,V> loader(AsyncCacheLoader<K,V> l)
Enables read through operation and sets a cache loader. Different loader types are available:CacheLoader
,AdvancedCacheLoader
,AsyncCacheLoader
- See Also:
for general discussion on cache loaders
-
bulkLoader
public final Cache2kBuilder<K,V> bulkLoader(AsyncBulkCacheLoader<K,V> l)
-
bulkLoader
public final Cache2kBuilder<K,V> bulkLoader(BulkCacheLoader<K,V> l)
-
writer
public final Cache2kBuilder<K,V> writer(CacheWriter<K,V> w)
Enables write through operation and sets a writer customization that gets called synchronously upon cache mutations. By default, write through is not enabled.
-
addCacheClosedListener
public final Cache2kBuilder<K,V> addCacheClosedListener(CacheClosedListener listener)
Listener that is called after a cache is closed. This is mainly used for the JCache integration.
-
addListener
public final Cache2kBuilder<K,V> addListener(CacheEntryOperationListener<K,V> listener)
Add a listener. The listeners will be executed in a synchronous mode, meaning, further processing for an entry will stall until a registered listener is executed. The expiry will always be executed asynchronously.- Parameters:
listener
- The listener to add- Throws:
IllegalArgumentException
- if an identical listener is already added.
-
addAsyncListener
public final Cache2kBuilder<K,V> addAsyncListener(CacheEntryOperationListener<K,V> listener)
A set of listeners. Listeners added in this collection will be executed in an asynchronous mode.- Parameters:
listener
- The listener to add- Throws:
IllegalArgumentException
- if an identical listener is already added.
-
expiryPolicy
public final Cache2kBuilder<K,V> expiryPolicy(ExpiryPolicy<? super K,? super V> c)
Set expiry policy to use.If this is specified the maximum expiry time is still limited to the value in
expireAfterWrite(long, java.util.concurrent.TimeUnit)
. IfexpireAfterWrite(long, java.util.concurrent.TimeUnit)
is set to 0 then expiry calculation is not used, all entries expire immediately.
-
refreshAhead
public final Cache2kBuilder<K,V> refreshAhead(boolean f)
Whentrue
, enable background refresh / refresh ahead. After the expiry time of a value is reached, the loader is invoked to fetch a fresh value. The old value will be returned by the cache, although it is expired, and will be replaced by the new value, once the loader is finished. In the case there are not enough loader threads available, the value will expire immediately and the nextget()
request will trigger the load.Once refreshed, the entry is in a probation period. If it is not accessed until the next expiry, no refresh will be done and the entry expires regularly. This means that the time an entry stays within the probation period is determined by the configured expiry time or the
ExpiryPolicy
. When an entry is not accessed for two times the expiry time, it gets removed from the cache.The number of threads used to do the refresh are configured via
loaderThreadCount(int)
By default, refresh ahead is not enabled.
- See Also:
CacheLoader
,loaderThreadCount(int)
-
sharpExpiry
public final Cache2kBuilder<K,V> sharpExpiry(boolean f)
By default, the time of expiry is not exact, which means, a value might be visible for up to a second longer after the requested time of expiry. The time lag depends on the system load and the parametertimerLag(long, TimeUnit)
Switching totrue
, means that values will not be visible when the entry is reached past its expiry time which set by theExpiryPolicy
. This has no effect onexpireAfterWrite(long, TimeUnit)
.Alternatively, sharp expiry can be requested on a per-entry basis in the
ExpiryPolicy
viaExpiry.toSharpTime(long)
-
loaderThreadCount
public final Cache2kBuilder<K,V> loaderThreadCount(int v)
If no separate executor is set vialoaderExecutor(Executor)
the cache will create a separate thread pool used exclusively by it. Defines the maximum number of threads this cache should use for calls to theCacheLoader
. The default is one thread per available CPU. If threads are exhausted the executor rejects the execution. If that happens the cache will carry out load requests in the calling thread or, in case of a refresh, drop the request.If a separate executor is defined the parameter has no effect.
-
storeByReference
public final Cache2kBuilder<K,V> storeByReference(boolean v)
Ensure that the cache value is stored via direct object reference and that no serialization takes place. Cache clients leveraging the fact that an in heap cache stores object references directly should set this value.If this value is not set to true this means: The key and value objects need to have a defined serialization mechanism and the cache may choose to transfer off the heap. For cache2k version 1.0 this value has no effect. It should be used by application developers to future-proof the applications with upcoming versions.
-
resiliencePolicy
public final Cache2kBuilder<K,V> resiliencePolicy(ResiliencePolicy<? super K,? super V> v)
Sets a custom resilience policy to control the cache behavior in the presence of exceptions from the loader. A specified policy will be ignored ifexpireAfterWrite(long, java.util.concurrent.TimeUnit)
is set to 0.
-
resiliencePolicy
public final Cache2kBuilder<K,V> resiliencePolicy(CustomizationSupplier<? extends ResiliencePolicy<? super K,? super V>> v)
-
strictEviction
public final Cache2kBuilder<K,V> strictEviction(boolean flag)
To increase performance cache2k optimizes the eviction and does eviction in greater chunks. With strict eviction, the eviction is done for one entry as soon as the capacity constraint is met. This is primarily used for testing and evaluation purposes.
-
permitNullValues
public final Cache2kBuilder<K,V> permitNullValues(boolean flag)
Whentrue
,null
values are allowed in the cache. In the default configurationnull
values are prohibited.See the chapter in the user guide for details on
null
values.When used within Spring,
null
are allowed by default.
-
disableStatistics
public final Cache2kBuilder<K,V> disableStatistics(boolean flag)
By default, statistic gathering is enabled. Switching this totrue
will disable all statistics that have significant overhead. Whether the values become visible in monitoring can be controlled viadisableMonitoring(boolean)
-
recordModificationTime
public final Cache2kBuilder<K,V> recordModificationTime(boolean flag)
Enables that time of an update (e.g. load or put) is available atMutableCacheEntry.getModificationTime()
. Default isfalse
.- See Also:
MutableCacheEntry.getModificationTime()
-
boostConcurrency
public final Cache2kBuilder<K,V> boostConcurrency(boolean f)
Whentrue
, optimize for high core counts and applications that do lots of mutations in the cache. When switched on, the cache will occupy slightly more memory and eviction efficiency may drop slightly. This overhead is negligible for big cache sizes (100K and more).Typical interactive do not need to enable this. May improve concurrency for applications that utilize all cores and cache operations account for most CPU cycles.
-
disableMonitoring
public final Cache2kBuilder<K,V> disableMonitoring(boolean f)
Disables reporting of cache metrics to monitoring systems or management. This should be set, e.g. if a cache is created dynamically and intended to be short-lived. All extensions for monitoring or management respect this parameter.
-
loaderExecutor
public final Cache2kBuilder<K,V> loaderExecutor(Executor v)
Thread pool / executor service to use for triggered load operations. If no executor is specified the cache will create a thread pool, if needed.The loader thread pool is used by the cache for executing load requests concurrent to the application, when a
Cache.loadAll(Iterable)
is issued. If no bulk loader is specified the thread pool will be used to carry out load requests in parallel. If anAsyncCacheLoader
is used, the cache itself does not use the loader executor.The executor should reject when not enough threads are available. In that case the cache will use the calling thread for the load operation.
- See Also:
loaderThreadCount(int)
-
refreshExecutor
public final Cache2kBuilder<K,V> refreshExecutor(Executor v)
Thread pool / executor service to use for refresh ahead operations. If not specified the same refresh ahead operation will use the thread pool defined byloaderExecutor(Executor)
or a cache local pool is created.The executor for refresh operations may reject execution when not enough resources are available. If a refresh is rejected, the cache entry expires normally.
- See Also:
loaderThreadCount(int)
,loaderExecutor(Executor)
-
executor
public final Cache2kBuilder<K,V> executor(Executor v)
Executor for asynchronous operations and expiry. TheForkJoinPool.commonPool()
is used by default.
-
scheduler
public final Cache2kBuilder<K,V> scheduler(Scheduler v)
Use a different scheduler to run timer tasks for.
-
timeReference
public final Cache2kBuilder<K,V> timeReference(TimeReference v)
Use a different time reference.
-
asyncListenerExecutor
public final Cache2kBuilder<K,V> asyncListenerExecutor(Executor v)
Executor for asynchronous listeners. If not configured the common asynchronous executor is used as defined byexecutor(Executor)
-
weigher
public final Cache2kBuilder<K,V> weigher(Weigher<K,V> v)
Set the weigher to be used to calculate the entry weight. The parametermaximumWeight(long)
needs to be specified as well. Using a weigher has a slight performance impact on the update of existing entries. When a weigher is set theentryCapacity(long)
parameter is ignored.
-
maximumWeight
public final Cache2kBuilder<K,V> maximumWeight(long v)
Specifies the maximum weight of entries the cache may contain. To obtain the entry weight aWeigher
must be specified viaweigher(org.cache2k.operation.Weigher<K, V>)
.The weight of an entry does not influence which entry is evicted next, but is only used to constrain the capacity of the cache. The cache implementation tries the best to keep the cache within its maximum weight limit, but eviction may kick in before or after reaching the limit.
The maximum weight setting cannot be used together with
entryCapacity(long)
.
-
setup
public final Cache2kBuilder<K,V> setup(Consumer<Cache2kBuilder<K,V>> consumer)
Call the consumer with this builder. This can be used to apply configuration fragments within the fluent configuration scheme.
-
enable
public final Cache2kBuilder<K,V> enable(Class<? extends ToggleFeature> feature)
Enable a feature
-
disable
public final Cache2kBuilder<K,V> disable(Class<? extends ToggleFeature> feature)
-
with
public final <B extends SectionBuilder<B,CFG>,CFG extends ConfigSection<CFG,B>> Cache2kBuilder<K,V> with(Class<CFG> configSectionClass, Consumer<B> builderAction)
Configure a config section. If the section is not existing it a new section is created. If the section is existing the existing instance is modified.- Type Parameters:
B
- type of the builder for the config sectionCFG
- the type of the config section- Parameters:
configSectionClass
- type of the config section that is created or alteredbuilderAction
- lambda that alters the config section via its builder
-
set
public Cache2kBuilder<K,V> set(Consumer<Cache2kConfig<K,V>> configAction)
Execute on the underlying configuration object. This can be used to set customization suppliers, likeCache2kConfig.setExecutor(CustomizationSupplier)
instead of instances.
-
setupWith
public <B extends SectionBuilder<B,CFG>,CFG extends ConfigSection<CFG,B>,SUP extends WithSection<CFG,B> & CustomizationSupplier<?>> Cache2kBuilder<K,V> setupWith(Function<Cache2kBuilder<K,V>,SUP> setupAction, Consumer<B> builderAction)
Execute setup code for a feature or customization and configure its associated configuration section via its builder.- Type Parameters:
B
- the builder for the customizations' configuration sectionCFG
- the configuration sectionSUP
- the supplier for the customization- Parameters:
setupAction
- function modifying the configurationbuilderAction
- function for configuring the customization
-
setup
public <B extends ConfigBuilder<B,CFG>,CFG extends ConfigBean<CFG,B>> Cache2kBuilder<K,V> setup(Function<Cache2kBuilder<K,V>,CFG> enabler, Consumer<B> builderAction)
Executes setup code for a feature or customization which has additional parameters and configures it via its builder.- Parameters:
enabler
- setup function returning am associated configurationbuilderAction
- function to configure
-
enable
public <B extends ConfigBuilder<B,T>,T extends ToggleFeature & ConfigBean<T,B>> Cache2kBuilder<K,V> enable(Class<T> featureType, Consumer<B> builderAction)
Enables a toggle feature which has additional parameters and configures it via its builder.- Parameters:
featureType
- Type of feature which has additional bean propertiesbuilderAction
- function to configure the feature
-
enableWith
public <B extends SectionBuilder<B,CFG>,T extends ToggleFeature & WithSection<CFG,B>,CFG extends ConfigSection<CFG,B>> Cache2kBuilder<K,V> enableWith(Class<T> featureType, Consumer<B> builderAction)
Enables a feature and configures its associated configuration section via its builder. Be aware that a section might be existing and preconfigured already, the semantics are identical towith(Class, Consumer)
-
config
public final Cache2kConfig<K,V> config()
Returns the configuration object this builder operates on. Changes to the configuration also will influence the created cache whenbuild()
is called. The method does not return the effective configuration if additional external/XML configuration is present, since this is applied whenbuild
is called. On the other hand, the method can be used to inspect the effective configuration afterbuild
completed.- Specified by:
config
in interfaceConfigBuilder<K,V>
- Returns:
- configuration objects with the parameters set in the builder.
-
getManager
public final CacheManager getManager()
Get the associated cache manager.
-
build
public final Cache<K,V> build()
Builds a cache with the specified configuration parameters.If XML configuration is present, the section for the cache name is looked up and the configuration in it applied, before the cache is build.
- Throws:
IllegalArgumentException
- if a cache of the same name is already active in the cache managerIllegalArgumentException
- if a configuration entry for the named cache is required but not present
-
-