2 Replies Latest reply on Dec 7, 2016 10:04 AM by Paulo Henrique Alves

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

    Paulo Henrique Alves Newbie

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