我有一个 Rails 应用程序,它通过 Dalli gem (https://github.com/mperham/dalli) 在 memcached 中缓存数据。
我想从 Node.js 读取这个缓存的内容。我正在使用 mc 模块与 Node.js 中的 memcached 进行交互。
我遇到的问题是编码和压缩。 Dalli 使用 Zlib::Deflate.deflate(data)
(https://github.com/mperham/dalli/blob/master/lib/dalli/compressor.rb)。当我尝试从 Node.js 膨胀时,我在尝试使用 zlib 模块膨胀时遇到错误:
{ [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }
这里是相关的 Ruby/Rails 代码:
config.cache_store = :dalli_store, memcached_server, {compress: true}
以及相关的 Node.js 代码:
client = new Memcached.Client(MEMCACHED_HOSTNAME, Memcached.Adapter.raw);
client.get(key, function (err, response) {
var data = response[key];
zlib.inflate(data.buffer, function (err, buf) {
console.log(err, buf);
});
});
从 memcached 的字符串值返回的缓冲区如下所示:
'\u0004\b[\u0015i\u0006i\u0007i\bi\ti\ni\u000bi\fi\ri\u000ei\u000fi\u0010i\u0011i\u0012i\u0014i\u0015i\u0016'
膨胀后我期望的值类似于:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17]
最佳答案
上面没有提到的重要细节:dalli 默认使用 ruby 原生编码来序列化值。
如果您想使用多种语言的 memcached 值,请考虑使用不同的 serializer
,请参阅配置文档 https://github.com/petergoldstein/dalli#configuration
关于ruby-on-rails - 如何在 Node.js 中解码/解压缩 memcached 支持的 Rails 缓存(Dalli gem)中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943875/