我们有一个应用程序,我们使用 python 在 memcached 中存储大量数据。我们在 python 中使用 pylibmc,在 php 端我们使用 php-memcached 库。总结
- pylibmc v.1.2.3
- php-memcached v.2.0.1
- libmemcached v1.0.8。
除了压缩发挥作用外,其他一切都很好。 python中数据是这样压缩的
import pylibmc
mem = pylibmc.Client(['10.90.15.104:11211'], binary=True)
mem.set('foo','this is a rather long string. this is a rather '+
'long string. this is a rather long string. this is a rather' +
'long string. this is a rather long string', 0, 10)
在 telnet 中检查我们看到一些乱码值,这意味着它被压缩了。现在用 php 阅读它。
$memd = new Memcached();
$memd->addServer('10.90.15.104', 11211);
echo $memd->get('foo');
当上面运行时,我们得到相同的乱码值,这意味着它没有被解压缩。 pylibmc 使用的是 zlib,因此我也相应地将 php 的压缩类型更改为 zlib。还需要做什么设置?请帮忙。
为了进一步引用,这里是在 python pylibmc 中设置字符串后 memcached 的输出
get foo
VALUE foo 8 40
x+��,V�D��Ē��"����t�⒢̼t=���g\5#
END
下面是使用 PHP 的内存缓存客户端存储的字符串的内存缓存输出:
get foo
VALUE foo 48 44
�x�+��,V�D��Ē��"����t�⒢̼t=���g\5#
END
如您所见,其中有些可疑之处。 pylibmc 中的压缩大小为 40 字节,使用 php-memcached 压缩的相同数据为 44 字节。另请注意,使用 pylibmc 存储时标志为 8,使用 php-memcached 存储时标志为 48!
最佳答案
我认为您观察到的是由于 memcache 本身没有实现压缩 所以每个库都以自己的方式进行,只需比较用于指示正在使用压缩的标志
由 pylibmc 定义
#define PYLIBMC_FLAG_ZLIB (1 << 3)
(所以这是标志 == 8)
#define MEMC_VAL_COMPRESSED (1<<4)
#define MEMC_VAL_COMPRESSION_ZLIB (1<<5)
#define MEMC_VAL_COMPRESSION_FASTLZ (1<<6)
所以我认为除非您愿意修改其中一个库以使其标志与另一个库一致,否则没有出路
编辑: 好的,这是一个小补丁,它同步为 pylibmc 和 php-memcached 提供压缩支持。查看我的 github fork of pylibmc .
大脂肪交战 - 它只适用于字符串,所以如果你想存储对象,你必须自己进行反序列化(JSON)。
关于php - pylibmc 存储的数据无法在 php memcached 中解压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912530/