11package qsql
22
33import (
4+ "os"
45 "sync"
6+ "time"
7+
8+ "github.com/go-ini/ini"
9+ "github.com/gwaylib/errors"
510)
611
712var (
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+
1224func 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
2951func 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+ }
0 commit comments