BigMemory 4.3.9 | Code Samples | Example: Using BigMemory Max As a Cache
 
Example: Using BigMemory Max As a Cache
Note:
The following description focuses on code snippets from the full code example, which is available at the /code-samples/src/ location in the installed product kit.
BigMemory Max is a powerful in-memory data management solution. Among its many applications, BigMemory Max may be used as a cache to speed up access to data from slow or expensive databases and other remote data sources. This example shows how to enable and configure the caching features available in BigMemory Max.
The following programmatic configuration snippet shows how to set time-to-live (TTL) and time-to-idle (TTI) policies on a data set:
Configuration managerConfiguration = new Configuration();
managerConfiguration.name("cacheManagerCompleteExample")
.terracotta(new TerracottaClientConfiguration().url("localhost:9510"))
.cache(
new CacheConfiguration()
.name("sample-cache")
.maxBytesLocalHeap(128, MemoryUnit.MEGABYTES)
.timeToLiveSeconds(4)
.timeToIdleSeconds(2)
.terracotta(new TerracottaConfiguration())
);
CacheManager manager = CacheManager.create(managerConfiguration);
The timeToLiveSeconds directive sets the maximum age of an element in the data set. Elements older than the maximum TTL will not be returned from the data store. This is useful when BigMemory is used as a cache of external data and you want to ensure the freshness of the cache.
The timeToIdleSeconds directive sets the maximum time since last access of an element. Elements that have been idle longer than the maximum TTI will not be returned from the data store. This is useful when BigMemory is being used as a cache of external data and you want to bias the eviction algorithm towards removing idle entries.
If neither TTL nor TTI are set (or set to zero), data will stay in BigMemory until it is explicitly removed.