Skip to content

Commit db562e6

Browse files
committed
fix cache init
1 parent e2a1a1d commit db562e6

3 files changed

Lines changed: 129 additions & 113 deletions

File tree

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,8 @@ import (
6060
_ "github.com/go-sql-driver/mysql"
6161
)
6262
63-
var dbFile = conf.RootDir() + "/etc/db.cfg"
64-
6563
func init() {
66-
if err := qsql.RegCacheWithIni(dbFile, "main"); err != nil{
67-
panic(err)
68-
}
69-
if err := qsql.RegCacheWithIni(dbFile, "log"); err != nil{
70-
panic(err)
71-
}
64+
qsql.RegCacheWithIni(conf.RootDir() + "/etc/db.cfg")
7265
}
7366
```
7467

cache.go

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package qsql
22

33
import (
4+
"os"
45
"sync"
6+
"time"
7+
8+
"github.com/go-ini/ini"
9+
"github.com/gwaylib/errors"
510
)
611

712
var (
8-
cacheLock = sync.Mutex{}
9-
cache = map[string]*DB{}
13+
cacheLock = sync.Mutex{}
14+
cacheIniPath string
15+
cache = map[string]*DB{}
1016
)
1117

18+
func setCacheIni(iniPath string) {
19+
cacheLock.Lock()
20+
defer cacheLock.Unlock()
21+
cacheIniPath = iniPath
22+
}
23+
1224
func regCache(key string, db *DB) {
1325
cacheLock.Lock()
1426
defer cacheLock.Unlock()
@@ -19,11 +31,21 @@ func regCache(key string, db *DB) {
1931
cache[key] = db
2032
}
2133

22-
func getCache(key string) (*DB, bool) {
34+
func getCache(key string) (*DB, error) {
2335
cacheLock.Lock()
2436
defer cacheLock.Unlock()
2537
db, ok := cache[key]
26-
return db, ok
38+
if !ok {
39+
if len(cacheIniPath) == 0 {
40+
return nil, errors.ErrNoData.As(key)
41+
}
42+
db, err := regCacheWithIni(cacheIniPath, key)
43+
if err != nil {
44+
return nil, errors.As(err)
45+
}
46+
return db, nil
47+
}
48+
return db, nil
2749
}
2850

2951
func rmCache(src *DB) {
@@ -45,3 +67,99 @@ func closeCache() {
4567
delete(cache, key)
4668
}
4769
}
70+
71+
// ini content example
72+
//
73+
// [main]
74+
// driver: mysql
75+
// dsn: username:passwd@tcp(127.0.0.1:3306)/main?timeout=30s&strict=true&loc=Local&parseTime=true&allowOldPasswords=1
76+
// max_life_time:7200 # seconds
77+
// max_idle_time:0 # seconds
78+
// max_idle_conns:0 # num
79+
// max_open_conns:0 # num
80+
//
81+
// [log]
82+
// driver: mysql
83+
// dsn: username:passwd@tcp(127.0.0.1:3306)/log?timeout=30s&strict=true&loc=Local&parseTime=true&allowOldPasswords=1
84+
// max_life_time:7200 # seconds
85+
//
86+
func regCacheWithIni(iniFile, iniSection string) (*DB, error) {
87+
// create a new
88+
cfg, err := ini.Load(iniFile)
89+
if err != nil {
90+
return nil, errors.As(err, iniFile)
91+
}
92+
section, err := cfg.GetSection(iniSection)
93+
if err != nil {
94+
return nil, errors.As(err, iniSection)
95+
}
96+
drvName, err := section.GetKey("driver")
97+
if err != nil {
98+
return nil, errors.As(err, "not found 'driver'")
99+
}
100+
dsn, err := section.GetKey("dsn")
101+
if err != nil {
102+
return nil, errors.As(err, "not found 'dsn'")
103+
}
104+
105+
// http://techblog.en.klab-blogs.com/archives/31093990.html
106+
lifeTime := int64(0)
107+
lifeTimeKey, err := section.GetKey("max_life_time")
108+
if err == nil {
109+
lifeTime, err = lifeTimeKey.Int64()
110+
if err != nil {
111+
return nil, errors.As(err, "error max_life_time value")
112+
}
113+
}
114+
idleTime := int64(0)
115+
idleTimeKey, err := section.GetKey("max_idle_time")
116+
if err == nil {
117+
idleTime, err = idleTimeKey.Int64()
118+
if err != nil {
119+
return nil, errors.As(err, "error max_idle_time value")
120+
}
121+
}
122+
123+
idleConns := int(0)
124+
idleConnKey, err := section.GetKey("max_idle_conns")
125+
if err == nil {
126+
idleConns, err = idleConnKey.Int()
127+
if err != nil {
128+
return nil, errors.As(err, "error max_idle_conns value")
129+
}
130+
}
131+
132+
openConns := int(0)
133+
openConnKey, err := section.GetKey("max_open_conns")
134+
if err == nil {
135+
openConns, err = openConnKey.Int()
136+
if err != nil {
137+
return nil, errors.As(err, "error max_open_conns value")
138+
}
139+
}
140+
141+
_, ok := cache[iniSection]
142+
if ok {
143+
return nil, errors.New("key is already exist").As(iniSection)
144+
}
145+
146+
db, err := Open(drvName.String(), os.ExpandEnv(dsn.String()))
147+
if err != nil {
148+
return nil, errors.As(err)
149+
}
150+
if lifeTime > 0 {
151+
db.SetConnMaxLifetime(time.Duration(lifeTime) * time.Second)
152+
}
153+
if idleTime > 0 {
154+
db.SetConnMaxIdleTime(time.Duration(idleTime) * time.Second)
155+
}
156+
if idleConns > 0 {
157+
db.SetMaxIdleConns(idleConns)
158+
}
159+
if openConns > 0 {
160+
db.SetMaxOpenConns(openConns)
161+
}
162+
163+
cache[iniSection] = db
164+
return db, nil
165+
}

factory.go

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,25 @@
11
package qsql
22

3-
import (
4-
"os"
5-
"time"
6-
7-
"github.com/go-ini/ini"
8-
"github.com/gwaylib/errors"
9-
)
10-
113
// Register a db to the connection pool by manully.
124
func RegCache(key string, db *DB) {
135
regCache(key, db)
146
}
157

16-
// ini content example
17-
//
18-
// [main]
19-
// driver: mysql
20-
// dsn: username:passwd@tcp(127.0.0.1:3306)/main?timeout=30s&strict=true&loc=Local&parseTime=true&allowOldPasswords=1
21-
// max_life_time:7200 # seconds
22-
// max_idle_time:0 # seconds
23-
// max_idle_conns:0 # num
24-
// max_open_conns:0 # num
25-
//
26-
// [log]
27-
// driver: mysql
28-
// dsn: username:passwd@tcp(127.0.0.1:3306)/log?timeout=30s&strict=true&loc=Local&parseTime=true&allowOldPasswords=1
29-
// max_life_time:7200 # seconds
30-
//
31-
func RegCacheWithIni(iniFile, iniSection string) error {
32-
// create a new
33-
cfg, err := ini.Load(iniFile)
34-
if err != nil {
35-
return errors.As(err, iniFile)
36-
}
37-
section, err := cfg.GetSection(iniSection)
38-
if err != nil {
39-
return errors.As(err, iniSection)
40-
}
41-
drvName, err := section.GetKey("driver")
42-
if err != nil {
43-
return errors.As(err, "not found 'driver'")
44-
}
45-
dsn, err := section.GetKey("dsn")
46-
if err != nil {
47-
return errors.As(err, "not found 'dsn'")
48-
}
49-
50-
// http://techblog.en.klab-blogs.com/archives/31093990.html
51-
lifeTime := int64(0)
52-
lifeTimeKey, err := section.GetKey("max_life_time")
53-
if err == nil {
54-
lifeTime, err = lifeTimeKey.Int64()
55-
if err != nil {
56-
return errors.As(err, "error max_life_time value")
57-
}
58-
}
59-
idleTime := int64(0)
60-
idleTimeKey, err := section.GetKey("max_idle_time")
61-
if err == nil {
62-
idleTime, err = idleTimeKey.Int64()
63-
if err != nil {
64-
return errors.As(err, "error max_idle_time value")
65-
}
66-
}
67-
68-
idleConns := int(0)
69-
idleConnKey, err := section.GetKey("max_idle_conns")
70-
if err == nil {
71-
idleConns, err = idleConnKey.Int()
72-
if err != nil {
73-
return errors.As(err, "error max_idle_conns value")
74-
}
75-
}
76-
77-
openConns := int(0)
78-
openConnKey, err := section.GetKey("max_open_conns")
79-
if err == nil {
80-
openConns, err = openConnKey.Int()
81-
if err != nil {
82-
return errors.As(err, "error max_open_conns value")
83-
}
84-
}
85-
86-
db, err := Open(drvName.String(), os.ExpandEnv(dsn.String()))
87-
if err != nil {
88-
return errors.As(err)
89-
}
90-
if lifeTime > 0 {
91-
db.SetConnMaxLifetime(time.Duration(lifeTime) * time.Second)
92-
}
93-
if idleTime > 0 {
94-
db.SetConnMaxIdleTime(time.Duration(idleTime) * time.Second)
95-
}
96-
if idleConns > 0 {
97-
db.SetMaxIdleConns(idleConns)
98-
}
99-
if openConns > 0 {
100-
db.SetMaxOpenConns(openConns)
101-
}
102-
103-
regCache(iniSection, db)
104-
return nil
8+
func RegCacheWithIni(iniPath string) {
9+
setCacheIni(iniPath)
10510
}
10611

10712
// Get the db instance from the cache.
10813
func GetCache(key string) *DB {
109-
db, ok := getCache(key)
110-
if !ok {
111-
panic("not found db: " + key)
14+
db, err := getCache(key)
15+
if err != nil {
16+
panic(err)
11217
}
11318
return db
11419
}
11520

11621
// Checking the cache does it have a db instance.
117-
func HasCache(key string) (*DB, bool) {
22+
func HasCache(key string) (*DB, error) {
11823
return getCache(key)
11924
}
12025

0 commit comments

Comments
 (0)