public class Cache2kBuilder<K,V> extends Object
Cache
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 by CacheManager.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>
, use forUnknownTypes()
.
Modifier | Constructor and Description |
---|---|
protected |
Cache2kBuilder()
Constructor to override for the usage pattern:
|
Modifier and Type | Method and Description |
---|---|
Cache2kBuilder<K,V> |
addAsyncListener(CacheEntryOperationListener<K,V> listener)
A set of listeners.
|
Cache2kBuilder<K,V> |
addListener(CacheEntryOperationListener<K,V> listener)
Add a listener.
|
Cache<K,V> |
build()
Builds a cache with the specified configuration parameters.
|
Cache2kBuilder<K,V> |
disableStatistics(boolean flag)
By default statistic gathering is enabled.
|
Cache2kBuilder<K,V> |
entryCapacity(long v)
The maximum number of entries hold by the cache.
|
Cache2kBuilder<K,V> |
eternal(boolean v)
When set to true, cached values do not expire by time.
|
Cache2kBuilder<K,V> |
evictionSegmentCount(int v)
Number of eviction segments.
|
Cache2kBuilder<K,V> |
exceptionPropagator(ExceptionPropagator<K> ep) |
Cache2kBuilder<K,V> |
expireAfterWrite(long v,
TimeUnit u)
Time duration after insert or updated an cache entry expires.
|
Cache2kBuilder<K,V> |
expiryPolicy(ExpiryPolicy<K,V> c)
Set expiry policy to use.
|
static Cache2kBuilder |
forUnknownTypes()
|
Cache2kBuilder<K,V> |
keepDataAfterExpired(boolean v)
Expired data is kept in the cache until the entry is evicted.
|
<K2> Cache2kBuilder<K2,V> |
keyType(CacheType<K2> t)
The used type of the cache key.
|
<K2> Cache2kBuilder<K2,V> |
keyType(Class<K2> t)
The used type of the cache key.
|
Cache2kBuilder<K,V> |
loader(AdvancedCacheLoader<K,V> l) |
Cache2kBuilder<K,V> |
loader(CacheLoader<K,V> l) |
Cache2kBuilder<K,V> |
loaderExecutor(Executor v)
Thread pool / executor service to use for asynchronous load operations.
|
Cache2kBuilder<K,V> |
loaderThreadCount(int v)
If no separate executor is set via
loaderExecutor(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> |
maxRetryInterval(long v,
TimeUnit u)
If a loader exception happens, this is the maximum time interval after a
retry attempt is made.
|
Cache2kBuilder<K,V> |
name(Class<?> _class)
Sets a cache name from the fully qualified class name.
|
Cache2kBuilder<K,V> |
name(Class<?> _class,
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.
|
Cache2kBuilder<K,V> |
name(String _uniqueName,
Class<?> _class,
String _fieldName)
Constructs a cache name out of the class name, a field name and a unique name identifying the
component in the application.
|
static <K,T> Cache2kBuilder<K,T> |
of(Cache2kConfiguration<K,T> c)
Create a builder from the configuration.
|
static <K,T> Cache2kBuilder<K,T> |
of(Class<K> _keyType,
Class<T> _valueType)
Create a new cache builder for key and value types are classes with no generic parameters.
|
Cache2kBuilder<K,V> |
permitNullValues(boolean flag)
When
true , null values are allowed in the cache. |
Cache2kBuilder<K,V> |
prefetchExecutor(Executor v)
Thread pool / executor service to use for refresh ahead and prefetch operations.
|
Cache2kBuilder<K,V> |
refreshAhead(boolean f)
When true, enable background refresh / refresh ahead.
|
Cache2kBuilder<K,V> |
resilienceDuration(long v,
TimeUnit u)
Time span the cache will suppress loader exceptions if a value is available from
a previous load.
|
Cache2kBuilder<K,V> |
resiliencePolicy(ResiliencePolicy<K,V> v)
Sets a custom resilience policy to control the cache behavior in the presence
of exceptions from the loader.
|
Cache2kBuilder<K,V> |
retryInterval(long v,
TimeUnit u)
If a loader exception happens, this is the time interval after a
retry attempt is made.
|
Cache2kBuilder<K,V> |
sharpExpiry(boolean f)
By default the expiry time is not exact, which means, a value might be visible a few
milliseconds after the 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> |
suppressExceptions(boolean v)
If an exceptions gets thrown by the cache loader, suppress it if there is
a previous value.
|
Cache2kConfiguration<K,V> |
toConfiguration()
Returns the configuration object this builder operates on.
|
<T2> Cache2kBuilder<K,T2> |
valueType(CacheType<T2> t)
Sets the value type to use.
|
<T2> Cache2kBuilder<K,T2> |
valueType(Class<T2> t)
Sets the value type to use.
|
Cache2kBuilder<K,V> |
with(ConfigurationSectionBuilder<? extends ConfigurationSection>... sectionBuilders)
Add a new configuration sub section.
|
Cache2kBuilder<K,V> |
writer(CacheWriter<K,V> w) |
protected Cache2kBuilder()
Cache<Long, List<String>> c =
new Cache2kBuilder<Long, List<String>>() {}
.name("myCache")
.eternal(true)
.build();
The builder extracts the generic type parameters from the anonymous subclass.public static Cache2kBuilder forUnknownTypes()
public static <K,T> Cache2kBuilder<K,T> of(Class<K> _keyType, Class<T> _valueType)
keyType(Class)
,
valueType(Class)
public static <K,T> Cache2kBuilder<K,T> of(Cache2kConfiguration<K,T> c)
public final Cache2kBuilder<K,V> manager(CacheManager manager)
manager
- The manager the created cache should belong to,
or null
if the default cache manager should be usedIllegalStateException
- if the manager is not provided immediately after the builder is created.public final <K2> Cache2kBuilder<K2,V> keyType(Class<K2> t)
equals
and hashCode
method. Arrays are not valid for cache keys.IllegalArgumentException
- in case the type is illegalfor a general discussion on types
public final <T2> Cache2kBuilder<K,T2> valueType(Class<T2> t)
IllegalArgumentException
- in case the type is illegalfor a general discussion on types
public final <K2> Cache2kBuilder<K2,V> keyType(CacheType<K2> t)
equals
and hashCode
method. Arrays are not valid for cache keys.IllegalArgumentException
- in case the type is illegalfor a general discussion on types
public final <T2> Cache2kBuilder<K,T2> valueType(CacheType<T2> t)
IllegalArgumentException
- in case the type is illegalfor a general discussion on types
public final Cache2kBuilder<K,V> name(String _uniqueName, Class<?> _class, String _fieldName)
webImagePool~com.example.ImagePool.id2Image
See name(String)
for a general discussion about cache names.
_uniqueName
- unique name differentiating multiple components of the same type.
May be null
.name(String)
public final Cache2kBuilder<K,V> name(Class<?> _class, String _fieldName)
com.example.ImagePool.id2Image
See name(String)
for a general discussion about cache names.
name(String)
public final Cache2kBuilder<K,V> name(Class<?> _class)
See name(String)
for a general discussion about cache names.
name(String)
public final Cache2kBuilder<K,V> name(String v)
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 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.
In case of a name collision the cache is generating a unique name by adding a counter value. This behavior may change.
Allowed characters for a cache name, are URL non-reserved characters,
these are: [A-Z]
, [a-z]
, [0-9]
and [~-_.]
,
see RFC3986 as well as the characters: [()]
. The characters [@, ]
are supported
as well, but should be avoided.
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 cache might not enforce the allowed character set for efficiency reasons.
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.
Cache.getName()
public final Cache2kBuilder<K,V> keepDataAfterExpired(boolean v)
AdvancedCacheLoader
public final Cache2kBuilder<K,V> entryCapacity(long v)
The value Long.MAX_VALUE
means the capacity is not limited.
The default value is: 2000. The default value is conservative, so the application will usually run stable without tuning or setting a reasonable size.
public final Cache2kBuilder<K,V> eternal(boolean v)
Setting eternal to false signals that the data should 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.
The default behavior of the cache is identical to the setting eternal, meaning no expiry.
Exceptions: If set to eternal with default setting and if there is no
explicit expiry configured for exceptions with retryInterval(long, TimeUnit)
,
exceptions will not be cached and expire immediately.
IllegalArgumentException
- in case a previous setting is resetpublic final Cache2kBuilder<K,V> suppressExceptions(boolean v)
retryInterval(long, TimeUnit)
.
Setting this to false, will disable suppression or caching (aka resilience). Default value: true
Additional information can be found under resilience in the documentation.
public final Cache2kBuilder<K,V> expireAfterWrite(long v, TimeUnit u)
eternal(boolean)
.
If an ExpiryPolicy
is specified, the maximum expiry duration
will not exceed the value that is specified here.
A value of 0
means every entry should expire immediately. Low values or
0
together with read through operation mode with a CacheLoader
should be
avoided in production environments.
IllegalArgumentException
- if eternal(boolean)
was set to truepublic final Cache2kBuilder<K,V> exceptionPropagator(ExceptionPropagator<K> ep)
public final Cache2kBuilder<K,V> loader(CacheLoader<K,V> l)
public final Cache2kBuilder<K,V> loader(AdvancedCacheLoader<K,V> l)
public final Cache2kBuilder<K,V> writer(CacheWriter<K,V> w)
public final Cache2kBuilder<K,V> addListener(CacheEntryOperationListener<K,V> listener)
listener
- The listener to addIllegalArgumentException
- if an identical listener is already added.public final Cache2kBuilder<K,V> addAsyncListener(CacheEntryOperationListener<K,V> listener)
listener
- The listener to addIllegalArgumentException
- if an identical listener is already added.public final Cache2kBuilder<K,V> expiryPolicy(ExpiryPolicy<K,V> c)
If this is specified the maximum expiry time is still limited to the value in
expireAfterWrite(long, java.util.concurrent.TimeUnit)
. If expireAfterWrite(long, java.util.concurrent.TimeUnit)
is set to 0 then expiry calculation is not used, all entries expire immediately.
If no maximum expiry is specified via expireAfterWrite(long, java.util.concurrent.TimeUnit)
at leas the
resilienceDuration(long, java.util.concurrent.TimeUnit)
needs to be specified, if resilience should be enabled.
public final Cache2kBuilder<K,V> refreshAhead(boolean f)
get()
request will trigger the load.
Once refreshed, the entry is in a trail 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 trail period is determined by the configured expiry time
or the the ExpiryPolicy
. In case an entry is not accessed any more it needs to
reach the expiry time twice before removed from the cache.
The number of threads used to do the refresh are configured via
loaderThreadCount(int)
CacheLoader
,
loaderThreadCount(int)
public final Cache2kBuilder<K,V> sharpExpiry(boolean f)
ExpiryPolicy
returned.public final Cache2kBuilder<K,V> loaderThreadCount(int v)
loaderExecutor(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 the CacheLoader
. The default is one thread
per available CPU.
If a separate executor is defined the parameter has no effect.
public final Cache2kBuilder<K,V> storeByReference(boolean v)
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.
public final Cache2kBuilder<K,V> retryInterval(long v, TimeUnit u)
maxRetryInterval(long, java.util.concurrent.TimeUnit)
.public final Cache2kBuilder<K,V> maxRetryInterval(long v, TimeUnit u)
By default identical to resilienceDuration(long, java.util.concurrent.TimeUnit)
public final Cache2kBuilder<K,V> resilienceDuration(long v, TimeUnit u)
suppressExceptions(boolean)
is switched off, this setting
has no effect.
Defaults to expireAfterWrite(long, java.util.concurrent.TimeUnit)
. If suppressExceptions(boolean)
is switched off, this setting has no effect.
public final Cache2kBuilder<K,V> resiliencePolicy(ResiliencePolicy<K,V> v)
expireAfterWrite(long, java.util.concurrent.TimeUnit)
is set to 0.public final Cache2kBuilder<K,V> with(ConfigurationSectionBuilder<? extends ConfigurationSection>... sectionBuilders)
ConfigurationWithSections
public final Cache2kBuilder<K,V> strictEviction(boolean flag)
public final Cache2kBuilder<K,V> permitNullValues(boolean flag)
true
, null
values are allowed in the cache. In the default configuration
null
values are prohibited.
See the chapter in the user guide for details on null
values.
public final Cache2kBuilder<K,V> disableStatistics(boolean flag)
public final Cache2kBuilder<K,V> evictionSegmentCount(int v)
public final Cache2kBuilder<K,V> loaderExecutor(Executor v)
loaderThreadCount(int)
,
prefetchExecutor(Executor)
public final Cache2kBuilder<K,V> prefetchExecutor(Executor v)
loaderExecutor(Executor)
or a cache local pool is created.loaderThreadCount(int)
,
loaderExecutor(Executor)
public final Cache2kConfiguration<K,V> toConfiguration()
build()
is called. The method does not
return the effective configuration if additional external/XML configuration is present, since
this is applied when build
is called. On the other hand, the method can be used
to inspect the effective configuration after build
completed.public final Cache<K,V> build()
IllegalArgumentException
- if a cache of the same name is already active in the cache managercache2k API documentation. Copyright © 2000–2016 headissue GmbH, Munich.