Cache Manager Configuration and Usage of Server Side Resources
This code sample demonstrates the usage of the concepts explained in the previous section in configuring a cache manager and clustered caches by using a broader clustering service configuration:
CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
CacheManagerBuilder.newCacheManagerBuilder()
.with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application")).autoCreateOnReconnect(server -> server
.defaultServerResource("primary-server-resource") // <1>
.resourcePool("resource-pool-a", 8, MemoryUnit.MB, "secondary-server-resource") // <2>
.resourcePool("resource-pool-b", 10, MemoryUnit.MB))) // <3>
.withCache("clustered-cache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class, // <4>
ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ClusteredResourcePoolBuilder.clusteredDedicated(
"primary-server-resource", 8, MemoryUnit.MB)))) // <5>
.withCache("shared-cache-1",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ClusteredResourcePoolBuilder.clusteredShared(
"resource-pool-a")))) // <6>
.withCache("shared-cache-2",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ClusteredResourcePoolBuilder.clusteredShared(
"resource-pool-a")))); // <7>
PersistentCacheManager cacheManager =
clusteredCacheManagerBuilder.build(true); // <8>
cacheManager.close();
1 | defaultServerResource(String) on ClusteringServiceConfigurationBuilder instance sets the default server off-heap resource for the cache manager. From the example, cache manager sets its default server off-heap resource to primary-server-resource in the server. |
2 | Adds a resource pool for the cache manager with the specified name (resource-pool-a) and size (28MB) consumed out of the named server off-heap resource secondary-server-resource. A resource pool at the cache manager level maps directly to a shared pool at the server side. |
3 | Adds another resource pool for the cache manager with the specified name (resource-pool-b) and size (32MB). Since the server resource identifier is not explicitly passed, this resource pool will be consumed out of default server resource provided in Step 3. This demonstrates that a cache manager with clustering support can have multiple resource pools created out of several server off-heap resources. |
4 | Provide the cache configuration to be created. |
5 | ClusteredResourcePoolBuilder.clusteredDedicated(String , long , MemoryUnit) allocates a dedicated pool of storage to the cache from the specified server off-heap resource. In this example, a dedicated pool of 32MB is allocated for clustered-cache from primary-server-resource. |
6 | ClusteredResourcePoolBuilder.clusteredShared(String), passing the name of the resource pool specifies that shared-cache-1 shares the storage resources with other caches using the same resource pool (resource-pool-a). |
7 | Configures another cache (shared-cache-2) that shares the resource pool (resource-pool-a) with shared-cache-1. |
8 | Creates fully initialized cache manager with the clustered caches. |
Note:
When a cache is allocated a block of memory from a shared pool, it is retained forever and would never get reallocated to another cache sharing the pool.