12.2. 创建远程缓存

先决条件

  1. 确保您的 cache-api 位于您的 classpath 上。
  2. 将以下依赖项添加到 pom.xml 中:

    <dependency>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-jcache-remote</artifactId>
    </dependency>

流程

  • 在远程 Data Grid 服务器上创建缓存,并使用默认的 JCache API 配置,如下所示:
import javax.cache.*;
import javax.cache.configuration.*;

// Retrieve the system wide cache manager via org.infinispan.jcache.remote.JCachingProvider
CacheManager cacheManager = Caching.getCachingProvider("org.infinispan.jcache.remote.JCachingProvider").getCacheManager();
// Define a named cache with default JCache configuration
Cache<String, String> cache = cacheManager.createCache("remoteNamedCache",
      new MutableConfiguration<String, String>());

12.2.1. 配置远程缓存

热 Rod 配置文件包括 infinispan.client.hotrod.cache mdadm 属性,可用于自定义远程缓存。

  • hotrod-client.properties 文件的 URI 传递给 caching Provider.getCacheManager (URI) 调用,如下所示:
import javax.cache.*;
import javax.cache.configuration.*;

// Load configuration from an absolute filesystem path
URI uri = URI.create("file:///path/to/hotrod-client.properties");
// Load configuration from a classpath resource
// URI uri = this.getClass().getClassLoader().getResource("hotrod-client.properties").toURI();

// Retrieve the system wide cache manager via org.infinispan.jcache.remote.JCachingProvider
CacheManager cacheManager = Caching.getCachingProvider("org.infinispan.jcache.remote.JCachingProvider")
      .getCacheManager(uri, this.getClass().getClassLoader(), null);