forked from panjjo/ppp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
104 lines (86 loc) · 2.13 KB
/
mongodb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package db
import (
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// MgoConn mongodb的连接实例
type MgoConn struct {
conn *mgo.Session
_db *mgo.Database
pool *Pool
t time.Time
}
// Ping 实现接口
func (c *MgoConn) Ping() error {
return c.conn.Ping()
}
// Close 实现接口
func (c *MgoConn) Close() error {
c.pool.put(c)
return nil
}
// p 实现接口
func (c *MgoConn) p(pool *Pool) {
c.pool = pool
}
// st 实现接口
func (c *MgoConn) setTime(t time.Time) {
c.t = t
}
// st 实现接口
func (c *MgoConn) time() time.Time {
return c.t
}
// FindOne 实现查询接口
func (c MgoConn) FindOne(tb string, query interface{}, res interface{}) interface{} {
c._db.C(tb).Find(query).One(res)
return res
}
// Update 实现查询接口
func (c MgoConn) Update(tb string, query, update interface{}) (err error) {
return c._db.C(tb).Update(query, update)
}
// UpSert 实现有就更新,没有新增
func (c MgoConn) UpSert(tb string, query, update interface{}) (change interface{}, err error) {
return c._db.C(tb).Upsert(query, update)
}
// UpAll 实现有就更新,没有新增
func (c MgoConn) UpAll(tb string, query, update interface{}) (change interface{}, err error) {
return c._db.C(tb).UpdateAll(query, bson.M{"$set": update})
}
// Save 实现保存接口
func (c MgoConn) Save(tb string, data interface{}) error {
return c._db.C(tb).Insert(data)
}
var mongoSession *mgo.Session
//NewMgoConnection 生成mongo 连接
func NewMgoConnection(config *Config) (Conn, error) {
if mongoSession == nil {
session, err := mgo.Dial(config.Addr)
if err != nil {
return nil, err
}
mongoSession = session
}
tmpsess := mongoSession.Clone()
return &MgoConn{
conn: tmpsess,
_db: tmpsess.DB(config.DB),
t: time.Now(),
}, nil
}
// GetPool 生成连接池
func GetPool(config *Config) *Pool {
return &Pool{
// 更换数据库类型请替换 NewMgoConnection
Dial: func() (Conn, error) { return NewMgoConnection(config) },
TestOnBorrow: func(c Conn) error {
return c.Ping()
},
MaxIdle: config.MaxActive,
MaxActive: config.MaxActive,
IdleTimeout: 240 * time.Second,
Wait: true,
}
}