You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
4 years ago
|
package db
|
||
|
|
||
|
import (
|
||
3 years ago
|
"Open_IM/pkg/common/config"
|
||
3 years ago
|
"github.com/garyburd/redigo/redis"
|
||
4 years ago
|
"gopkg.in/mgo.v2"
|
||
3 years ago
|
"time"
|
||
4 years ago
|
)
|
||
|
|
||
|
var DB DataBases
|
||
|
|
||
|
type DataBases struct {
|
||
3 years ago
|
MysqlDB mysqlDB
|
||
|
mgoSession *mgo.Session
|
||
|
redisPool *redis.Pool
|
||
4 years ago
|
}
|
||
|
|
||
|
func key(dbAddress, dbName string) string {
|
||
|
return dbAddress + "_" + dbName
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
3 years ago
|
//mysql init
|
||
3 years ago
|
initMysqlDB()
|
||
3 years ago
|
mgoDailInfo := &mgo.DialInfo{
|
||
|
Addrs: config.Config.Mongo.DBAddress,
|
||
|
Direct: config.Config.Mongo.DBDirect,
|
||
|
Timeout: time.Second * time.Duration(config.Config.Mongo.DBTimeout),
|
||
|
Database: config.Config.Mongo.DBDatabase,
|
||
|
Source: config.Config.Mongo.DBSource,
|
||
|
Username: config.Config.Mongo.DBUserName,
|
||
|
Password: config.Config.Mongo.DBPassword,
|
||
|
PoolLimit: config.Config.Mongo.DBMaxPoolSize,
|
||
|
}
|
||
|
mgoSession, err := mgo.DialWithInfo(mgoDailInfo)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
DB.mgoSession = mgoSession
|
||
|
DB.mgoSession.SetMode(mgo.Monotonic, true)
|
||
3 years ago
|
c := DB.mgoSession.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
|
err = c.EnsureIndexKey("uid")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
3 years ago
|
|
||
|
// redis pool init
|
||
|
DB.redisPool = &redis.Pool{
|
||
|
MaxIdle: config.Config.Redis.DBMaxIdle,
|
||
|
MaxActive: config.Config.Redis.DBMaxActive,
|
||
|
IdleTimeout: time.Duration(config.Config.Redis.DBIdleTimeout) * time.Second,
|
||
|
Dial: func() (redis.Conn, error) {
|
||
|
return redis.Dial(
|
||
|
"tcp",
|
||
|
config.Config.Redis.DBAddress,
|
||
|
redis.DialReadTimeout(time.Duration(1000)*time.Millisecond),
|
||
|
redis.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
|
||
|
redis.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
|
||
|
redis.DialDatabase(0),
|
||
|
redis.DialPassword(config.Config.Redis.DBPassWord),
|
||
|
)
|
||
|
},
|
||
|
}
|
||
4 years ago
|
}
|