@@ -20,6 +20,7 @@ type DBSet struct {
20
20
//Instance 实例
21
21
type Instance struct {
22
22
dbName string
23
+ debug bool
23
24
}
24
25
25
26
var pool * DBSet
@@ -37,7 +38,13 @@ func GetDBInstance(dbName string) *Instance {
37
38
//当前库不存在,先创建
38
39
(* pool .db )[dbName ] = & queue {}
39
40
}
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
41
48
}
42
49
43
50
//LPush 向队列头部添加节点
@@ -47,7 +54,9 @@ func (instance *Instance) LPush(val interface{}) error {
47
54
48
55
list , err := (* pool .db )[instance .dbName ].list .lPush (val )
49
56
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
+ }
51
60
return err
52
61
}
53
62
@@ -64,7 +73,9 @@ func (instance *Instance) LPop() (interface{}, error) {
64
73
65
74
list , val , err := (* pool .db )[instance .dbName ].list .lPop ()
66
75
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
+ }
68
79
return nil , err
69
80
}
70
81
@@ -81,7 +92,9 @@ func (instance *Instance) RPush(val interface{}) error {
81
92
82
93
list , err := (* pool .db )[instance .dbName ].list .rPush (val )
83
94
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
+ }
85
98
return err
86
99
}
87
100
@@ -98,7 +111,9 @@ func (instance *Instance) RPop() (interface{}, error) {
98
111
99
112
list , val , err := (* pool .db )[instance .dbName ].list .rPop ()
100
113
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
+ }
102
117
return nil , err
103
118
}
104
119
@@ -158,14 +173,18 @@ func (instance *Instance) DisplayQueue() {
158
173
//instance.LPush(1) # <- 此处将会panic
159
174
func (instance * Instance ) FlushDB () error {
160
175
pool .mu .Lock ()
161
- defer pool .mu .Unlock ()
176
+ defer func () {
177
+ pool .mu .Unlock ()
178
+ //销毁实例
179
+ instance = (* Instance )(nil )
180
+ }()
162
181
163
182
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
+ }
165
186
delete (* pool .db , instance .dbName )
166
187
return nil
167
188
}
168
- //销毁实例
169
- instance = nil
170
189
return fmt .Errorf ("db {%s} not exist" , instance .dbName )
171
190
}
0 commit comments