共计 2633 个字符,预计需要花费 7 分钟才能阅读完成。
案例
例如,有个 GET
接口,可以批量获取用户信息👇
> curl 'http://localhost:8080/user/1,2'
[
{
"user_id":1,
"other_suff":...
},
{
"user_id":2,
"other_suff":...
}
]
同时,我们要将用户信息和他们的某些订单信息放在一起,组装成为👇的接口,满足其他业务需求。
[
{
"user_info":{
"user_id":1,
"other_suff":...
},
"order_info":{
"order_id":1,
"user_id":1,
"other_suff":...
}
},
{
"user_info":{
"user_id":2,
"other_suff":...
},
"order_info":{
"order_id":2,
"user_id":2,
"other_suff":...
}
}
]
分析
解决这个问题很简单:把 user 信息和 order 信息的 json 用工具解析得到结构体,然后调用他们的接口得到数据,根据 id 关联和拼装,最后返回。
这样的做法存在的一个问题是,代码解析了 user 和 order 的完整结构。如果 user 接口返回的用户信息增加了字段,我们这里的结构体要同步更新,否则我们给出的数据就是不完整的。(这可能是很痛苦的,你要求别的团队加字段,得排期 …)
其实我们作为数据的“中间商”,只关心 user 接口 json 里的 user_id
,我们使用这个字段关联 order 数据。对于 user 信息里的 other_suff
或者其他数据,我们并不关心,只要保证完整传出去就好了。
根据 https://golang.org/pkg/encodi…,可以知道直接丢一个 map[string]interface{}
给 json.Unmarshal
也可以正常解析的,于是我们可以写出比较通用的透传代码。
type Content []map[string]interface{}
func (c Content) GetByFieldName(name string, defaultVal interface{}) infterface{} {
for _, item := range c {val, ok := item[name]
if !ok {continue}
if val == nil {return defaultVal}
return val
}
return defaultVal
}
func getUserContentByIDs(ids []int) Content {
...
var c Content
err := json.Unmarshal(jsonData, &c)
...
return c
}
func getOrderContentByUserIDs(ids []int) Content {.../* 同上 */}
func Handler(userIDs []int) []Combine {users := getUserContentByIDs(userIDs)
orders := getOrderContentByUserIDs(userIDs)
// 这里假设用户和订单是一对一的关系
ret := make([]Combine, 0, len(users))
for _, u := range users {userID := u.GetByFieldName("user_id", 0)
if userID == 0 {continue}
for _, o := range orders {orderUserID := o.GetByFieldName("user_id", 0)
if userID == orderUserID {
ret = append(ret, Combine{
UserInfo: u,
OrderInfo: o,
})
break
}
}
}
return ret
}
更进一步
- 在上面的例子中,每次查询 Content 都要遍历数组。如果数据量大或者查询频繁,可以在初始化 Content 的时候,根据 item 的唯一标标识,再给 Content 根据封装一个 map,提高查询效率。
- 从 json 包的文档可以看到,对于嵌套的结构,用
interface{}
解析以后就是嵌套的map[string]interface{}
, 例如
{
"foo": "a",
"bar": {
"hello": "world",
"good": "morning"
}
}
如果用 map[string]interface{}
解析,得到的是 map 是
map[string]interface{} {
"foo": "a",
"bar": map[string]interface{} {
"hello": "world",
"good": "morning"
}
}
性能测试
Mackbook Pro 2015 Mid
2.2 GHz Intel Core i7
16 GB 1600 MHz DDR3
package main
import (
"encoding/json"
"github.com/tidwall/gjson"
"math/rand"
"strconv"
"testing"
"time"
)
type UserInfo struct {
ID int64 `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int64 `json:"age"`
Sex string `json:"sex"`
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
Department string `json:"department"`
Salary string `json:"salary"`
Comment string `json:"comment"`
}
const testAmount = 1000
var testByte []byte
func TestMain(m *testing.M) {users := make([]UserInfo, testAmount)
for i := 0; i
结果
const testAmount = 10const testAmount = 100
const testAmount = 1000
结论:使用 map 解析的性能,比完全解析的性能会差一些,大概慢了 50%,内存多了 100% 以上。另外,使用 github.com/tidwall/gjson
的通用 json 查询包,在数据量少的时候性能优异,但随着数据量变大,速度急剧下降,内存占用上升不多。