xmemcached set byte array

250 Views Asked by At

Im using xmemcached to write memcacheclient. Now I want to have a method like: set(key, byte[]) and get(key) return byte[] in memcache client. Anyone can suggest me something to do that in XMEMCACHED. In my case, Im using protocol buffer to serial/deserial objects, then store them to Memcache. Thanks so much.

2

There are 2 best solutions below

1
On

I think you can set the byte array directly.

0
On

To be able to get byte[] value with XMemcached I implemented my own transcoder:

public class ByteArrayTranscoder implements net.rubyeye.xmemcached.transcoders.Transcoder<byte[]>
{

    @Override
    public byte[] decode(CachedData d)
    {
        return d.getData();
    }
    @Override
    public CachedData encode(byte[] o)
    {
        throw new UnsupportedOperationException();
    }
    // UnsupportedOperationException for all other methods
}

Usage:

byte[] value = client.get(queueName, new ByteArrayTranscoder())

Probably, the same trick can be done for setting value, though I didn't tried it.