Skip to content

Instantly share code, notes, and snippets.

@scuzhanglei
scuzhanglei / struct.go
Last active November 8, 2019 06:11 — forked from jsmouret/struct.go
Convert map[string]interface{} to a google.protobuf.Struct
package pb
import (
"fmt"
"reflect"
st "github.com/golang/protobuf/ptypes/struct"
)
// ToStruct converts a map[string]interface{} to a ptypes.Struct

给 Rails3 项目添加简单的主从数据库分离访问

特别感谢:我实现的主从分离,基于 tumayun ( https://github.com/tumayun )的 master_slave ( https://github.com/tumayun/master_slave )项目。该项目是 rails 4 的。由于 Rails4 和 Rails3 在数据库访等方面的接口变化,导致该项目不适合 rails3,因此我将其改造成 rails3,并写了这篇笔记分析其实现。

( 我的Blog http://blog.yuaz.net 暂时无法无法创建文章,因此将这篇笔记贴在这里)

默认情况下,Rails 项目仅支持一个数据库访存。当出现数据库访问瓶颈时,就要考虑主从数据库分离:在主库上写,在从库上读。只要从库的延迟在可接受的范围内,这样的分离可以大大提高性能。本文分析Rails的源码,介绍添加从库的方式。

在程序上,如何访问从库?