与 PHP 和 Python 共享 Memcache
我正在尝试在 Python 和 PHP 之间共享 Memcache 密钥。 Python 写入密钥,PHP 读取它。我正在使用 Cakephp 框架,带有 php-pecl-memcache (不是 php-pecl-memcached),以及 python-memcache 所有 python 库。
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 标志有关。
这就是当我 telnet 到 memcached 时发生的情况:
Python 设置密钥:
get key1
VALUE key1 1 12
1323779849
.
END
PHP 设置密钥:
get key1
VALUE key 1 0 12
1323779849
END
- 有没有办法覆盖这些标志并只是“做你自己的事情”?
- 是否有 php/python memcache 库可以更好地协同工作?
- 或者,是否有一种简单的方法可以在 python/php 之间创建公共内存空间?
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 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
- Is there a way to override these flags and just 'do your own thing'?
- Are there php/python memcache libraries that play better together?
- Alternatively, is there a simple way to create a common memory space between python/php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于开始工作了。很多东西没有按预期工作。
一个问题是 php 和 python 使用不同的标志来执行不同的操作。
在全 python 或全 php 解决方案中不是问题,但对于环境间通信来说是一个真正的问题。有用的资源是 http://www.hjp.at/zettel/m/memcached_flags.rxml ,这表明 python-memcache 将长整型标记为“2”,而 php-memcache 无法理解这一点,因此出现压缩错误。我修改了 python-memcache 以在 set 函数中包含“标志覆盖”。这个变量只是强制使用特定的标志,而不管 python memcache 认为它应该是什么。这使我能够将 Int 从 2 重新标记为 0。我将对 python-memcache 的当前版本进行 prob 分支并将其提交到 Github。这允许我将 python long int 标志 (2) 强制设置为 php 可以理解的值 (0)。
1.3.3 之前的 CakePhp 将所有密钥存储在 memcached 中,并带有一个附加的 key_expires 键,标记为 768 等。没有这个附加密钥,它无法找到您正在查找的密钥。值得庆幸的是,这种行为在后来的 Cakephp 版本中被转储了(我只是升级到 1.3.13),现在运行良好。
Finally got it to work. Lot's of stuff wasn't working as expected.
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).
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.
当您通过 python memcached 放置某些内容时,它可能已被腌制。所以 PHP 无法解封它。我会尝试使用某种非常基本的类型,也许是 ctypes?也许原始字符串?
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?
我有一个类似的问题,使用 PHP 和 pymemcache。我使用 https://www.php2python.com/wiki/function.serialize/ 序列化了 Python 字典 并将其写入内存缓存。 PHP端也有自己的存储到memcache的方式,并且PHP和Python编写的memcache值似乎是相同的,但是PHP无法正确读取Python设置值,所以这让我很困惑。 PHP 将其读取为字符串,无法将其反序列化/将其转换为数组。然后我必须使用 netcat 读取 memcache 值,如下所示:
Python set value returned:
,而 PHP set value had:
不知道如何处理这些标志,我只是使用这个 - 在 PHP 方面,如果我得到一个字符串“a :{s:6..." 而不是数组,我使用 PHP 的 unserialize() 方法将其设为数组并且它有效。
I had a similar problem, using PHP and pymemcache. I serialized Python dictionary with https://www.php2python.com/wiki/function.serialize/ and wrote that to memcache. PHP side also had it's own way of storing into memcache, and memcache values written by PHP and Python seemed to be the same, but PHP couldn't read Python set value properly, so it puzzled me a lot. PHP read it as a String, being unable to deserialize it / convert it to an Array. Then I got to read memcache values using netcat, like this:
Python set value returned:
, while PHP set value had:
Not knowing how to deal with these flags, I simply used this - on PHP side, if I got a String "a:{s:6..." instead of an Array, I used PHP's unserialize() method to make it an Array and it worked.