Chapter 3. Running Data Grid as an Embedded Library
Learn how to run Data Grid as an embedded data store in your project.
Procedure
- Initialize the default Cache Manager and add a cache definition 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 default cache definitions so after initializing the default Cache Manager, you need to add at least one cache instance. This example uses the ConfigurationBuilder
class to create a 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.