org.cache2k.configuration.Cache2kConfiguration Maven / Gradle / Ivy
Show all versions of cache2k-api Show documentation
package org.cache2k.configuration;
/*
* #%L
* cache2k API
* %%
* Copyright (C) 2000 - 2020 headissue GmbH, Munich
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.cache2k.Cache2kBuilder;
import org.cache2k.TimeReference;
import org.cache2k.Weigher;
import org.cache2k.event.CacheClosedListener;
import org.cache2k.event.CacheEntryOperationListener;
import org.cache2k.expiry.Expiry;
import org.cache2k.expiry.ExpiryPolicy;
import org.cache2k.expiry.ExpiryTimeValues;
import org.cache2k.integration.AdvancedCacheLoader;
import org.cache2k.integration.AsyncCacheLoader;
import org.cache2k.integration.CacheWriter;
import org.cache2k.integration.ExceptionPropagator;
import org.cache2k.integration.FunctionalCacheLoader;
import org.cache2k.integration.ResiliencePolicy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
/**
* Configuration for a cache2k cache.
*
* To create a cache, the {@link Cache2kBuilder} is used. All configuration properties
* are present on the builder and are documented in this place. Consequently all properties
* refer to the corresponding builder method.
*
*
The configuration bean is designed to be serializable. This is used for example to copy
* default configurations. The builder allows object references to customizations to be set.
* If this happens the configuration is not serializable. Such configuration is only used for
* immediate creation of one cache via the builder.
*
*
The configuration may contain additional beans, called configuration sections, that are
* used to configure extensions or sub modules.
*
*
Within the XML configuration of a cache manager different default configuration
* values may be specified. To get a configuration bean with the effective defaults of
* a specific manager do {@code Cache2kBuilder.forUnknownTypes().manager(...).toConfiguration()}
*
* @author Jens Wilke
*/
@SuppressWarnings("unused")
public class Cache2kConfiguration implements ConfigurationBean, ConfigurationWithSections {
public static final long EXPIRY_NOT_ETERNAL = Long.MAX_VALUE - 1;
public static final long UNSET_LONG = -1;
private boolean storeByReference;
private String name;
private CacheType keyType;
private CacheType valueType;
private long entryCapacity = UNSET_LONG;
private boolean strictEviction = false;
private boolean refreshAhead = false;
private long expireAfterWrite = UNSET_LONG;
private long retryInterval = UNSET_LONG;
private long maxRetryInterval = UNSET_LONG;
private long timerLag = UNSET_LONG;
private long resilienceDuration = UNSET_LONG;
private long maximumWeight = UNSET_LONG;
private boolean keepDataAfterExpired = false;
private boolean sharpExpiry = false;
private boolean suppressExceptions = true;
private int loaderThreadCount;
private boolean permitNullValues = false;
private boolean disableStatistics = false;
private boolean recordRefreshedTime = false;
private boolean externalConfigurationPresent = false;
private boolean boostConcurrency = false;
private boolean enableJmx = false;
private boolean disableMonitoring = false;
private CustomizationSupplier loaderExecutor;
private CustomizationSupplier refreshExecutor;
private CustomizationSupplier asyncListenerExecutor;
private CustomizationSupplier executor;
private CustomizationSupplier> expiryPolicy;
private CustomizationSupplier> resiliencePolicy;
private CustomizationSupplier extends FunctionalCacheLoader> loader;
private CustomizationSupplier> writer;
private CustomizationSupplier> advancedLoader;
private CustomizationSupplier> asyncLoader;
private CustomizationSupplier> exceptionPropagator;
private CustomizationSupplier timeReference;
private CustomizationSupplier weigher;
private CustomizationCollection> listeners;
private CustomizationCollection> asyncListeners;
private CustomizationCollection closedListeners;
private ConfigurationSectionContainer sections;
/**
* Construct a config instance setting the type parameters and returning a
* proper generic type.
*
* @see Cache2kBuilder#keyType(Class)
* @see Cache2kBuilder#valueType(Class)
*/
public static Cache2kConfiguration of(Class keyType, Class valueType) {
Cache2kConfiguration c = new Cache2kConfiguration();
c.setKeyType(keyType);
c.setValueType(valueType);
return c;
}
/**
* Construct a config instance setting the type parameters and returning a
* proper generic type.
*
* @see Cache2kBuilder#keyType(CacheType)
* @see Cache2kBuilder#valueType(CacheType)
*/
public static Cache2kConfiguration of(Class keyType, CacheType valueType) {
Cache2kConfiguration c = new Cache2kConfiguration();
c.setKeyType(keyType);
c.setValueType(valueType);
return c;
}
/**
* Construct a config instance setting the type parameters and returning a
* proper generic type.
*
* @see Cache2kBuilder#keyType(Class)
* @see Cache2kBuilder#valueType(Class)
*/
public static Cache2kConfiguration of(CacheType keyType, Class valueType) {
Cache2kConfiguration c = new Cache2kConfiguration();
c.setKeyType(keyType);
c.setValueType(valueType);
return c;
}
/**
* Construct a config instance setting the type parameters and returning a
* proper generic type.
*
* @see Cache2kBuilder#keyType(CacheType)
* @see Cache2kBuilder#valueType(CacheType)
*/
public static Cache2kConfiguration of(CacheType keyType, CacheType valueType) {
Cache2kConfiguration c = new Cache2kConfiguration();
c.setKeyType(keyType);
c.setValueType(valueType);
return c;
}
/**
* @see Cache2kBuilder#name(String)
*/
public String getName() {
return name;
}
/**
*
* @see Cache2kBuilder#name(String)
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @see Cache2kBuilder#entryCapacity
*/
public long getEntryCapacity() {
return entryCapacity;
}
public void setEntryCapacity(long v) {
this.entryCapacity = v;
}
/**
* @see Cache2kBuilder#refreshAhead(boolean)
*/
public boolean isRefreshAhead() {
return refreshAhead;
}
/**
* @see Cache2kBuilder#refreshAhead(boolean)
*/
public void setRefreshAhead(boolean v) {
this.refreshAhead = v;
}
public CacheType getKeyType() {
return keyType;
}
private void checkNull(Object v) {
if (v == null) {
throw new NullPointerException("null value not allowed");
}
}
/**
* @see Cache2kBuilder#keyType(Class)
* @see CacheType for a general discussion on types
*/
public void setKeyType(Class v) {
checkNull(v);
setKeyType(CacheTypeCapture.of(v));
}
/**
* @see Cache2kBuilder#keyType(CacheType)
* @see CacheType for a general discussion on types
*/
public void setKeyType(CacheType v) {
checkNull(v);
if (v.isArray()) {
throw new IllegalArgumentException("Arrays are not supported for keys");
}
keyType = v.getBeanRepresentation();
}
public CacheType getValueType() {
return valueType;
}
/**
* @see Cache2kBuilder#valueType(Class)
* @see CacheType for a general discussion on types
*/
public void setValueType(Class v) {
checkNull(v);
setValueType(CacheTypeCapture.of(v));
}
/**
* @see Cache2kBuilder#valueType(CacheType)
* @see CacheType for a general discussion on types
*/
public void setValueType(CacheType v) {
checkNull(v);
if (v.isArray()) {
throw new IllegalArgumentException("Arrays are not supported for values");
}
valueType = v.getBeanRepresentation();
}
public boolean isEternal() {
return expireAfterWrite == UNSET_LONG || expireAfterWrite == ExpiryTimeValues.ETERNAL;
}
/**
* @see Cache2kBuilder#eternal(boolean)
*/
public void setEternal(boolean v) {
if (v) {
setExpireAfterWrite(ExpiryTimeValues.ETERNAL);
} else {
setExpireAfterWrite(EXPIRY_NOT_ETERNAL);
}
}
public long getExpireAfterWrite() {
return expireAfterWrite;
}
/**
* @see Cache2kBuilder#expireAfterWrite
*/
public void setExpireAfterWrite(long millis) {
if (millis == expireAfterWrite) {
return;
}
if (expireAfterWrite != UNSET_LONG) {
if (millis == Expiry.ETERNAL) {
throw new IllegalArgumentException(
"eternal disabled or expiry was set, refusing to reset back to eternal");
}
if (expireAfterWrite == Expiry.ETERNAL) {
throw new IllegalArgumentException("eternal enabled explicitly, refusing to enable expiry");
}
}
this.expireAfterWrite = millis;
}
public long getTimerLag() {
return timerLag;
}
/**
* @see Cache2kBuilder#timerLag(long, TimeUnit)
*/
public void setTimerLag(long timerLag) {
this.timerLag = timerLag;
}
/**
* @see Cache2kBuilder#retryInterval
*/
public long getRetryInterval() {
return retryInterval;
}
/**
* @see Cache2kBuilder#retryInterval
*/
public void setRetryInterval(long millis) {
retryInterval = millis;
}
/**
* @see Cache2kBuilder#maxRetryInterval
*/
public long getMaxRetryInterval() {
return maxRetryInterval;
}
/**
* @see Cache2kBuilder#maxRetryInterval
*/
public void setMaxRetryInterval(long millis) {
maxRetryInterval = millis;
}
/**
* @see Cache2kBuilder#resilienceDuration
*/
public long getResilienceDuration() {
return resilienceDuration;
}
/**
* @see Cache2kBuilder#resilienceDuration
*/
public void setResilienceDuration(long millis) {
resilienceDuration = millis;
}
public boolean isKeepDataAfterExpired() {
return keepDataAfterExpired;
}
public long getMaximumWeight() {
return maximumWeight;
}
/**
* @see Cache2kBuilder#maximumWeight
*/
public void setMaximumWeight(long v) {
if (entryCapacity >= 0) {
throw new IllegalArgumentException(
"entryCapacity already set, setting maximumWeight is illegal");
}
maximumWeight = v;
}
/**
* @see Cache2kBuilder#keepDataAfterExpired(boolean)
*/
public void setKeepDataAfterExpired(boolean v) {
this.keepDataAfterExpired = v;
}
public boolean isSharpExpiry() {
return sharpExpiry;
}
/**
* @see Cache2kBuilder#sharpExpiry(boolean)
*/
public void setSharpExpiry(boolean v) {
this.sharpExpiry = v;
}
public boolean isSuppressExceptions() {
return suppressExceptions;
}
/**
* @see Cache2kBuilder#suppressExceptions(boolean)
*/
public void setSuppressExceptions(boolean v) {
this.suppressExceptions = v;
}
/**
* An external configuration for the cache was found and is applied.
* This is {@code true} if default values are set via the XML configuration or
* if there is a specific section for the cache name.
*/
public boolean isExternalConfigurationPresent() {
return externalConfigurationPresent;
}
public void setExternalConfigurationPresent(boolean v) {
externalConfigurationPresent = v;
}
/**
* Mutable collection of additional configuration sections
*/
public ConfigurationSectionContainer getSections() {
if (sections == null) {
sections = new ConfigurationSectionContainer();
}
return sections;
}
/**
* Adds the collection of sections to the existing list. This method is intended to
* improve integration with bean configuration mechanisms that use the set method and
* construct a set or list, like Springs' bean XML configuration.
*/
public void setSections(Collection c) {
getSections().addAll(c);
}
public CustomizationSupplier extends FunctionalCacheLoader> getLoader() {
return loader;
}
public void setLoader(CustomizationSupplier extends FunctionalCacheLoader> v) {
loader = v;
}
public CustomizationSupplier> getAdvancedLoader() {
return advancedLoader;
}
/**
* @see Cache2kBuilder#loader(AdvancedCacheLoader)
*/
public void setAdvancedLoader(CustomizationSupplier> v) {
advancedLoader = v;
}
public CustomizationSupplier> getAsyncLoader() {
return asyncLoader;
}
public void setAsyncLoader(CustomizationSupplier> v) {
asyncLoader = v;
}
public int getLoaderThreadCount() {
return loaderThreadCount;
}
/**
* @see Cache2kBuilder#loaderThreadCount(int)
*/
public void setLoaderThreadCount(int v) {
loaderThreadCount = v;
}
public CustomizationSupplier> getExpiryPolicy() {
return expiryPolicy;
}
public void setExpiryPolicy(CustomizationSupplier> v) {
expiryPolicy = v;
}
public CustomizationSupplier> getWriter() {
return writer;
}
/**
* @see Cache2kBuilder#writer(CacheWriter)
*/
public void setWriter(CustomizationSupplier> v) {
writer = v;
}
public boolean isStoreByReference() {
return storeByReference;
}
/**
* @see Cache2kBuilder#storeByReference(boolean)
*/
public void setStoreByReference(boolean v) {
storeByReference = v;
}
public CustomizationSupplier> getExceptionPropagator() {
return exceptionPropagator;
}
/**
* @see Cache2kBuilder#exceptionPropagator(ExceptionPropagator)
*/
public void setExceptionPropagator(CustomizationSupplier> v) {
exceptionPropagator = v;
}
/**
* A set of listeners. Listeners added in this collection will be
* executed in a synchronous mode, meaning, further processing for
* an entry will stall until a registered listener is executed.
* The expiry will be always executed asynchronously.
*
* A listener can be added by adding it to the collection.
* Duplicate (in terms of equal objects) listeners will be ignored.
*
* @return Mutable collection of listeners
*/
public CustomizationCollection> getListeners() {
if (listeners == null) {
listeners = new DefaultCustomizationCollection>();
}
return listeners;
}
/**
* @return True if listeners are added to this configuration.
*/
public boolean hasListeners() {
return listeners != null && !listeners.isEmpty();
}
/**
* Adds the collection of customizations to the existing list. This method is intended to
* improve integration with bean configuration mechanisms that use the set method and
* construct a set or list, like Springs' bean XML configuration.
*/
public void setListeners(Collection>> c) {
getListeners().addAll(c);
}
/**
* A set of listeners. A listener can be added by adding it to the collection.
* Duplicate (in terms of equal objects) listeners will be ignored.
*
* @return Mutable collection of listeners
*/
public CustomizationCollection> getAsyncListeners() {
if (asyncListeners == null) {
asyncListeners = new DefaultCustomizationCollection>();
}
return asyncListeners;
}
/**
* @return True if listeners are added to this configuration.
*/
public boolean hasAsyncListeners() {
return asyncListeners != null && !asyncListeners.isEmpty();
}
/**
* Adds the collection of customizations to the existing list. This method is intended to
* improve integration with bean configuration mechanisms that use the set method and
* construct a set or list, like Springs' bean XML configuration.
*/
public void setAsyncListeners(
Collection>> c) {
getAsyncListeners().addAll(c);
}
/**
* A set of listeners. A listener can be added by adding it to the collection.
* Duplicate (in terms of equal objects) listeners will be ignored.
*
* @return Mutable collection of listeners
* @since 1.0.2
*/
public CustomizationCollection getCacheClosedListeners() {
if (closedListeners == null) {
closedListeners = new DefaultCustomizationCollection();
}
return closedListeners;
}
/**
* @return True if listeners are added to this configuration.
*/
public boolean hasCacheClosedListeners() {
return closedListeners != null && !closedListeners.isEmpty();
}
/**
* Adds the collection of customizations to the existing list. This method is intended to
* improve integration with bean configuration mechanisms that use the set method and
* construct a set or list, like Springs' bean XML configuration.
*/
public void setCacheClosedListeners(Collection> c) {
getCacheClosedListeners().addAll(c);
}
public CustomizationSupplier> getResiliencePolicy() {
return resiliencePolicy;
}
/**
* @see Cache2kBuilder#resiliencePolicy
*/
public void setResiliencePolicy(CustomizationSupplier> v) {
resiliencePolicy = v;
}
public boolean isStrictEviction() {
return strictEviction;
}
/**
* @see Cache2kBuilder#strictEviction(boolean)
*/
public void setStrictEviction(boolean v) {
strictEviction = v;
}
public boolean isPermitNullValues() {
return permitNullValues;
}
/**
* @see Cache2kBuilder#permitNullValues(boolean)
*/
public void setPermitNullValues(boolean v) {
permitNullValues = v;
}
public boolean isDisableStatistics() {
return disableStatistics;
}
/**
* @see Cache2kBuilder#disableStatistics
*/
public void setDisableStatistics(boolean v) {
disableStatistics = v;
}
public CustomizationSupplier getLoaderExecutor() {
return loaderExecutor;
}
@Deprecated
public boolean isDisableLastModificationTime() {
return false;
}
/**
* @see Cache2kBuilder#disableLastModificationTime
*/
@Deprecated
public void setDisableLastModificationTime(boolean v) { }
public boolean isRecordRefreshedTime() {
return recordRefreshedTime;
}
/**
* @see Cache2kBuilder#recordRefreshedTime
*/
public void setRecordRefreshedTime(boolean v) {
recordRefreshedTime = v;
}
/**
* @see Cache2kBuilder#loaderExecutor(Executor)
*/
public void setLoaderExecutor(CustomizationSupplier v) {
loaderExecutor = v;
}
/**
* @deprecated Use {@link #getRefreshExecutor()}
*/
public CustomizationSupplier getPrefetchExecutor() {
return refreshExecutor;
}
/**
* @see Cache2kBuilder#prefetchExecutor(Executor)
* @deprecated use {@link #setRefreshExecutor(CustomizationSupplier)}
*/
public void setPrefetchExecutor(CustomizationSupplier v) {
refreshExecutor = v;
}
public CustomizationSupplier getRefreshExecutor() {
return refreshExecutor;
}
/**
* @see Cache2kBuilder#refreshExecutor(Executor)
*/
public void setRefreshExecutor(CustomizationSupplier v) {
refreshExecutor = v;
}
public CustomizationSupplier getExecutor() {
return executor;
}
/**
* @see Cache2kBuilder#executor(Executor)
*/
public void setExecutor(CustomizationSupplier v) {
executor = v;
}
public CustomizationSupplier getAsyncListenerExecutor() {
return asyncListenerExecutor;
}
/**
* @see Cache2kBuilder#asyncListenerExecutor(Executor)
*/
public void setAsyncListenerExecutor(CustomizationSupplier v) {
asyncListenerExecutor = v;
}
public CustomizationSupplier getTimeReference() {
return timeReference;
}
/**
* @see Cache2kBuilder#timeReference(TimeReference)
*/
public void setTimeReference(CustomizationSupplier v) {
timeReference = v;
}
public CustomizationSupplier getWeigher() {
return weigher;
}
/**
* @see Cache2kBuilder#weigher(Weigher)
*/
public void setWeigher(CustomizationSupplier v) {
if (entryCapacity >= 0) {
throw new IllegalArgumentException(
"entryCapacity already set, specifying a weigher is illegal");
}
weigher = v;
}
public boolean isBoostConcurrency() {
return boostConcurrency;
}
/**
* @see Cache2kBuilder#boostConcurrency(boolean)
*/
public void setBoostConcurrency(boolean v) {
boostConcurrency = v;
}
public boolean isEnableJmx() {
return enableJmx;
}
/**
* @see Cache2kBuilder#enableJmx(boolean)
*/
public void setEnableJmx(boolean v) {
enableJmx = v;
}
public boolean isDisableMonitoring() {
return disableMonitoring;
}
/**
* @see Cache2kBuilder#disableMonitoring(boolean)
*/
public void setDisableMonitoring(boolean disableMonitoring) {
this.disableMonitoring = disableMonitoring;
}
}