pull/4/head
away 4 years ago
parent 4c90036f2a
commit 94428bfc40

@ -1,51 +0,0 @@
package db
import (
"Open_IM/src/common/config"
"errors"
"fmt"
"gopkg.in/mgo.v2"
"sync"
"time"
)
type mongoDB struct {
sync.RWMutex
dbMap map[string]*mgo.Session
}
func (m *mongoDB) mgoSession(dbName string) *mgo.Session {
m.Lock()
defer m.Unlock()
if _, ok := m.dbMap[dbName]; !ok {
if err := m.newMgoSession(dbName); err != nil {
panic(err)
return nil
}
}
return m.dbMap[dbName]
}
func (m *mongoDB) newMgoSession(dbName string) error {
dailInfo := &mgo.DialInfo{
Addrs: config.Config.Mongo.DBAddress,
Direct: config.Config.Mongo.DBDirect,
Timeout: time.Second * time.Duration(config.Config.Mongo.DBTimeout),
Database: dbName,
Source: config.Config.Mongo.DBSource,
Username: config.Config.Mongo.DBUserName,
Password: config.Config.Mongo.DBPassword,
PoolLimit: config.Config.Mongo.DBMaxPoolSize,
}
session, err := mgo.DialWithInfo(dailInfo)
if err != nil {
return errors.New(fmt.Sprintf("mongo DialWithInfo fail, err= %s", err.Error()))
}
if m.dbMap == nil {
m.dbMap = make(map[string]*mgo.Session)
}
m.dbMap[dbName] = session
return nil
}

@ -1,52 +0,0 @@
package db
import (
"Open_IM/src/common/config"
log2 "Open_IM/src/common/log"
"github.com/garyburd/redigo/redis"
"time"
)
type redisDB struct {
pool *redis.Pool
}
func (r *redisDB) newPool() {
r.pool = &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[0],
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),
)
},
}
}
func (r *redisDB) Exec(cmd string, key interface{}, args ...interface{}) (interface{}, error) {
con := r.pool.Get()
if err := con.Err(); err != nil {
log2.Error("", "", "redis cmd = %v, err = %v", cmd, err)
return nil, err
}
defer con.Close()
params := make([]interface{}, 0)
params = append(params, key)
if len(args) > 0 {
for _, v := range args {
params = append(params, v)
}
}
return con.Do(cmd, params...)
}
Loading…
Cancel
Save