So cache has to be global variable?
Following is my Encoder class. Where should I put the cache?

public abstract class AbstractMessageEncoder implements MessageEncoder
{
   protected AbstractMessageEncoder()
   {
   }

   public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception
   {
       AbstractMessage m = (AbstractMessage)message
;
       ByteBuffer buf = ByteBuffer.allocate(1024);

       buf.setAutoExpand(true); // Enable auto-expand for easier encoding

       encodeBody(session, m, buf);
       buf.flip();
       out.write(buf);
   }

   protected abstract void encodeBody(IoSession session, AbstractMessage
message, ByteBuffer out);
}

2007/4/25, Eero Nevalainen <[EMAIL PROTECTED]>:

Trustin Lee wrote:
>> > If so,
>> > should I move my encoding part in the IoHandler which means
>> > encoding once
>> > and send bytes to sessions(without encoding N times but once).
>
> You can do that, but it's not encouraged.  I'd use a LRU cache so the
> encoded buffer is cached, and reused on and on.  Of course, you will
> have to use the duplicate of the encoded buffer:
>
> ByteBuffer data = cache.get(message);
> out.write(data.duplicate());

I have to do something like this as well. Do you mean that this is
called from the encoder? So that the encoder has the cache as an
instance variable and for each session to broadcast it will retrieve the
message from the cache?

-Eero Nevalainen

Reply via email to