Example: Using BigMemory Go 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 Go is a powerful in-memory data management solution. Among its many applications, BigMemory Go 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 Go .
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.updateCheck(true)
.monitoring(Configuration.Monitoring.AUTODETECT)
.name("cacheManagerCompleteExample")
.addCache(
new CacheConfiguration()
.name("sample-cache")
.maxBytesLocalHeap(512, MemoryUnit.MEGABYTES)
.maxBytesLocalOffHeap(1, MemoryUnit.GIGABYTES)
.timeToLiveSeconds(4)
.timeToIdleSeconds(2)
);
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.