没有Marshal的缓存第二部分:通往MessagePack的道路

In part one of Caching Without Marshal, we dove into the internals of Marshal, Ruby’s built-in binary serialization format. Marshal is the black box that Rails uses under the hood to transform almost any object into binary data and back. Caching, in particular, depends heavily on Marshal: Rails uses it to cache pretty much everything, be it actions, pages, partials, or anything else.

《没有Marshal的缓存》第一部分中,我们深入了解了Marshal的内部情况,即Ruby的内置二进制序列化格式。Marshal是Rails使用的黑匣子,用于将几乎所有对象转化为二进制数据并返回。缓存,特别是,在很大程度上依赖于Marshal。Rails用它来缓存几乎所有的东西,无论是动作、页面、部分还是其他东西。

Marshal’s magic is convenient, but it comes with risks. Part one presented a deep dive into some of the little-documented internals of Marshal with the goal of ultimately replacing it with a more robust cache format. In particular, we wanted a cache format that would not blow up when we shipped code changes.

Marshal的魔力很方便,但也有风险。第一部分介绍了对Marshal内部的一些小细节的深入研究,目的是为了最终用一种更强大的缓存格式来取代它。特别是,我们希望有一种缓存格式,当我们修改代码时,它不会被炸毁。

Part two is all about MessagePack, the format that did this for us. It’s a binary serialization format, and in this sense it’s similar to Marshal. Its key difference is that whereas Marshal is a Ruby-specific format, MessagePack is generic by default. There are MessagePack libraries for Java, Python, and many other languages.

第二部分是关于MessagePack,这个格式为我们做到了这一点。它是一种二进制序列化格式,在这个意义上它与Marshal类似。它的关键区别在于,Marshal是一种Ruby特有的格式,而MessagePack默认是通用的。有用于Java、Python和许多其他语言的MessagePack库。

You may not know MessagePack, but if you’re using Rails chances are you’ve got it in your Gemfile because it’s a dependency of Bootsnap.

你可能不知道MessagePack,但如果你在使用Rails,你的Gemfile中很可能有它,因为它是Bootsnap的一个依赖项。

The MessagePack Format

消息包格式

On the surface, MessagePack is similar to Marshal: just replace .dump with .pack and .load with .unpack. For many payloads, the two are interchangeable.

表面上看,MessagePack与Marshal类似:只需用.pack 替换.dump ,用.load 替换.unpack 。对于许多有效载荷,两者是可以互换的。

Here’s an example of using MessagePack to encode and decode a hash:

下面是一个使用MessagePack来编码和解码哈希值的例子。

MessagePack supports a set of core types that ...

开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.123.4. UTC+08:00, 2024-04-19 13:42
浙ICP备14020137号-1 $访客地图$