简体   繁体   English

将golang map [string]接口存储到数据存储中的最佳方法

[英]Best way to store golang map[string]interface into data store

Scenario : I am having a arbitrary JSON size ranging from 300 KB - 6.5 MB read from MongoDB. 场景 :我从MongoDB读取的JSON大小介于300 KB-6.5 MB之间。 Since it is very much arbitrary/dynamic data I cannot have struct type defined in golang, So I am using map[sting]interface{} type. 由于它是非常任意/动态的数据,因此我无法在golang中定义结构类型,因此我正在使用map[sting]interface{}类型。 And string of JSON data is parsed by using encoding/json 's Unmarshal method. JSON数据字符串通过使用encoding/jsonUnmarshal方法进行解析。 Some what similar to what is mentioned in Generic JSON with interface{} . 一些类似于与interface {}的通用JSON中提到的内容。

Issue : But the problem is its taking more time (around 30ms to 180ms) to parse the string json into map[string]interface{} . 问题 :但是问题在于,将字符串json解析为map[string]interface{}花费更多的时间(大约30ms至180ms)。 (Comparing to php parsing json using json_encode/decode / igbinary/ msgpack) (与使用json_encode / decode / igbinary / msgpack解析json的php相比)

Question : Is there any way to pre-process it and store in cache? 问题 :有什么方法可以对其进行预处理并存储在缓存中?

I mean parse string into map[string]interface{} and serialize it and store to some cache, then when we retrieve it should not take much time to unserialization and proceed with execution. 我的意思是将字符串解析为map[string]interface{}并将其序列化并存储到某个缓存中,然后在我们检索它时,不需要花费很多时间进行反序列化并继续执行。

Note: I am Newbie for the golang any suggestion are highly appriciated. 注意:对于Golang,我是新手,任何建议都应予以高度重视。 Thanks 谢谢

Updates : Serialization using Gob , binary built-in package & Msgpack implementation for Golang package are already tried. 更新 :已尝试使用Gob序列化, binary内置软件包和Msgpack对Golang软件包的实现 No luck, No improvement in the time to unserialization. 没有运气,没有序列化的时间没有改善。

The standard library package for JSON is notoriously slow. 众所周知,JSON的标准库包非常慢。 There is a good reason for that: it use RTTI to provide a really flexible interface that is really simple. 这样做有一个很好的理由:它使用RTTI提供了非常简单的非常灵活的界面。 Hence the unmarshalling being slower than PHP's… 因此,解组比PHP的慢。

Fortunately, there is an alternative way, which is to implement the json.Unmarshaller interface on the types you want to use. 幸运的是,还有另一种方法,可以在要使用的类型上实现json.Unmarshaller接口。 If this interface is implemented, the package will use it instead of its standard method so you can see huge performance boosts. 如果实现了此接口,则程序包将使用它而不是其标准方法,因此您可以看到性能的极大提高。

And to help you, there is a small group of tools that appeared, among which: 为了帮助您,这里出现了一小组工具,其中包括:

(listing here the main players from memory, there must be others) (这里列出了内存中的主要参与者,肯定还有其他参与者)

These tools will generate tailored implementations of the json.Unmarshaller interface for the types you requested. 这些工具将为您请求的类型生成json.Unmarshaller接口的定制实现。 And with go:generate , you can even integrate them seamlessly to your build step. 使用go:generate ,您甚至可以将它们无缝集成到构建步骤中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM