Chapter 8. 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:

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.