Cache Template

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Overview

A cache template is an instance of the CacheConfiguration class that can be registered in the cluster via the Ignite.addCacheConfiguration(...) method and used later as a basis for creating new caches. A cache created from a template inherits all the properties of the template.

Cache templates are useful when you want to create a cache with the same configuration as an existing cache in the cluster. This allows you to create a cache without defining a long list of configuration parameters. Currently, templates in Ignite are supported for the CREATE TABLE and REST commands. When a template name is used, Ignite uses all the configurations mentioned in the template to instantiate the new cache.

Defining a Cache Template

To create a template, define a cache configuration and add it to the Ignite instance, as shown below. If you want do define a cache template in the XML configuration file, you must add an asterisk to the template's name. This is required to indicate that the configuration is a template and not an actual cache.

Ignite ignite = Ignition.start();

CacheConfiguration cfg = new CacheConfiguration("myCacheTemplate");
// Set required cache configuration properties.
cfg.setBackups(2);
cfg.setCacheMode(CacheMode.PARTITIONED);
...

// Register the cache template in Ignite.
ignite.addCacheConfiguration(cfg);
<property name="cacheConfiguration"> 
   <list> 
       <bean id="cache-template-bean" abstract="true" class="org.apache.ignite.configuration.CacheConfiguration"> 
         <!-- when you create a template via XML configuration, 
         you must add an asterisk to the name of the template -->
          <property name="name" value="myCacheTemplate*"/> 
          <property name="cacheMode" value="PARTITIONED" /> 
          <property name="backups" value="2" /> 
          <!-- Other cache configurations -->
          ...
       </bean> 
   </list> 
</property>

Once the cache template is registered in the cluster, as shown in the code snippet above, you can use it to create another cache with the same configuration.

Creating a Cache Based On a Template

You can create a cache based on a template using the following commands.

Using the REST command

http://host:port/ignite?cmd=getorcreate&cacheName=mynewCache&templateName=myCacheTemplate

In the above command, replace host and port with actual values.

Using the CREATE TABLE command

CREATE TABLE IF NOT EXISTS Person (
  id int,
  city_id int,
  name varchar,
  age int, 
  company varchar,
  PRIMARY KEY (id, city_id)
) WITH "template=myCacheTemplate, key_type=PersonKey, value_type=MyPerson";