using memcached with c and python api

104 views
Skip to first unread message

aniketh

unread,
Dec 3, 2008, 3:12:02 AM12/3/08
to memcached
hey,

i've been trying to use the libmemcached api from c and the python
api
to access memcached.

however, i have come across a problem. I am setting values on multiple
servers using c, and attempting to retrieve it using python

fetching the keys back in c works fine, but python throws a lot of
"not found". i'm guessing it is something to do with the hash
algorithm. i tried setting it to crc32 in c, but it does not help.

any pointers would be appreciated.

thanks

regards,
aniketh

matt farey

unread,
Dec 3, 2008, 3:43:03 AM12/3/08
to memc...@googlegroups.com
Sorry for top post, have you looked at using thrift (apache incubator) and memcached together, ala facebook?

Sent from my BlackBerry® wireless device

-----Original Message-----
From: aniketh <ani...@gmail.com>

Date: Wed, 3 Dec 2008 00:12:02
To: memcached<memc...@googlegroups.com>
Subject: using memcached with c and python api

ruturajv

unread,
Dec 3, 2008, 4:35:02 AM12/3/08
to memcached
I too faced similar problem, eg

using.. PHP

<code>
<?php
$m = new Memcache;
$m->addserver('192.168.28.7, 11211);
$m->addserver('192.168.28.8, 11211);
$m->addserver('192.168.28.9, 11211);
for ($i=0; $i<30; $i++) {
$v = $m->set('fkey'.$i, $i);
// $va = $m->get('key'.$i);
var_dump($va);
}
</code>

and I'm unable to retrieve "ALL" keys using this (Python)

<code>
import memcache
m = memcache.Client(['192.168.28.7:11211', '192.168.28.8:11211',
'192.168.28.9:11211'])

for i in range(30):
a = m.get('key'+str(i))
print i, a

</code>

Brian Aker

unread,
Dec 3, 2008, 4:39:15 AM12/3/08
to memc...@googlegroups.com
Hi!

Are yo using the PHP wrapper for libmemcached?

Cheers,
-Brian

--
_______________________________________________________
Brian "Krow" Aker, brian at tangent.org
Seattle, Washington
http://krow.net/ <-- Me
http://tangent.org/ <-- Software
_______________________________________________________
You can't grep a dead tree.

Brian Rue

unread,
Dec 3, 2008, 4:50:14 AM12/3/08
to memc...@googlegroups.com
I'm not sure about libmemcached so I can't answer the original question, but to get the pecl php client and the python-memcached to work together (that's what it looks like you're using), change your python code to this:

<code>
import memcache
import binascii

m = memcache.Client(['192.168.28.7:11211', '192.168.28.8:11211', '192.168.28.9:11211'])

def php_hash(key):
    return (binascii.crc32(key) >> 16) & 0x7fff

for i in range(30):
       key = 'key' + str(i)
       a = m.get((php_hash(key), key))
       print i, a
</code>

(this assumes that php is set up with the defaults--standard hashing, crc32)

For whatever reason, the pecl php client doesn't use a straight crc32 and instead does what's in the php_hash function above. Calculating the hash value yourself in python and passing a tuple of (hash_value, key) to get() (or set, delete, etc) does the trick. You could also subclass memcache.Client, I suppose.

Hope this helps,

Brian Rue

ruturajv

unread,
Dec 3, 2008, 5:14:49 AM12/3/08
to memcached
hey!!!
Worked fine !
thanks a lot

On Dec 3, 2:50 pm, "Brian Rue" <brian...@gmail.com> wrote:
> I'm not sure about libmemcached so I can't answer the original question, but
> to get the pecl php client and the python-memcached to work together (that's
> what it looks like you're using), change your python code to this:
> <code>
> import memcache
> import binascii
> m = memcache.Client(['192.168.28.7:11211', '192.168.28.8:11211
> ', '192.168.28.9:11211'])
>
> def php_hash(key):
>     return (binascii.crc32(key) >> 16) & 0x7fff
>
> for i in range(30):
>        key = 'key' + str(i)
>        a = m.get((php_hash(key), key))
>        print i, a
> </code>
>
> (this assumes that php is set up with the defaults--standard hashing, crc32)
>
> For whatever reason, the pecl php client doesn't use a straight crc32 and
> instead does what's in the php_hash function above. Calculating the hash
> value yourself in python and passing a tuple of (hash_value, key) to get()
> (or set, delete, etc) does the trick. You could also subclass
> memcache.Client, I suppose.
>
> Hope this helps,
>
> Brian Rue
>
> > > Sent from my BlackBerry(R) wireless device
Reply all
Reply to author
Forward
0 new messages