与PHP和Python共享Memcache [英] Sharing Memcache with PHP and Python

查看:78
本文介绍了与PHP和Python共享Memcache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python和PHP之间共享Memcache密钥. Python编写密钥,PHP读取密钥.我使用的是Cakephp框架,带有php-pecl-memcache(不是php-pecl-memcached)和python-memcache所有python库.

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.

Python:

  mc = memcache.Client( ["127.0.0.1:11211"])
  key = "key1"
  value = 1323779849
  mc.set(key, value)

PHP:

  echo Cache::read('key1', 'memcached');

PHP无法读取变量,我收到奇怪的"MemcachePool :: get()[http://php.net/memcachepool.get]:无法解压缩数据"错误;我怀疑这与在每个库中设置的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.

这是我通过telnet到memcached时发生的事情:

This is what happens when I telnet to memcached:

Python设置了密钥:

Python sets key:

 get key1
 VALUE key1 1 12
 1323779849
 .
 END

PHP设置密钥:

 get key1
 VALUE key 1 0 12
 1323779849
 END

  • 是否有一种方法可以覆盖这些标志,而只是做自己的事"?
  • 是否存在可以一起更好地发挥作用的php/python内存缓存库?
  • 或者,是否有一种简单的方法来在python/php之间创建公共内存空间?
  • 推荐答案

    最后使它起作用.很多东西没有按预期工作.

    Finally got it to work. Lot's of stuff wasn't working as expected.

    1. 一个问题是php和python使用不同的标志来执行不同的操作. 在全python或全PHP解决方案中这不是问题,但是对于环境之间的通信而言,这是一个真正的问题.有用的资源是 http://www.hjp.at/zettel/m/memcached_flags.rxml ,这表明python-memcache将长整数标记为"2",而php-memcache无法识别该整数,因此出现了压缩错误.我修改了python-memcache以在set函数中包含"flag-override".不管python memcache认为应该是什么,此变量都将强制使用特定标志.这使我可以将Int从2重新标记为0.我将对当前版本的python-memcache进行分支,并将其提交给Github.这使我可以将python long int标志(2)强制为php可以理解的(0).

    1. 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 http://www.hjp.at/zettel/m/memcached_flags.rxml, which shows that python-memcache flags long integer as '2', which php-memcache does not understand, hence the compression error. 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).

    1.3.3之前的CakePhp将所有密钥与一个附加的key_expires密钥(标记为768等)存储在memcached中.如果没有此附加密钥,它将无法找到您要查找的密钥.值得庆幸的是,此行为已在以后的Cakephp版本(我简单地升级到1.3.13)中转储了,并且现在运行良好.

    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.

    这篇关于与PHP和Python共享Memcache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆