2 Replies Latest reply on Dec 7, 2016 10:04 AM by h3nrique

    What the better way to store Collection Type in Remote Cache?

    h3nrique

      Hi Guys,

      I have an application that use a remote cache with key as String and value is a HashSet and when my application execute I check that its not performing as well. I know that the better way is always use a Complex Object to be store in Cache, but I want know if anybody have used Collection Type like List, Map or Set as a value in cache and what a implementation used.

       

      To complements, I'm using a HashSetMarshaller.java to write a value cache.

       

      import org.infinispan.protostream.MessageMarshaller;
      
      
      import java.io.*;
      import java.util.HashSet;
      
      
      public class HashSetMarshaller implements MessageMarshaller<HashSet> {
      
      
        @Override
        public String getTypeName() {
        return "java.util.HashSet";
        }
      
      
        @Override
        public Class<? extends HashSet> getJavaClass() {
        return HashSet.class;
        }
      
      
        @Override
        public HashSet readFrom(ProtoStreamReader protoStreamReader) throws IOException {
        byte[] readBytes = protoStreamReader.readBytes("bytess");
        ByteArrayInputStream byteIn = new ByteArrayInputStream(readBytes);
        ObjectInputStream in = new ObjectInputStream(byteIn);
        try {
        Object readObject = in.readObject();
        return (HashSet) readObject;
        } catch (ClassNotFoundException err) {
        throw new IllegalArgumentException("Erro durante leitura de objeto.", err);
        }
        }
      
      
        @Override
        public void writeTo(ProtoStreamWriter protoStreamWriter, HashSet hashSet) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(hashSet);
        protoStreamWriter.writeBytes("bytess", byteArrayOutputStream.toByteArray());
        }
      }
      
        • 1. Re: What the better way to store Collection Type in Remote Cache?
          rvansa

          Infinispan offers AtomicMaps that perform better as these send only deltas of the operations, but regrettably this is not available over remote protocols. If you want to modify/select from big complex data in-place, you have to use stored scripts invoking functional calls. Regrettably it won't be possible to invoke functional calls over Hot Rod directly - Hot Rod is language agnostic (as well as the other remote protocols) and you can't just pass a lambda through.

           

          However treating complex relationships of objects in Infinispan can be tricky. That's why Hibernate OGM has recently implemented support over Hot Rod protocol

          1 of 1 people found this helpful
          • 2. Re: What the better way to store Collection Type in Remote Cache?
            h3nrique

            Thanks Radim!

            I redo my logic and replace HashSet to a List. The code below is was i store in cache.

             

            Write

            protoStreamWriter.writeArray("arrayObject", myObject.getObjectList().toArray(), ObjectList.class);

             

            Read

            ObjectList[] myList = protoStreamReader,readArray("arrayObject", ObjectList.class);

             

             

            Ps: I will do some tests with Hibernate OGM.