ruby-on-rails - 如何在 Node.js 中解码/解压缩 memcached 支持的 Rails 缓存(Dalli gem)中的值

标签 ruby-on-rails ruby node.js caching zlib

我有一个 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/

相关文章:

ruby-on-rails - 结果表中的 Postgresql 总和计数

mysql - Rails has_many/belongs_to 自定义数据库 ActiveRecord::AssociationTypeMismatch 得到 Fixnum

javascript - 带 Rails 的入门级 javascript : updating a div on form submit (3. 1/jquery)

node.js - 使用 node.js 按顺序读取目录中所有文件的内容

mysql - 在 Rails 中添加默认值为空字符串的 NOT NULL 列

ruby-on-rails - ruby on rails,无法将 check_box_tag 值传回 update_attributes

ruby - 使用watir-webdriver,是否可以使用元素的一部分来测试

ruby-on-rails - 在resque队列中查找特定作业

javascript - JS中字符串中 '='后面的解析和提取

javascript - Sequelize 创建错误: Cannot read property 'length' of undefined