12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package mysqlpool
- import (
- "sync"
- "golang-fave/engine/sqlw"
- )
- type MySqlPool struct {
- sync.RWMutex
- connections map[string]*sqlw.DB
- }
- func New() *MySqlPool {
- r := MySqlPool{}
- r.connections = map[string]*sqlw.DB{}
- return &r
- }
- func (this *MySqlPool) Get(key string) *sqlw.DB {
- this.Lock()
- defer this.Unlock()
- if value, ok := this.connections[key]; ok == true {
- return value
- }
- return nil
- }
- func (this *MySqlPool) Set(key string, value *sqlw.DB) {
- this.Lock()
- defer this.Unlock()
- this.connections[key] = value
- }
- func (this *MySqlPool) CloseAll() {
- this.Lock()
- defer this.Unlock()
- for _, c := range this.connections {
- if c != nil {
- c.Close()
- }
- }
- }
|