I am trying to share a Memcache key between Python and PHP. Python writes the key and PHP reads it. I am using the Cakephp framework, with php-pecl-memcache (not php-pecl-memcached), and the python-memcache all python library.
mc = memcache.Client( ["127.0.0.1:11211"]) key = "key1" value = 1323779849 mc.set(key, value)
echo Cache::read('key1', 'memcached');
PHP can't read the variable, I get weird "MemcachePool::get() [http://php.net/memcachepool.get]: Failed to uncompress data" errors; I suspect it has to do with memcached flags that are set differently in each library.
This is what happens when I telnet to memcached:
Python sets key:
get key1 VALUE key1 1 12 1323779849 . END
PHP sets key:
get key1 VALUE key 1 0 12 1323779849 END<ul><li>Is there a way to override these flags and just 'do your own thing'?</li> <li>Are there php/python memcache libraries that play better together?</li> <li>Alternatively, is there a simple way to create a common memory space between python/php?</li> </ul>Answer1:
Finally got it to work. Lot's of stuff wasn't working as expected.<ol><li>
One problem is that php and python use different flags to do different things. Not a problem in an all-python or all-php solution, but for inter-environment communication a real problem. A useful resource is <a href="http://www.hjp.at/zettel/m/memcached_flags.rxml" rel="nofollow">http://www.hjp.at/zettel/m/memcached_flags.rxml</a>, which shows that python-memcache flags long integer as '2', which php-memcache does not understand, hence the <em>compression error</em>. I amended python-memcache to include a 'flag-override' in the set function. This variable simply forces a particular flag irrespective of what python memcache thinks it ought to be. This allowed me to re-flag Int from 2 to 0. I will prob branch the current version of python-memcache and submit it to Github. This allowed me to force the python long int flag (2) to something php would understand (0).</li> <li>
CakePhp prior to 1.3.3 stores all keys in memcached with an additional key_expires key, flagged as 768, etc, etc. Without this additional key it cannot find the key you are looking for. Thankfully this behaviour was dumped in later Cakephp version (I simply upgraded to 1.3.13) and it works well now.</li> </ol>Answer2:
When you put something via python memcached, it's probably pickled. So PHP cannot unpickle it. I would try to use some kind of very basic type maybe ctypes? Maybe raw strings?