mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
提交origin2.0版本
This commit is contained in:
@@ -4,13 +4,16 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/originnet/log"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/originnet/service"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
@@ -20,9 +23,19 @@ const (
|
||||
MAX_EXECUTE_FUN = 10000
|
||||
)
|
||||
|
||||
type DBExecute struct {
|
||||
syncExecuteFun chan SyncFun
|
||||
syncExecuteExit chan bool
|
||||
}
|
||||
|
||||
type PingExecute struct {
|
||||
tickerPing *time.Ticker
|
||||
pintExit chan bool
|
||||
}
|
||||
|
||||
// DBModule ...
|
||||
type DBModule struct {
|
||||
service.BaseModule
|
||||
service.Module
|
||||
db *sql.DB
|
||||
url string
|
||||
username string
|
||||
@@ -30,8 +43,12 @@ type DBModule struct {
|
||||
dbname string
|
||||
maxconn int
|
||||
PrintTime time.Duration
|
||||
syncExecuteFun chan SyncFun
|
||||
|
||||
pingCoroutine PingExecute
|
||||
|
||||
syncCoroutineNum int
|
||||
executeList []DBExecute
|
||||
waitGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
// Tx ...
|
||||
@@ -72,43 +89,97 @@ type SyncDBResult struct {
|
||||
sres chan DBResult
|
||||
}
|
||||
|
||||
type SyncDBResultExCallBack func(dataList *DataSetList, err error)
|
||||
|
||||
type SyncQueryDBResultEx struct {
|
||||
sres chan *DataSetList
|
||||
err chan error
|
||||
}
|
||||
|
||||
func (slf *SyncQueryDBResultEx) Get(timeoutMs int) (*DataSetList, error) {
|
||||
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
||||
select {
|
||||
case <-timerC:
|
||||
break
|
||||
case err := <-slf.err:
|
||||
dataset := <-slf.sres
|
||||
return dataset, err
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs)
|
||||
}
|
||||
|
||||
type SyncExecuteDBResult struct {
|
||||
sres chan *DBResultEx
|
||||
err chan error
|
||||
}
|
||||
|
||||
func (slf *DBModule) OnRun() bool {
|
||||
if slf.db != nil {
|
||||
slf.db.Ping()
|
||||
|
||||
func (slf *SyncExecuteDBResult) Get(timeoutMs int) (*DBResultEx, error) {
|
||||
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
||||
select {
|
||||
case <-timerC:
|
||||
break
|
||||
case err := <-slf.err:
|
||||
dataset := <-slf.sres
|
||||
return dataset, err
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
return true
|
||||
|
||||
return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs)
|
||||
}
|
||||
func (slf *DBModule) Init(maxConn int, url string, userName string, password string, dbname string) error {
|
||||
|
||||
func (slf *DBModule) RunPing() {
|
||||
for {
|
||||
select {
|
||||
case <-slf.pingCoroutine.pintExit:
|
||||
log.Error("RunPing stopping %s...", fmt.Sprintf("%T", slf))
|
||||
return
|
||||
case <-slf.pingCoroutine.tickerPing.C:
|
||||
if slf.db != nil {
|
||||
slf.db.Ping()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *DBModule) Init(maxConn, executeNum int, url string, userName string, password string, dbname string) error {
|
||||
slf.url = url
|
||||
slf.maxconn = maxConn
|
||||
slf.username = userName
|
||||
slf.password = password
|
||||
slf.dbname = dbname
|
||||
slf.syncExecuteFun = make(chan SyncFun, MAX_EXECUTE_FUN)
|
||||
slf.syncCoroutineNum = executeNum
|
||||
|
||||
if executeNum <= 0 {
|
||||
return fmt.Errorf("executeNum mast more than zero:%d", executeNum)
|
||||
}
|
||||
|
||||
slf.executeList = []DBExecute{}
|
||||
for i := 0; i < executeNum; i++ {
|
||||
itemInfo := DBExecute{syncExecuteFun:make(chan SyncFun, MAX_EXECUTE_FUN), syncExecuteExit:make(chan bool, 1)}
|
||||
slf.executeList = append(slf.executeList, itemInfo)
|
||||
}
|
||||
slf.pingCoroutine = PingExecute{tickerPing : time.NewTicker(5*time.Second), pintExit : make(chan bool, 1)}
|
||||
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
||||
return slf.Connect(slf.maxconn)
|
||||
}
|
||||
|
||||
func (slf *DBModule) OnInit() error {
|
||||
for i := 0; i < slf.syncCoroutineNum; i++ {
|
||||
go slf.RunExecuteDBCoroutine()
|
||||
go slf.RunExecuteDBCoroutine(i)
|
||||
}
|
||||
go slf.RunPing()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *DBModule) OnRelease() {
|
||||
for i := 0; i < slf.syncCoroutineNum; i++ {
|
||||
close(slf.executeList[i].syncExecuteExit)
|
||||
}
|
||||
}
|
||||
|
||||
//Close ...
|
||||
func (slf *DBResult) Close() {
|
||||
if slf.res != nil {
|
||||
@@ -316,8 +387,6 @@ func (slf *DBModule) Connect(maxConn int) error {
|
||||
db.SetMaxIdleConns(maxConn)
|
||||
db.SetConnMaxLifetime(time.Second * 90)
|
||||
|
||||
slf.syncCoroutineNum = maxConn
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -335,19 +404,6 @@ func (slf *SyncDBResult) Get(timeoutMs int) DBResult {
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *SyncQueryDBResultEx) Get(timeoutMs int) (*DataSetList, error) {
|
||||
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
||||
select {
|
||||
case <-timerC:
|
||||
break
|
||||
case err := <-slf.err:
|
||||
dataset := <-slf.sres
|
||||
return dataset, err
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs)
|
||||
}
|
||||
|
||||
func (slf *DBModule) CheckArgs(args ...interface{}) error {
|
||||
for _, val := range args {
|
||||
if reflect.TypeOf(val).Kind() == reflect.String {
|
||||
@@ -395,20 +451,20 @@ func (slf *DBModule) CheckArgs(args ...interface{}) error {
|
||||
func (slf *DBModule) Query(query string, args ...interface{}) DBResult {
|
||||
if slf.CheckArgs(args) != nil {
|
||||
ret := DBResult{}
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
ret.Err = fmt.Errorf("CheckArgs is error!")
|
||||
return ret
|
||||
}
|
||||
|
||||
if slf.db == nil {
|
||||
ret := DBResult{}
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
ret.Err = fmt.Errorf("cannot connect database!")
|
||||
return ret
|
||||
}
|
||||
rows, err := slf.db.Query(query, args...)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%v)", query, err)
|
||||
log.Error("Query:%s(%v)", query, err)
|
||||
}
|
||||
|
||||
return DBResult{
|
||||
@@ -425,12 +481,12 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
|
||||
datasetList.blur = true
|
||||
|
||||
if slf.CheckArgs(args) != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
return &datasetList, fmt.Errorf("CheckArgs is error!")
|
||||
}
|
||||
|
||||
if slf.db == nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||
}
|
||||
|
||||
@@ -439,10 +495,10 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
|
||||
TimeFuncPass := time.Since(TimeFuncStart)
|
||||
|
||||
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||
service.GetLogger().Printf(service.LEVER_INFO, "DBModule QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
}
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%v)", query, err)
|
||||
log.Error("Query:%s(%v)", query, err)
|
||||
if rows != nil {
|
||||
rows.Close()
|
||||
}
|
||||
@@ -483,7 +539,7 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
|
||||
|
||||
if hasRet == false {
|
||||
if rows.Err() != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%+v)", query, rows)
|
||||
log.Error( "Query:%s(%+v)", query, rows)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -493,13 +549,18 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
|
||||
}
|
||||
|
||||
// SyncQuery ...
|
||||
func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncQueryDBResultEx {
|
||||
func (slf *DBModule) SyncQuery(queryHas int, query string, args ...interface{}) SyncQueryDBResultEx {
|
||||
ret := SyncQueryDBResultEx{
|
||||
sres: make(chan *DataSetList, 1),
|
||||
err: make(chan error, 1),
|
||||
}
|
||||
|
||||
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
chanIndex := queryHas % len(slf.executeList)
|
||||
if chanIndex < 0 {
|
||||
chanIndex = rand.Intn(len(slf.executeList))
|
||||
}
|
||||
|
||||
if len(slf.executeList[chanIndex].syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
dbret := DataSetList{}
|
||||
ret.err <- fmt.Errorf("chan is full,sql:%s", query)
|
||||
ret.sres <- &dbret
|
||||
@@ -507,7 +568,7 @@ func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncQueryDBRes
|
||||
return ret
|
||||
}
|
||||
|
||||
slf.syncExecuteFun <- func() {
|
||||
slf.executeList[chanIndex].syncExecuteFun <- func() {
|
||||
rsp, err := slf.QueryEx(query, args...)
|
||||
ret.err <- err
|
||||
ret.sres <- rsp
|
||||
@@ -516,16 +577,35 @@ func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncQueryDBRes
|
||||
return ret
|
||||
}
|
||||
|
||||
func (slf *DBModule) AsyncQuery(call SyncDBResultExCallBack, queryHas int, query string, args ...interface{}) error {
|
||||
chanIndex := queryHas % len(slf.executeList)
|
||||
if chanIndex < 0 {
|
||||
chanIndex = rand.Intn(len(slf.executeList))
|
||||
}
|
||||
|
||||
if len(slf.executeList[chanIndex].syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
return fmt.Errorf("chan is full,sql:%s", query)
|
||||
}
|
||||
|
||||
slf.executeList[chanIndex].syncExecuteFun <- func() {
|
||||
rsp, err := slf.QueryEx(query, args...)
|
||||
call(rsp, err)
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec ...
|
||||
func (slf *DBModule) Exec(query string, args ...interface{}) (*DBResultEx, error) {
|
||||
ret := &DBResultEx{}
|
||||
if slf.db == nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
return ret, fmt.Errorf("cannot connect database!")
|
||||
}
|
||||
|
||||
if slf.CheckArgs(args) != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
//return ret, fmt.Errorf("cannot connect database!")
|
||||
return ret, fmt.Errorf("CheckArgs is error!")
|
||||
}
|
||||
@@ -534,10 +614,10 @@ func (slf *DBModule) Exec(query string, args ...interface{}) (*DBResultEx, error
|
||||
res, err := slf.db.Exec(query, args...)
|
||||
TimeFuncPass := time.Since(TimeFuncStart)
|
||||
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||
service.GetLogger().Printf(service.LEVER_INFO, "DBModule QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
}
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err)
|
||||
log.Error("Exec:%s(%v)", query, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -548,18 +628,23 @@ func (slf *DBModule) Exec(query string, args ...interface{}) (*DBResultEx, error
|
||||
}
|
||||
|
||||
// SyncExec ...
|
||||
func (slf *DBModule) SyncExec(query string, args ...interface{}) *SyncExecuteDBResult {
|
||||
func (slf *DBModule) SyncExec(queryHas int, query string, args ...interface{}) *SyncExecuteDBResult {
|
||||
ret := &SyncExecuteDBResult{
|
||||
sres: make(chan *DBResultEx, 1),
|
||||
err: make(chan error, 1),
|
||||
}
|
||||
|
||||
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
chanIndex := queryHas % len(slf.executeList)
|
||||
if chanIndex < 0 {
|
||||
chanIndex = rand.Intn(len(slf.executeList))
|
||||
}
|
||||
|
||||
if len(slf.executeList[chanIndex].syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
ret.err <- fmt.Errorf("chan is full,sql:%s", query)
|
||||
return ret
|
||||
}
|
||||
|
||||
slf.syncExecuteFun <- func() {
|
||||
slf.executeList[chanIndex].syncExecuteFun <- func() {
|
||||
rsp, err := slf.Exec(query, args...)
|
||||
if err != nil {
|
||||
ret.err <- err
|
||||
@@ -573,19 +658,41 @@ func (slf *DBModule) SyncExec(query string, args ...interface{}) *SyncExecuteDBR
|
||||
return ret
|
||||
}
|
||||
|
||||
func (slf *DBModule) RunExecuteDBCoroutine() {
|
||||
slf.WaitGroup.Add(1)
|
||||
defer slf.WaitGroup.Done()
|
||||
func (slf *DBModule) AsyncExec(call SyncDBResultExCallBack, queryHas int, query string, args ...interface{}) error {
|
||||
chanIndex := queryHas % len(slf.executeList)
|
||||
if chanIndex < 0 {
|
||||
chanIndex = rand.Intn(len(slf.executeList))
|
||||
}
|
||||
|
||||
if len(slf.executeList[chanIndex].syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||
return fmt.Errorf("chan is full,sql:%s", query)
|
||||
}
|
||||
|
||||
slf.executeList[chanIndex].syncExecuteFun <- func() {
|
||||
rsp, err := slf.Exec(query, args...)
|
||||
|
||||
data := DataSetList{tag:"json", blur:true, dataSetList:[]DBResultEx{}}
|
||||
data.dataSetList = append(data.dataSetList, *rsp)
|
||||
call(&data, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *DBModule) RunExecuteDBCoroutine(has int) {
|
||||
slf.waitGroup.Add(1)
|
||||
defer slf.waitGroup.Done()
|
||||
for {
|
||||
select {
|
||||
case <-slf.ExitChan:
|
||||
service.GetLogger().Printf(LEVER_WARN, "stopping module %s...", fmt.Sprintf("%T", slf))
|
||||
case <-slf.executeList[has].syncExecuteExit:
|
||||
log.Error("stopping module %s...", fmt.Sprintf("%T", slf))
|
||||
return
|
||||
case fun := <-slf.syncExecuteFun:
|
||||
case fun := <-slf.executeList[has].syncExecuteFun:
|
||||
fun()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (slf *DataSetList) UnMarshal(args ...interface{}) error {
|
||||
@@ -740,7 +847,7 @@ func (slf *DBModule) Begin() (*Tx, error) {
|
||||
var txDBMoudule Tx
|
||||
txdb, err := slf.db.Begin()
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Begin error:%s", err.Error())
|
||||
log.Error("Begin error:%s", err.Error())
|
||||
return &txDBMoudule, err
|
||||
}
|
||||
txDBMoudule.tx = txdb
|
||||
@@ -805,21 +912,21 @@ func (slf *Tx) CheckArgs(args ...interface{}) error {
|
||||
func (slf *Tx) Query(query string, args ...interface{}) DBResult {
|
||||
if slf.CheckArgs(args) != nil {
|
||||
ret := DBResult{}
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
ret.Err = fmt.Errorf("CheckArgs is error!")
|
||||
return ret
|
||||
}
|
||||
|
||||
if slf.tx == nil {
|
||||
ret := DBResult{}
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
ret.Err = fmt.Errorf("cannot connect database!")
|
||||
return ret
|
||||
}
|
||||
|
||||
rows, err := slf.tx.Query(query, args...)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%v)", query, err)
|
||||
log.Error("Tx Query:%s(%v)", query, err)
|
||||
}
|
||||
|
||||
return DBResult{
|
||||
@@ -845,12 +952,12 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
|
||||
datasetList.blur = true
|
||||
|
||||
if slf.CheckArgs(args) != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
return &datasetList, fmt.Errorf("CheckArgs is error!")
|
||||
}
|
||||
|
||||
if slf.tx == nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||
}
|
||||
|
||||
@@ -859,10 +966,10 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
|
||||
TimeFuncPass := time.Since(TimeFuncStart)
|
||||
|
||||
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||
service.GetLogger().Printf(service.LEVER_INFO, "Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
log.Error("Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
}
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%v)", query, err)
|
||||
log.Error("Tx Query:%s(%v)", query, err)
|
||||
if rows != nil {
|
||||
rows.Close()
|
||||
}
|
||||
@@ -903,7 +1010,7 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
|
||||
|
||||
if hasRet == false {
|
||||
if rows.Err() != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%+v)", query, rows)
|
||||
log.Error("Tx Query:%s(%+v)", query, rows)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -916,12 +1023,12 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
|
||||
func (slf *Tx) Exec(query string, args ...interface{}) (*DBResultEx, error) {
|
||||
ret := &DBResultEx{}
|
||||
if slf.tx == nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||
log.Error("cannot connect database:%s", query)
|
||||
return ret, fmt.Errorf("cannot connect database!")
|
||||
}
|
||||
|
||||
if slf.CheckArgs(args) != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
|
||||
log.Error("CheckArgs is error :%s", query)
|
||||
//return ret, fmt.Errorf("cannot connect database!")
|
||||
return ret, fmt.Errorf("CheckArgs is error!")
|
||||
}
|
||||
@@ -930,10 +1037,10 @@ func (slf *Tx) Exec(query string, args ...interface{}) (*DBResultEx, error) {
|
||||
res, err := slf.tx.Exec(query, args...)
|
||||
TimeFuncPass := time.Since(TimeFuncStart)
|
||||
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||
service.GetLogger().Printf(service.LEVER_INFO, "Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
log.Error("Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
|
||||
}
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Tx Exec:%s(%v)", query, err)
|
||||
log.Error("Tx Exec:%s(%v)", query, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
package sysmodule_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/duanhf2012/origin/sysmodule"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestDBModule(t *testing.T) {
|
||||
db := sysmodule.DBModule{}
|
||||
db.ExitChan = make(chan bool)
|
||||
db.WaitGroup = new(sync.WaitGroup)
|
||||
|
||||
// db.Init(100, "192.168.0.5:3306", "root", "Root!!2018", "QuantFundsDB")
|
||||
db.Init(100, "127.0.0.1:3306", "root", "zgh50221", "rebort_message")
|
||||
db.OnInit()
|
||||
tx, err := db.GetTx()
|
||||
if err != nil {
|
||||
fmt.Println("err 1", err)
|
||||
return
|
||||
}
|
||||
res, err := tx.QueryEx("select id as Id, info_type as InfoType, info_type_Name as InfoTypeName from tbl_info_type where id >= 1")
|
||||
if err != nil {
|
||||
fmt.Println("err 2", err)
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
out := []struct {
|
||||
Id int64
|
||||
InfoType string
|
||||
InfoTypeName string
|
||||
}{}
|
||||
err = res.UnMarshal(&out)
|
||||
if err != nil {
|
||||
fmt.Println("err 3", err)
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
fmt.Println(out)
|
||||
_, err = tx.Exec("insert into tbl_info_type(info_type, info_type_name) VALUES (?, ?)", "4", "weibo")
|
||||
if err != nil {
|
||||
fmt.Println("err 4", err)
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
_, err = tx.Exec("update tbl_info_type set info_types = ? Where id = ?", "5", 0)
|
||||
if err != nil {
|
||||
fmt.Println("err 4", err)
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
// res, err := db.QueryEx("select * from tbl_fun_heelthrow where id >= 1")
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// out := []struct {
|
||||
// Addtime int64 `json:"addtime"`
|
||||
// Tname string `json:"tname"`
|
||||
// Uuid string `json:"uuid,omitempty"`
|
||||
// AAAA string `json:"xxx"`
|
||||
// }{}
|
||||
// err = res.UnMarshal(&out)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
|
||||
// sres := db.SyncQuery("select * from tbl_fun_heelthrow where id >= 1")
|
||||
// res, err = sres.Get(2000)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
|
||||
// out2 := []struct {
|
||||
// Addtime int64 `json:"addtime"`
|
||||
// Tname string `json:"tname"`
|
||||
// Uuid string `json:"uuid,omitempty"`
|
||||
// AAAA string `json:"xxx"`
|
||||
// }{}
|
||||
|
||||
// err = res.UnMarshal(&out2)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
}
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/originnet/service"
|
||||
)
|
||||
|
||||
type HttpClientPoolModule struct {
|
||||
service.BaseModule
|
||||
service.Module
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package sysmodule_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/duanhf2012/origin/sysmodule"
|
||||
)
|
||||
|
||||
func TestHttpClientPoolModule(t *testing.T) {
|
||||
c := sysmodule.HttpClientPoolModule{}
|
||||
c.Init(10, "")
|
||||
|
||||
rsp := c.Request(http.MethodGet, "https://www.baidu.com/", nil, nil)
|
||||
fmt.Println(rsp.Err)
|
||||
fmt.Println(rsp.Header)
|
||||
fmt.Println(rsp.StatusCode)
|
||||
fmt.Println(rsp.Status)
|
||||
fmt.Println(string(rsp.Body))
|
||||
|
||||
srsp := c.SyncRequest(http.MethodGet, "https://www.baidu.com/", nil, nil)
|
||||
rsp1 := srsp.Get(1)
|
||||
fmt.Println(rsp1.Err)
|
||||
fmt.Println(rsp1.Header)
|
||||
fmt.Println(rsp1.StatusCode)
|
||||
fmt.Println(rsp1.Status)
|
||||
fmt.Println(string(rsp1.Body))
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
package sysmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/service"
|
||||
)
|
||||
|
||||
const (
|
||||
maxLinesInLog = 100000 //一个日志文件最多只写这么多行
|
||||
)
|
||||
|
||||
//等级从低到高
|
||||
const (
|
||||
LEVER_UNKNOW = 0
|
||||
LEVER_DEBUG = 1
|
||||
LEVER_INFO = 2
|
||||
LEVER_WARN = 3
|
||||
LEVER_ERROR = 4
|
||||
LEVER_FATAL = 5
|
||||
LEVEL_MAX = 6
|
||||
)
|
||||
|
||||
var LogPrefix = [LEVEL_MAX]string{"[UNKNOW]", "[DEBUG]", "[INFO ]", "[WARN ]", "[ERROR]", "[FATAL]"}
|
||||
|
||||
type ILogger interface {
|
||||
Printf(level uint, format string, v ...interface{})
|
||||
Print(level uint, v ...interface{})
|
||||
SetLogLevel(level uint)
|
||||
}
|
||||
|
||||
type FunListenLog func(uint, string)
|
||||
|
||||
type LogModule struct {
|
||||
service.BaseModule
|
||||
currentDay int64
|
||||
lines int64
|
||||
logfilename string
|
||||
logger [LEVEL_MAX]*log.Logger
|
||||
logFile *os.File
|
||||
openLevel uint
|
||||
locker sync.Mutex
|
||||
calldepth int
|
||||
listenFun FunListenLog
|
||||
}
|
||||
|
||||
func (slf *LogModule) GetCurrentFileName() string {
|
||||
now := time.Now()
|
||||
fpath := filepath.Join("logs")
|
||||
os.MkdirAll(fpath, os.ModePerm)
|
||||
y, m, d := now.Date()
|
||||
h := now.Hour()
|
||||
mm := now.Minute()
|
||||
mm -= mm % 15 //15分钟内使用同一个日志文件
|
||||
dt := y*10000 + int(m)*100 + d
|
||||
tm := h*100 + mm
|
||||
fname := fmt.Sprintf("%s-%d-%d.log", slf.logfilename, dt, tm)
|
||||
ret := filepath.Join(fpath, fname)
|
||||
return ret
|
||||
}
|
||||
|
||||
//检查是否需要切换新的日志文件
|
||||
func (slf *LogModule) CheckAndGenFile(fileline string) (newFile bool) {
|
||||
now := time.Now()
|
||||
nowDate := int64(now.Day())
|
||||
|
||||
slf.locker.Lock()
|
||||
|
||||
isNewDay := nowDate != slf.currentDay
|
||||
slf.lines++
|
||||
if isNewDay || slf.lines > maxLinesInLog {
|
||||
// if time.Now().Day() == slf.currentDay {
|
||||
// slf.locker.Unlock()
|
||||
// return
|
||||
// }
|
||||
//fmt.Println("new log file", slf.currentDay, nowDate, isNewDay, slf.lines, maxLinesInLog)
|
||||
|
||||
slf.currentDay = nowDate
|
||||
slf.lines = 1
|
||||
newFile = true
|
||||
if slf.logFile != nil {
|
||||
slf.logFile.Close()
|
||||
}
|
||||
|
||||
var err error
|
||||
filename := slf.GetCurrentFileName()
|
||||
slf.logFile, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Printf("create log file %+v error!", filename)
|
||||
slf.locker.Unlock()
|
||||
return false
|
||||
}
|
||||
|
||||
for level := 0; level < LEVEL_MAX; level++ {
|
||||
slf.logger[level] = log.New(slf.logFile, LogPrefix[level], log.Lshortfile|log.LstdFlags)
|
||||
}
|
||||
}
|
||||
|
||||
slf.locker.Unlock()
|
||||
return newFile
|
||||
}
|
||||
|
||||
func (slf *LogModule) Init(logFilePrefixName string, openLevel uint) {
|
||||
slf.currentDay = 0
|
||||
slf.logfilename = logFilePrefixName
|
||||
slf.openLevel = openLevel
|
||||
slf.calldepth = 3
|
||||
}
|
||||
|
||||
func (slf *LogModule) SetListenLogFunc(listenFun FunListenLog) {
|
||||
slf.listenFun = listenFun
|
||||
}
|
||||
|
||||
func (slf *LogModule) GetLoggerByLevel(level uint) *log.Logger {
|
||||
if level >= LEVEL_MAX {
|
||||
level = 0
|
||||
}
|
||||
return slf.logger[level]
|
||||
}
|
||||
|
||||
func (slf *LogModule) Printf(level uint, format string, v ...interface{}) {
|
||||
if level < slf.openLevel {
|
||||
return
|
||||
}
|
||||
|
||||
_, file, line, ok := runtime.Caller(slf.calldepth - 1)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
fileLine := fmt.Sprintf(" %s:%d: ", file, line)
|
||||
slf.CheckAndGenFile(fileLine)
|
||||
|
||||
logContents := fmt.Sprintf(format, v...)
|
||||
slf.doPutLog(level, fileLine, logContents)
|
||||
}
|
||||
|
||||
func (slf *LogModule) Print(level uint, v ...interface{}) {
|
||||
if level < slf.openLevel {
|
||||
return
|
||||
}
|
||||
|
||||
_, file, line, ok := runtime.Caller(slf.calldepth - 1)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
fileLine := fmt.Sprintf(" %s:%d: ", file, line)
|
||||
slf.CheckAndGenFile(fileLine)
|
||||
|
||||
logContents := fmt.Sprint(v...)
|
||||
slf.doPutLog(level, fileLine, logContents)
|
||||
}
|
||||
|
||||
//最终写日志的接口
|
||||
func (slf *LogModule) doPutLog(level uint, fileLine, logContents string) {
|
||||
if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil {
|
||||
strlog := fmt.Sprintf("%s %s %s", LogPrefix[level], time.Now().Format("2006-01-02 15:04:05"), logContents)
|
||||
if slf.openLevel == LEVER_DEBUG {
|
||||
fmt.Println(strlog)
|
||||
}
|
||||
|
||||
if slf.listenFun != nil {
|
||||
fline := fileLine
|
||||
if idx := strings.LastIndex(fileLine, "/"); idx >= 0 {
|
||||
fline = fileLine[idx+1:]
|
||||
}
|
||||
|
||||
ft := fline + " " + strlog
|
||||
slf.listenFun(level, fmt.Sprintf(ft))
|
||||
}
|
||||
}
|
||||
|
||||
slf.GetLoggerByLevel(level).Output(slf.calldepth+1, logContents)
|
||||
}
|
||||
|
||||
func (slf *LogModule) AppendCallDepth(calldepth int) {
|
||||
slf.calldepth += calldepth
|
||||
}
|
||||
|
||||
func (slf *LogModule) SetLogLevel(level uint) {
|
||||
slf.openLevel = level
|
||||
}
|
||||
@@ -5,9 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/originnet/log"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/originnet/service"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func (slf *RetMapString) Get() (error, map[string]bool) {
|
||||
|
||||
type Func func()
|
||||
type RedisModule struct {
|
||||
service.BaseModule
|
||||
service.Module
|
||||
redispool *redis.Pool
|
||||
redisTask chan Func
|
||||
}
|
||||
@@ -71,7 +72,7 @@ func (slf *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
}
|
||||
c, err := redis.Dial("tcp", redisServer, opt...)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Connect redis fail reason:%v", err)
|
||||
log.Error("Connect redis fail reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ func (slf *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
}
|
||||
_, err := c.Do("PING")
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_WARN, "Do PING fail reason:%v", err)
|
||||
log.Error("Do PING fail reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
@@ -107,7 +108,7 @@ func (slf *RedisModule) RunAnsyTask() {
|
||||
|
||||
func (slf *RedisModule) GoTask(fc Func) error {
|
||||
if len(slf.redisTask) >= MAX_TASK_CHANNEL {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Redis task channel recover max.")
|
||||
log.Error("Redis task channel recover max.")
|
||||
return fmt.Errorf("Redis task channel recover max.")
|
||||
}
|
||||
|
||||
@@ -118,19 +119,19 @@ func (slf *RedisModule) GoTask(fc Func) error {
|
||||
// GetConn ...
|
||||
func (slf *RedisModule) getConn() (redis.Conn, error) {
|
||||
if slf.redispool == nil {
|
||||
service.GetLogger().Printf(service.LEVER_FATAL, "Not Init RedisModule")
|
||||
log.Error("Not Init RedisModule")
|
||||
return nil, fmt.Errorf("Not Init RedisModule")
|
||||
}
|
||||
conn := slf.redispool.Get()
|
||||
if conn == nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "Cannot get connection")
|
||||
log.Error("Cannot get connection")
|
||||
return nil, fmt.Errorf("Cannot get connection")
|
||||
}
|
||||
|
||||
if conn.Err() != nil {
|
||||
err := conn.Err()
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_WARN, "Get Conn have error,reason:%v", err)
|
||||
log.Error("Get Conn have error,reason:%v", err)
|
||||
}
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@@ -148,7 +149,7 @@ func (slf *RedisModule) TestPingRedis() error {
|
||||
|
||||
err = slf.redispool.TestOnBorrow(conn, time.Now())
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "TestOnBorrow fail,reason:%v", err)
|
||||
log.Error("TestOnBorrow fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -222,7 +223,7 @@ func (slf *RedisModule) GoSetStringJSONExpire(key string, val interface{}, expir
|
||||
slf.GoSetStringExpire(key, string(temp), expire, retErr)
|
||||
return nil
|
||||
} else {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GoSetStringJSONExpire fail,reason:%v", err)
|
||||
log.Error("GoSetStringJSONExpire fail,reason:%v", err)
|
||||
retErr.resultChan <- err
|
||||
}
|
||||
return err
|
||||
@@ -248,14 +249,14 @@ func (slf *RedisModule) setStringByExpire(key, value, expire string) error {
|
||||
}
|
||||
|
||||
if retErr != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "setStringByExpire fail,reason:%v", retErr)
|
||||
log.Error("setStringByExpire fail,reason:%v", retErr)
|
||||
return retErr
|
||||
}
|
||||
|
||||
_, ok := ret.(string)
|
||||
if !ok {
|
||||
retErr = errors.New("setStringByExpire redis data is error")
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "setStringByExpire redis data is error")
|
||||
log.Error("setStringByExpire redis data is error")
|
||||
return retErr
|
||||
}
|
||||
|
||||
@@ -301,7 +302,7 @@ func (slf *RedisModule) GoSetMuchStringExpire(mapInfo map[string]string, expire
|
||||
|
||||
func (slf *RedisModule) setMuchStringByExpire(mapInfo map[string]string, expire string) error {
|
||||
if len(mapInfo) <= 0 {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "setMuchStringByExpire Info Is Empty")
|
||||
log.Error("setMuchStringByExpire Info Is Empty")
|
||||
return errors.New("setMuchStringByExpire Info Is Empty")
|
||||
}
|
||||
|
||||
@@ -324,7 +325,7 @@ func (slf *RedisModule) setMuchStringByExpire(mapInfo map[string]string, expire
|
||||
_, err = conn.Do("EXEC")
|
||||
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "setMuchStringByExpire fail,reason:%v", err)
|
||||
log.Error("setMuchStringByExpire fail,reason:%v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
@@ -341,7 +342,7 @@ func (slf *RedisModule) GetString(key string) (string, error) {
|
||||
|
||||
ret, err := conn.Do("GET", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetString fail,reason:%v", err)
|
||||
log.Error("GetString fail,reason:%v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -353,7 +354,7 @@ func (slf *RedisModule) GetString(key string) (string, error) {
|
||||
str, ok := ret.([]byte)
|
||||
if !ok {
|
||||
err = errors.New("GetString redis data is error")
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetString redis data is error")
|
||||
log.Error("GetString redis data is error")
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -371,7 +372,7 @@ func (slf *RedisModule) GetStringJSON(key string, st interface{}) error {
|
||||
|
||||
ret, err := conn.Do("GET", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetStringJSON fail,reason:%v", err)
|
||||
log.Error("GetStringJSON fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -383,12 +384,12 @@ func (slf *RedisModule) GetStringJSON(key string, st interface{}) error {
|
||||
str, ok := ret.([]byte)
|
||||
if !ok {
|
||||
err = errors.New("GetStringJSON redis data is error!")
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetStringJSON redis data is error!")
|
||||
log.Error("GetStringJSON redis data is error!")
|
||||
return err
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(str, st); err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetStringJSON fail json.Unmarshal is error:%s,%s,reason:%v", key, string(str), err)
|
||||
log.Error("GetStringJSON fail json.Unmarshal is error:%s,%s,reason:%v", key, string(str), err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -413,7 +414,7 @@ func (slf *RedisModule) GetMuchString(keys []string) (retMap map[string]string,
|
||||
// 开始Send数据
|
||||
err = conn.Send("MULTI")
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetMuchString fail %v", err)
|
||||
log.Error("GetMuchString fail %v", err)
|
||||
return nil, err
|
||||
}
|
||||
for _, val := range keys {
|
||||
@@ -423,7 +424,7 @@ func (slf *RedisModule) GetMuchString(keys []string) (retMap map[string]string,
|
||||
ret, err := conn.Do("EXEC")
|
||||
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetMuchString fail %v", err)
|
||||
log.Error("GetMuchString fail %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -455,7 +456,7 @@ func (slf *RedisModule) GetMuchString(keys []string) (retMap map[string]string,
|
||||
func (slf *RedisModule) GetMuchStringJSON(keys map[string]interface{}) error {
|
||||
if len(keys) <= 0 {
|
||||
err := errors.New("GetMuchStringJSON fail key is empty")
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetMuchStringJSON fail key is empty")
|
||||
log.Error("GetMuchStringJSON fail key is empty")
|
||||
return err
|
||||
}
|
||||
conn, err := slf.getConn()
|
||||
@@ -476,7 +477,7 @@ func (slf *RedisModule) GetMuchStringJSON(keys map[string]interface{}) error {
|
||||
ret, err := conn.Do("EXEC")
|
||||
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetMuchStringJSON fail, reason:%v", err)
|
||||
log.Error("GetMuchStringJSON fail, reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -496,7 +497,7 @@ func (slf *RedisModule) GetMuchStringJSON(keys map[string]interface{}) error {
|
||||
|
||||
err = json.Unmarshal(strVal, keys[tempKeys[index]])
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetMuchStringJSON Unmarshal fail, reason:%v", err)
|
||||
log.Error("GetMuchStringJSON Unmarshal fail, reason:%v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -514,7 +515,7 @@ func (slf *RedisModule) ExistsKey(key string) (bool, error) {
|
||||
|
||||
ret, err := conn.Do("EXISTS", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "ExistsKey fail, reason:%v", err)
|
||||
log.Error("ExistsKey fail, reason:%v", err)
|
||||
return false, err
|
||||
}
|
||||
retValue, ok := ret.(int64)
|
||||
@@ -537,7 +538,7 @@ func (slf *RedisModule) DelString(key string) error {
|
||||
|
||||
ret, err := conn.Do("DEL", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "DelString fail, reason:%v", err)
|
||||
log.Error("DelString fail, reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -613,7 +614,7 @@ func (slf *RedisModule) DelMuchString(keys []string) (map[string]bool, error) {
|
||||
ret, err := conn.Do("EXEC")
|
||||
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "DelMuchString fail,reason:%v", err)
|
||||
log.Error("DelMuchString fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -652,7 +653,7 @@ func (slf *RedisModule) SetHash(redisKey, hashKey, value string) error {
|
||||
|
||||
_, retErr := conn.Do("HSET", redisKey, hashKey, value)
|
||||
if retErr != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetHash fail,reason:%v", retErr)
|
||||
log.Error("SetHash fail,reason:%v", retErr)
|
||||
}
|
||||
|
||||
return retErr
|
||||
@@ -686,7 +687,7 @@ func (slf *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, erro
|
||||
|
||||
value, err := conn.Do("HGETALL", redisKey)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetAllHashJSON fail,reason:%v", err)
|
||||
log.Error("GetAllHashJSON fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -697,7 +698,7 @@ func (slf *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, erro
|
||||
func (slf *RedisModule) GetHashValueByKey(redisKey string, fieldKey string) (string, error) {
|
||||
|
||||
if redisKey == "" || fieldKey == "" {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetHashValueByKey key is empty!")
|
||||
log.Error("GetHashValueByKey key is empty!")
|
||||
return "", errors.New("Key Is Empty")
|
||||
}
|
||||
conn, err := slf.getConn()
|
||||
@@ -708,7 +709,7 @@ func (slf *RedisModule) GetHashValueByKey(redisKey string, fieldKey string) (str
|
||||
|
||||
value, err := conn.Do("HGET", redisKey, fieldKey)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetHashValueByKey fail,reason:%v", err)
|
||||
log.Error("GetHashValueByKey fail,reason:%v", err)
|
||||
return "", err
|
||||
}
|
||||
if value == nil {
|
||||
@@ -770,7 +771,7 @@ func (slf *RedisModule) SetMuchHashJSON(redisKey string, value map[string][]inte
|
||||
// 执行命令
|
||||
_, err = conn.Do("EXEC")
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetMuchHashJSON fail,reason:%v", err)
|
||||
log.Error("SetMuchHashJSON fail,reason:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -841,7 +842,7 @@ func (slf *RedisModule) DelMuchHash(redisKey string, hsahKey []string) error {
|
||||
|
||||
_, retErr := conn.Do("HDEL", arg...)
|
||||
if retErr != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "DelMuchHash fail,reason:%v", retErr)
|
||||
log.Error("DelMuchHash fail,reason:%v", retErr)
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
@@ -883,7 +884,7 @@ func (slf *RedisModule) setList(key string, value []string, setType string) erro
|
||||
}
|
||||
_, retErr := conn.Do(setType, arg...)
|
||||
if retErr != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "setList fail,reason:%v", retErr)
|
||||
log.Error("setList fail,reason:%v", retErr)
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
@@ -918,7 +919,7 @@ func (slf *RedisModule) SetListJSONLpush(key string, value interface{}) error {
|
||||
tempVal := []string{string(temp)}
|
||||
err = slf.setList(key, tempVal, "LPUSH")
|
||||
} else {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetListJSONLpush fail,reason:%v", err)
|
||||
log.Error("SetListJSONLpush fail,reason:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -955,7 +956,7 @@ func (slf *RedisModule) SetMuchListJSONLpush(key string, value []interface{}) er
|
||||
for _, val := range value {
|
||||
temp, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetMuchListJSONLpush fail,reason:%v", err)
|
||||
log.Error("SetMuchListJSONLpush fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -998,7 +999,7 @@ func (slf *RedisModule) SetListJSONRpush(key string, value interface{}) error {
|
||||
tempVal := []string{string(temp)}
|
||||
err = slf.setList(key, tempVal, "RPUSH")
|
||||
} else {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetListJSONRpush fail,reason:%v", err)
|
||||
log.Error("SetListJSONRpush fail,reason:%v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
@@ -1052,7 +1053,7 @@ func (slf *RedisModule) Lrange(key string, start, end int) ([]string, error) {
|
||||
|
||||
reply, err := conn.Do("lrange", key, start, end)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "SetListJSONRpush fail,reason:%v", err)
|
||||
log.Error("SetListJSONRpush fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1069,7 +1070,7 @@ func (slf *RedisModule) GetListLen(key string) (int, error) {
|
||||
|
||||
reply, err := conn.Do("LLEN", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "GetListLen fail,reason:%v", err)
|
||||
log.Error("GetListLen fail,reason:%v", err)
|
||||
return -1, err
|
||||
}
|
||||
return redis.Int(reply, err)
|
||||
@@ -1086,7 +1087,7 @@ func (slf *RedisModule) RPOPListValue(key string) error {
|
||||
|
||||
_, err = conn.Do("RPOP", key, 100)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "RPOPListValue fail,reason:%v", err)
|
||||
log.Error("RPOPListValue fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1103,7 +1104,7 @@ func (slf *RedisModule) LtrimList(key string, start, end int) error {
|
||||
|
||||
_, err = conn.Do("LTRIM", key, start, end)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "LtrimListValue fail,reason:%v", err)
|
||||
log.Error("LtrimListValue fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1124,7 +1125,7 @@ func (slf *RedisModule) ZADDInsertJson(key string, score float64, value interfac
|
||||
}
|
||||
_, err = conn.Do("ZADD", key, score, JsonValue)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "ZADDInsertJson fail,reason:%v", err)
|
||||
log.Error("ZADDInsertJson fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1140,7 +1141,7 @@ func (slf *RedisModule) ZADDInsert(key string, score float64, Data interface{})
|
||||
|
||||
_, err = conn.Do("ZADD", key, score, Data)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "ZADDInsert fail,reason:%v", err)
|
||||
log.Error("ZADDInsert fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1444,13 +1445,13 @@ func (slf *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) erro
|
||||
|
||||
_, retErr := conn.Do("HINCRBY", redisKey, hashKey, value)
|
||||
if retErr != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "HincrbyHashInt fail,reason:%v", retErr)
|
||||
log.Error("HincrbyHashInt fail,reason:%v", retErr)
|
||||
}
|
||||
|
||||
return retErr
|
||||
}
|
||||
|
||||
func (slf *RedisModule) EXPlREInsert(key string, TTl int) error {
|
||||
func (slf *RedisModule) EXPlREInsert(key string, TTl int) error {
|
||||
conn, err := slf.getConn()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1459,7 +1460,7 @@ func (slf *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) erro
|
||||
|
||||
_, err = conn.Do("expire", key, TTl)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "expire fail,reason:%v", err)
|
||||
log.Error("expire fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1486,7 +1487,7 @@ func (slf *RedisModule) Keys(key string) ([]string, error) {
|
||||
|
||||
ret, err := conn.Do("KEYS", key)
|
||||
if err != nil {
|
||||
service.GetLogger().Printf(service.LEVER_ERROR, "KEYS fail, reason:%v", err)
|
||||
log.Error("KEYS fail, reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
retList, ok := ret.([]interface{})
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
package sysmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CTestJson struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
|
||||
func TestRedisModule(t *testing.T) {
|
||||
|
||||
var cfg ConfigRedis
|
||||
var redsmodule RedisModule
|
||||
|
||||
cfg.IP = "127.0.0.1"
|
||||
cfg.Password = ""
|
||||
cfg.Port = "6379"
|
||||
cfg.IdleTimeout = 2
|
||||
cfg.MaxActive = 3
|
||||
cfg.MaxIdle = 3
|
||||
cfg.DbIndex = 15
|
||||
redsmodule.Init(&cfg)
|
||||
|
||||
//GoSetString
|
||||
var retErr RetError
|
||||
redsmodule.GoSetString("testkey", "testvalue", &retErr)
|
||||
ret1, err1 := redsmodule.GetString("testkey")
|
||||
|
||||
//GoSetStringExpire
|
||||
redsmodule.GoSetStringExpire("setstring_key", "value11", "1", &retErr)
|
||||
time.Sleep(time.Second * 2)
|
||||
ret2, err2 := redsmodule.GetString("setstring_key")
|
||||
fmt.Print(ret1, err1, retErr.Get(), "\n")
|
||||
fmt.Print(ret2, err2, "\n")
|
||||
|
||||
//GoSetStringJSON
|
||||
var testjson CTestJson
|
||||
testjson.A = "A value"
|
||||
testjson.B = "B value"
|
||||
|
||||
var retErr2 RetError
|
||||
redsmodule.GoSetStringJSON("setstring_key_json", &testjson, &retErr)
|
||||
|
||||
//GoSetStringJSONExpire
|
||||
redsmodule.GoSetStringJSONExpire("setstring_key_json_expire", &testjson, "10", &retErr2)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
fmt.Print(retErr2.Get(), "\n")
|
||||
var testjson2 CTestJson
|
||||
err := redsmodule.GetStringJSON("setstring_key_json", &testjson2)
|
||||
fmt.Print(err, "\n", testjson2, "\n")
|
||||
|
||||
//GoSetMuchString/GoSetMuchStringExpire
|
||||
mapInfo := make(map[string]string)
|
||||
mapInfo["A"] = "A_value"
|
||||
mapInfo["B"] = "B_value"
|
||||
mapInfo["C"] = "C_value"
|
||||
redsmodule.GoSetMuchString(mapInfo, &retErr)
|
||||
redsmodule.GoSetMuchStringExpire(mapInfo, "100", &retErr)
|
||||
var keylist []string
|
||||
keylist = append(keylist, "A", "C")
|
||||
mapInfo, _ = redsmodule.GetMuchString(keylist)
|
||||
fmt.Print(retErr.Get(), "\n", mapInfo, "\n")
|
||||
|
||||
//GoDelString
|
||||
redsmodule.GoDelString("setstring_key_json", &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
|
||||
var retMapStr RetMapString
|
||||
redsmodule.GoDelMuchString(keylist, &retMapStr)
|
||||
err2, mapstr := retMapStr.Get()
|
||||
fmt.Print(err2, "\n", mapstr, "\n")
|
||||
|
||||
//GoSetHash
|
||||
redsmodule.GoSetHash("rediskey", "hashkey", "1111", &retErr)
|
||||
ret, err := redsmodule.GetHashValueByKey("rediskey", "hashkey")
|
||||
fmt.Print(retErr.Get(), ret, err)
|
||||
|
||||
//GoSetHashJSON
|
||||
redsmodule.GoSetHashJSON("rediskey", "hashkey2", &testjson, &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
|
||||
//SetMuchHashJSON
|
||||
var mapHashJson map[string][]interface{}
|
||||
var listInterface []interface{}
|
||||
mapHashJson = make(map[string][]interface{})
|
||||
listInterface = append(listInterface, testjson, testjson)
|
||||
|
||||
mapHashJson["key3"] = listInterface
|
||||
mapHashJson["key4"] = listInterface
|
||||
|
||||
redsmodule.GoSetMuchHashJSON("rediskey", mapHashJson, &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
|
||||
//GoDelHash
|
||||
redsmodule.GoDelHash("rediskey", "hashkey", &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
|
||||
//GoDelMuchHash
|
||||
var keys []string
|
||||
keys = append(keys, "hashkey", "hashkey2")
|
||||
|
||||
redsmodule.GoDelMuchHash("rediskey", keys, &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
|
||||
//GoSetListLpush
|
||||
redsmodule.GoSetListLpush("setlistKey", "list1", &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
redsmodule.GoSetListLpush("setlistKey", "list2", &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
redsmodule.GoSetListLpush("setlistKey", "list3", &retErr)
|
||||
fmt.Print(retErr.Get(), "\n")
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package sysmodule
|
||||
|
||||
//等级从低到高
|
||||
const (
|
||||
SYS_LOG = 1
|
||||
SYS_TEST = 2
|
||||
)
|
||||
Reference in New Issue
Block a user