Cross-client memcached compatibility
Dustin Sallings
dustin at spy.net
Tue Jan 29 20:32:01 UTC 2008
On Jan 29, 2008, at 11:57, Ciaran wrote:
> My problem appears to be that I can't get the data out with your
> client (I assume yours is the Spy 'Optomised' Java client) as it
> 'sees' the flag telling it the data is compressed, and attempts to
> de-compress (the un-compressed) data immediately so I get a null
> back [this what I've interpreted from setting up an ErrorHandler]
>
> Are you saying there might be a way for me to disable to automatic
> de-compression (enableCompression(false) only seems to affect
> compression [makes sense I guess!) so I can get this data out ?
The thing that takes your value and formats it for memcached is
modular. It's this interface:
http://bleu.west.spy.net/~dustin/projects/memcached/apidocs/net/spy/memcached/Transcoder.html
You give it an object, it returns a CachedData object (byte array and
flags integer). You give it a CachedData object and it gives you your
original object back. You could implement something like this:
public StringTranscoder implements Transcoder {
public CachedData encode(Object o) {
// Will throw a ClassCastException if it's not a string.
String s=(String)o;
return new CachedData(0, s.getBytes("UTF-8"));
}
public Object decode(CachedData d) {
return new String(d.getData(), "UTF-8");
}
}
(note that there's an UnsupportedEncodingException that needs to be
caught and rethrown in there, but that's the basic idea).
Then you just do this:
client.setTranscoder(new StringTranscoder());
-- again, though, this solves your exact problem, but not the general
problem of getting clients to agree on storage formats. That comes up
quite often.
--
Dustin Sallings
More information about the memcached
mailing list