第 8 章 定义基于文件的缓存存储

使用 datagrid-service 定义基于文件的缓存存储,以便将数据持久化到外部存储。

使用 XMLStringConfiguration 类,通过 Hot Rod 接口将 XML 配置作为字符串提供。

  • XML 必须使用 Data Grid 配置模式有效。
  • 您文件存储的位置应该位于 /opt/datagrid/standalone/data 的存储卷中,因为 data 文件夹是一个 PersistentVolume,允许在容器重启时保留数据。

例如,以下 方法创建带有包含文件存储的分布式配置的缓存:

public static void main(String[] args) {

   ConfigurationBuilder cfg = ...

   RemoteCacheManager rcm = new RemoteCacheManager(build);

   String xml = String.format(
      "<infinispan>" +
         "<cache-container>" +
            "<distributed-cache name=\"%1$s\">" +
               "<persistence passivation=\"false\">" +
                  "<file-store " +
                     "shared=\"false\" " +
                     "fetch-state=\"true\" " +
                     "path=\"${jboss.server.data.dir}/datagrid-infinispan/%1$s\"" +
                  "/>" +
               "</persistence>" +
            "</distributed-cache>" +
         "</cache-container>" +
      "</infinispan>",
      "cacheName"
   );

   RemoteCache<Object, Object> index = rcm.administration()
   //Include a flag to make the cache permanent.
   .withFlags(CacheContainerAdmin.AdminFlag.PERMANENT)
   //Create a cache with the XML configuration
   .createCache("cacheName", new XMLStringConfiguration(xml));

   System.out.println(index.size());

}

有关有效 file-store 配置选项的详情,请查看 Data Grid 配置模式

如需更多信息,请参阅 Javadocs: