Can I use the JCache API for distributed caches in Apache Ignite?

2.3k views Asked by At

I would like to configure a distributed cache with Apache Ignite using the JCache API (JSR107, javax.cache). Is this possible?

The examples I have found either create a local cache with the JCache API or create a distributed cache (or datagrid) using the Apache Ignite API.

2

There are 2 answers

2
Valentin Kulichenko On BEST ANSWER

JCache allows to provide provider-specific configuration when creating a cache. I.e., you can do this:

// Get or create a cache manager.
CacheManager cacheMgr = Caching.getCachingProvider().getCacheManager();

// This is an Ignite configuration object (org.apache.ignite.configuration.CacheConfiguration).
CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();

// Specify cache mode and/or any other Ignite-specific configuration properties.
cfg.setCacheMode(CacheMode.PARTITIONED);

// Create a cache based on configuration create above.
Cache<Integer, String> cache = cacheMgr.createCache("a", cfg);

Also note that partitioned mode is actually the default one in Ignite, so you are not required to specify it explicitly.

UPD. In addition, CachingProvider.getCacheManager(..) method accepts a provider-specific URI that in case of Ignite should point to XML configuration file. Discovery, communication and other parameters can be provided there.

0
sg4j On

Please note that JCache specification does not specify all the configurations that apply to individual cache providers in terms of configuring via CacheManager for creating a Grid. The requirement for creating a CacheManager is standard but not everything relevant to how the manager itself is configured.

Following code will demonstrate how to create a grid using Apache Ignite in SpringBoot

@Bean
@SuppressWarnings("unchecked")
public org.apache.ignite.cache.spring.SpringCacheManager cacheManager() {
    IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
    igniteConfiguration.setGridName("petclinic-ignite-grid");
    //igniteConfiguration.setClassLoader(dynamicClassLoaderWrapper());

    igniteConfiguration.setCacheConfiguration(this.createDefaultCache("petclinic"),
            this.createDefaultCache("org.hibernate.cache.spi.UpdateTimestampsCache"),
            this.createDefaultCache("org.hibernate.cache.internal.StandardQueryCache"));

    SpringCacheManager springCacheManager = new SpringCacheManager();
    springCacheManager.setConfiguration(igniteConfiguration);
    springCacheManager.setDynamicCacheConfiguration(this.createDefaultCache(null));
    return springCacheManager;
}

private org.apache.ignite.configuration.CacheConfiguration createDefaultCache(String name) {

    org.apache.ignite.configuration.CacheConfiguration cacheConfiguration = new org.apache.ignite.configuration.CacheConfiguration();
    cacheConfiguration.setName(name);
    cacheConfiguration.setCacheMode(CacheMode.PARTITIONED);
    cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    cacheConfiguration.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    cacheConfiguration.setStatisticsEnabled(true);
    cacheConfiguration.setEvictSynchronized(true);
    return cacheConfiguration;
 }
}

If we were to create another instance of this service and have it register to the same grid as igniteConfiguration.setGridName("petclinic-ignite-grid"), an IMDG will be created. Please note that the 2 service instances with this version of partitioned, embedded distributed cache should be able to talk to each other via required PORTS. Please refer to Apache Ignite - Data Grid for more details.

Hope this helps.