Principles
The configuration derivation features allows a new Ehcache configuration object to be derived via a transformation on an existing configuration object. This can be useful for:
-
pre-processing an externally sourced configuration, adding additional settings before creating the cache manager.
-
processing the configuration of an existing cache manager to generate a new configuration.
The basis of the configuration derivation API is the Configuration.derive()
method that generates a builder seeded
with the configurations values
FluentConfigurationBuilder<?> derivedBuilder = configuration.derive(); (1)
Configuration configurationCopy = derivedBuilder.build(); (2)
1 | Creates a builder seeded with the configuration’s state. |
2 | Configurations built using the builder are then functionally identical to the original configuration. |
Core Configuration Changes
The configuration builder returned by the derive method provide direct methods for modifying core configuration concepts:
Configuration withClassLoader = configuration.derive()
.withClassLoader(classLoader)
.build();
Configuration withCache = configuration.derive()
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(
Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
|
→ |
|
Configuration withoutCache = configuration.derive()
.withoutCache("cache")
.build();
|
→ |
|
Updating a cache configuration uses a UnaryOperator
that is run against a cache configuration
builder seeded using the existing cache configuration.
Configuration withOffHeap = configuration.derive()
.updateCache("cache", cache -> cache.updateResourcePools(
resources -> ResourcePoolsBuilder.newResourcePoolsBuilder(resources)
.offheap(100, MemoryUnit.MB)
.build()))
.build();
|
→ |
|
Extended Configuration Changes
Ehcache is a pluggable system, so modifying many of the more complex configurations requires modifying both service creation configurations and service configurations:
Configuration withBoundedThreads = configuration.derive()
.withService(new PooledExecutionServiceConfiguration()
.addDefaultPool("default", 1, 16))
.build();
|
→ |
|
Configuration withUpdatedPersistence = configuration.derive()
.updateServices(DefaultPersistenceConfiguration.class,
existing -> new File("/var/persistence/path"))
.build();
|
→ |
|
Configuration withThrowingStrategy = configuration.derive()
.updateCache("cache", existing -> existing.withService(
new DefaultResilienceStrategyConfiguration(new ThrowingResilienceStrategy<>())
))
.build();
|
→ |
|
Configuration changedConsistency = configuration.derive()
.updateCache("cache", cache -> cache.updateServices(
ClusteredStoreConfiguration.class,
existing -> Consistency.EVENTUAL)
)
.build();
|
→ |
|
Removing a service
Removing a service often involves removing both service creation and a service configuration instances since a service instance its configuration are usually strongly coupled:
Configuration withoutClustering = configuration.derive()
.updateCaches(cache -> cache (1)
.withoutServices(ClusteredStoreConfiguration.class) (2)
.updateResourcePools(existing -> {
ResourcePoolsBuilder poolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder(); (3)
for (ResourcePool pool : existing.getResourceTypeSet().stream() (4)
.filter(p -> !(p instanceof ClusteredResourceType)) (5)
.map(existing::getPoolForResource)
.toArray(ResourcePool[]::new)) {
poolsBuilder = poolsBuilder.with(pool); (6)
}
return poolsBuilder.build();
}))
.withoutServices(ClusteringServiceConfiguration.class) (7)
.build();
1 | From all cache configurations… |
2 | remove any existing ClusteredStoreConfiguration instances. |
3 | Create a new resource pool builder… |
4 | From the existing resource pools… |
5 | filter out any clustered resources. |
6 | Add all remaining pools to the new resource pools instance |
7 | Finally remove the clustering service creation configuration |
|
→ |
|