Skip to content

Commit

Permalink
1.加入信息打印开关,2.更新readme
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Sep 16, 2020
1 parent f9372fe commit 66f0c9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ go get github.com/keepchen/message-queue/queue
import "github.com/keepchen/message-queue/queue"

instance := queue.GetDBInstance("db1")

# 关闭打印信息
instance = instance.SetDebugMode(false)
```

* 向左侧追加
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func benchmark(ops int) {
benchmarkLPop(ops)
benchmarkRPop(ops)

// benchmarkPushAndPop(ops)
benchmarkPushAndPop(ops)
}

func benchmarkLPush(ops int) {
Expand Down
37 changes: 28 additions & 9 deletions queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DBSet struct {
//Instance 实例
type Instance struct {
dbName string
debug bool
}

var pool *DBSet
Expand All @@ -37,7 +38,13 @@ func GetDBInstance(dbName string) *Instance {
//当前库不存在,先创建
(*pool.db)[dbName] = &queue{}
}
return &Instance{dbName: dbName}
return &Instance{dbName: dbName, debug: true}
}

//SetDebugMode 设置调试模式
func (instance *Instance) SetDebugMode(isDebug bool) *Instance {
instance.debug = isDebug
return instance
}

//LPush 向队列头部添加节点
Expand All @@ -47,7 +54,9 @@ func (instance *Instance) LPush(val interface{}) error {

list, err := (*pool.db)[instance.dbName].list.lPush(val)
if err != nil {
log.Printf("db {%s} [LPush] error: %#v\n", instance.dbName, err)
if instance.debug {
log.Printf("db {%s} [LPush] error: %#v\n", instance.dbName, err)
}
return err
}

Expand All @@ -64,7 +73,9 @@ func (instance *Instance) LPop() (interface{}, error) {

list, val, err := (*pool.db)[instance.dbName].list.lPop()
if err != nil {
log.Printf("db {%s} [LPop] error: %#v\n", instance.dbName, err)
if instance.debug {
log.Printf("db {%s} [LPop] error: %#v\n", instance.dbName, err)
}
return nil, err
}

Expand All @@ -81,7 +92,9 @@ func (instance *Instance) RPush(val interface{}) error {

list, err := (*pool.db)[instance.dbName].list.rPush(val)
if err != nil {
log.Printf("db {%s} [RPush] error: %#v\n", instance.dbName, err)
if instance.debug {
log.Printf("db {%s} [RPush] error: %#v\n", instance.dbName, err)
}
return err
}

Expand All @@ -98,7 +111,9 @@ func (instance *Instance) RPop() (interface{}, error) {

list, val, err := (*pool.db)[instance.dbName].list.rPop()
if err != nil {
log.Printf("db {%s} [RPop] error: %#v\n", instance.dbName, err)
if instance.debug {
log.Printf("db {%s} [RPop] error: %#v\n", instance.dbName, err)
}
return nil, err
}

Expand Down Expand Up @@ -158,14 +173,18 @@ func (instance *Instance) DisplayQueue() {
//instance.LPush(1) # <- 此处将会panic
func (instance *Instance) FlushDB() error {
pool.mu.Lock()
defer pool.mu.Unlock()
defer func() {
pool.mu.Unlock()
//销毁实例
instance = (*Instance)(nil)
}()

if _, ok := (*pool.db)[instance.dbName]; ok {
log.Printf("db {%s} will be delete\n", instance.dbName)
if instance.debug {
log.Printf("db {%s} will be delete\n", instance.dbName)
}
delete(*pool.db, instance.dbName)
return nil
}
//销毁实例
instance = nil
return fmt.Errorf("db {%s} not exist", instance.dbName)
}

0 comments on commit 66f0c9f

Please sign in to comment.