Chapter 3. Initializing embedded caches
Intialize the Cache Manager and add embedded cache in your project to start running Data Grid in your application.
Procedure
- Initialize the default Cache Manager and add an embedded cache named as follows:
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); DefaultCacheManager cacheManager = new DefaultCacheManager(global.build()); ConfigurationBuilder builder = new ConfigurationBuilder(); builder.clustering().cacheMode(CacheMode.DIST_SYNC); cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("myCache", builder.build());
The preceding code initializes a default, clustered Cache Manager. Cache Managers contain your cache definitions and control cache lifecycles.
Data Grid does not provide a default cache configuration so after initializing the default Cache Manager, you need to add at least one cache instance. This example uses the ConfigurationBuilder
class to create an embedded cache definition that uses the distributed, synchronous cache mode. You then call the getOrCreateCache()
method that either creates a cache named "myCache" on all nodes in the cluster or returns it if it already exists.
Next steps
Now that you have a running Cache Manager with a cache created, you can add some more cache definitions, put some data into the cache, or configure Data Grid as needed.