Chapter 7. Defining File-based Cache Stores

Define a file-based cache store with datagrid-service to persist data to external storage.

Use the XMLStringConfiguration class to provide XML configuration as a string through the Hot Rod interface.

  • XML must be valid with the Data Grid configuration schema.
  • Location of your file store should be under the storage volume mounted at /opt/datagrid/standalone/data because the data folder is a PersistentVolume that allows data to survive when the container restarts.

As an example, the following main method creates a cache with a distributed configuration that includes a file store:

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());

}

For information about valid file-store configuration options, see the Data Grid configuration schema.

See the Javadocs for more information: