@@ -20,6 +20,7 @@ type DBSet struct {
2020//Instance 实例
2121type Instance struct {
2222 dbName string
23+ debug bool
2324}
2425
2526var pool * DBSet
@@ -37,7 +38,13 @@ func GetDBInstance(dbName string) *Instance {
3738 //当前库不存在,先创建
3839 (* pool .db )[dbName ] = & queue {}
3940 }
40- return & Instance {dbName : dbName }
41+ return & Instance {dbName : dbName , debug : true }
42+ }
43+
44+ //SetDebugMode 设置调试模式
45+ func (instance * Instance ) SetDebugMode (isDebug bool ) * Instance {
46+ instance .debug = isDebug
47+ return instance
4148}
4249
4350//LPush 向队列头部添加节点
@@ -47,7 +54,9 @@ func (instance *Instance) LPush(val interface{}) error {
4754
4855 list , err := (* pool .db )[instance .dbName ].list .lPush (val )
4956 if err != nil {
50- log .Printf ("db {%s} [LPush] error: %#v\n " , instance .dbName , err )
57+ if instance .debug {
58+ log .Printf ("db {%s} [LPush] error: %#v\n " , instance .dbName , err )
59+ }
5160 return err
5261 }
5362
@@ -64,7 +73,9 @@ func (instance *Instance) LPop() (interface{}, error) {
6473
6574 list , val , err := (* pool .db )[instance .dbName ].list .lPop ()
6675 if err != nil {
67- log .Printf ("db {%s} [LPop] error: %#v\n " , instance .dbName , err )
76+ if instance .debug {
77+ log .Printf ("db {%s} [LPop] error: %#v\n " , instance .dbName , err )
78+ }
6879 return nil , err
6980 }
7081
@@ -81,7 +92,9 @@ func (instance *Instance) RPush(val interface{}) error {
8192
8293 list , err := (* pool .db )[instance .dbName ].list .rPush (val )
8394 if err != nil {
84- log .Printf ("db {%s} [RPush] error: %#v\n " , instance .dbName , err )
95+ if instance .debug {
96+ log .Printf ("db {%s} [RPush] error: %#v\n " , instance .dbName , err )
97+ }
8598 return err
8699 }
87100
@@ -98,7 +111,9 @@ func (instance *Instance) RPop() (interface{}, error) {
98111
99112 list , val , err := (* pool .db )[instance .dbName ].list .rPop ()
100113 if err != nil {
101- log .Printf ("db {%s} [RPop] error: %#v\n " , instance .dbName , err )
114+ if instance .debug {
115+ log .Printf ("db {%s} [RPop] error: %#v\n " , instance .dbName , err )
116+ }
102117 return nil , err
103118 }
104119
@@ -158,14 +173,18 @@ func (instance *Instance) DisplayQueue() {
158173//instance.LPush(1) # <- 此处将会panic
159174func (instance * Instance ) FlushDB () error {
160175 pool .mu .Lock ()
161- defer pool .mu .Unlock ()
176+ defer func () {
177+ pool .mu .Unlock ()
178+ //销毁实例
179+ instance = (* Instance )(nil )
180+ }()
162181
163182 if _ , ok := (* pool .db )[instance .dbName ]; ok {
164- log .Printf ("db {%s} will be delete\n " , instance .dbName )
183+ if instance .debug {
184+ log .Printf ("db {%s} will be delete\n " , instance .dbName )
185+ }
165186 delete (* pool .db , instance .dbName )
166187 return nil
167188 }
168- //销毁实例
169- instance = nil
170189 return fmt .Errorf ("db {%s} not exist" , instance .dbName )
171190}
0 commit comments