Search code examples
javainfinispan

Infinispan 10.1 - Append items to List object in the Remote Cache is not working


While try to a new item to List object stored in the Cache against the Key is not reflecting.

// Setup up a clustered cache manager
    ConfigurationBuilder builder = new ConfigurationBuilder();

    builder.addServer().host("127.0.0.1").port(11322).marshaller(new JavaSerializationMarshaller()).addJavaSerialWhiteList("java.util.List", "java.util.ArrayList");
    // Connect to the server
    RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
    // Create test cache, if such does not exist
    cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("test123", DefaultTemplate.DIST_SYNC);
    // Obtain the remote cache
    RemoteCache<String, List<String>> cache = cacheManager.getCache("test123");
    List<String> test = new ArrayList<String>();
    cache.put("key", test);

Append Data to the List:

    for (int i = 0; i < 10; i++) {
        cache.get("key").add("1234");
    }

Output of cache.get("key") is []

When using the infinispan implementation of Widlfly 10 above approach worked, but trying the same in Infinispan 10.1 standalone is not working, please highlight if any configuration needs to be done.


Solution

  • This could have worked if you used something similar with an embedded cache in local mode - cache.get(...) returns the stored object itself for efficiency reasons. I am not sure if that could also work had you configured client-side cache for remote (Hot Rod) client.

    However in all clustered modes (replicated, distributed) and with remote the cache.get(...) returns only a copy of the stored object. This copy is usually the result of deserialization of the object stored in the cache. Any mutations are not reflected neither in the cache nor in any object returned from a subsequent retrieval. You need to be explicit when modifying the cache.

    And you certainly don't want to invoke any synchronous operation 10 times when adding elements to the list but update the cache once when the list is complete.