cachingdatagridignitejcachejsr107

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


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.


Solution

  • 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.