Basic help required for the JAVA API

Greg Whalin greg at meetup.com
Mon Apr 9 14:50:57 UTC 2007


I doubt this is due to to serialization issues, though it could be.  By
default, the Java client hashes using the default String hashing as it is
fast and pretty good for even key distribution.  The other clients have all
agreed on a common hashing algorithm.  The java client supports this as
well, but you need to configure it to use it.  So, you should:

String[] serverlist = { "localhost:11001" };
SockIOPool memcached_pool = SockIOPool.getInstance();
memcached_pool.setServers( serverlist );
memcached_pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );
memcached_pool.initialize();

MemCachedClient mc = new MemCachedClient();
mc.setPrimitiveAsString(true);

This sets the hashing alg to the compat one (which I assume is used by the
PHP client), as well as stores all primitives as strings, which I think the
PHP client does as well.

gw

On 4/9/07, Arnaud Connois <arnaud-memcached at connois.com> wrote:
>
>
> Hi all,
>
> I've been using memcached for many year with great success with PHP.
>
> I'm building now a new application based on the Java API of Memcached
> which is going to run in background of my website
> (Apache/PHP/Mysql/Memcached) to generate complex data structures out of
> existing structures already in the cache.
>
> I've plugged into my mysql database quite easily, and I am now trying to
> plug into memcached. I'm kind of stuck at a very basic level to retreive
> data stored previously from PHP, probably beacuse of the fact that I'm
> not very used to Java, and more to PHP...
>
> So here is the idea :
>
> I have some arrays stored by php in the memcached and I'm trying to read
> them from the Java API...
>
> Here is a sample code...
>
>         String[] serverlist = { "localhost:11001" };
>         SockIOPool memcached_pool = SockIOPool.getInstance();
>         memcached_pool.setServers( serverlist );
>         memcached_pool.initialize();
>
>         MemCachedClient mc = new MemCachedClient();
>
>         Object myarray = mc.get( "list__2__smileys_to" );
>         System.out.println( "Query Result : " + myarray );
>
> I get a null result...
>
> Query Result : null
>
> I guess it cannot make sence of the serialized data...
>
> To make sure my structiure was there and readable I tried with
> "asString" set to TRUE in the get method :
>
>         String mystring = (String)mc.get( "list__2__smileys_to", null,
> true );
>         System.out.println( "Query Result : " + mystring );
>
> and I get a correct result :
>
> com.danga.MemCached.MemCachedClient Mon Apr 09 15:57:11 CEST 2007 - ++++
> retrieving object and stuffing into a string.
> Query Result :
>
> a:15:{i:11;s:1:"3";i:3;s:1:"4";i:7;s:1:"3";i:9;s:1:"4";i:10;s:1:"3";i:21;s:1:"1";i:24;s:1:"4";i:25;s:1:"4";i:30;s:1:"3";i:19;s:1:"1";i:18;s:1:"0";i:17;s:1:"6";i:16;s:1:"1";i:14;s:1:"4";i:1;s:1:"3";}
>
> - How can I use this data store as a PHP array from the Java API and
> have the serialised array being store into a Java object ?
> - Which java object showld I use ?
> - What synthax shoud I use ?
>
> I'm sure we have a few Java gurus on this list who are gonna fix that
> for me in a few minutes...
>
> Many Thanks
>
> Arnaud
>
>
>
>
>
>
>


-- 
Greg Whalin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.danga.com/pipermail/memcached/attachments/20070409/c5ad0eb9/attachment.htm


More information about the memcached mailing list