optimize log tool

This commit is contained in:
hoteas 2021-05-25 20:27:24 +08:00
parent 740059075a
commit fc315064e7
11 changed files with 86 additions and 78 deletions

View File

@ -22,6 +22,7 @@ type Application struct {
MethodRouter MethodRouter
Router Router
ContextBase ContextBase
Error
Log *logrus.Logger Log *logrus.Logger
WebConnectLog *logrus.Logger WebConnectLog *logrus.Logger
Port string //端口号 Port string //端口号

1
cache/cache.go vendored
View File

@ -9,6 +9,7 @@ type HoTimeCache struct {
//set(key string, value interface{}, time int64) //set(key string, value interface{}, time int64)
//get(key string) interface{} //get(key string) interface{}
//delete(key string) //delete(key string)
Error
dbCache CacheIns dbCache CacheIns
redisCache CacheIns redisCache CacheIns
memoryCache CacheIns memoryCache CacheIns

69
cache/cache_db.go vendored
View File

@ -22,43 +22,44 @@ type HoTimeDBInterface interface {
type CacheDb struct { type CacheDb struct {
Time int64 Time int64
Db HoTimeDBInterface Db HoTimeDBInterface
Error
ContextBase ContextBase
isInit bool isInit bool
} }
func (this *CacheDb) initDbTable() { func (that *CacheDb) initDbTable() {
if this.isInit { if that.isInit {
return return
} }
if this.Db.GetType() == "mysql" { if that.Db.GetType() == "mysql" {
dbNames := this.Db.Query("SELECT DATABASE()") dbNames := that.Db.Query("SELECT DATABASE()")
if len(dbNames) == 0 { if len(dbNames) == 0 {
return return
} }
dbName := dbNames[0].GetString("DATABASE()") dbName := dbNames[0].GetString("DATABASE()")
res := this.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'") res := that.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'")
if len(res) != 0 { if len(res) != 0 {
this.isInit = true that.isInit = true
return return
} }
_, e := this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8") _, e := that.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
if e.GetError() == nil { if e.GetError() == nil {
this.isInit = true that.isInit = true
} }
} }
if this.Db.GetType() == "sqlite" { if that.Db.GetType() == "sqlite" {
res := this.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`) res := that.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`)
if len(res) != 0 { if len(res) != 0 {
this.isInit = true that.isInit = true
return return
} }
_, e := this.Db.Exec(`CREATE TABLE "cached" ( _, e := that.Db.Exec(`CREATE TABLE "cached" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"ckey" TEXT(60), "ckey" TEXT(60),
"cvalue" TEXT(2000), "cvalue" TEXT(2000),
@ -66,7 +67,7 @@ func (this *CacheDb) initDbTable() {
"endtime" integer "endtime" integer
);`) );`)
if e.GetError() == nil { if e.GetError() == nil {
this.isInit = true that.isInit = true
} }
} }
@ -74,9 +75,9 @@ func (this *CacheDb) initDbTable() {
} }
//获取Cache键只能为string类型 //获取Cache键只能为string类型
func (this *CacheDb) get(key string) interface{} { func (that *CacheDb) get(key string) interface{} {
cached := this.Db.Get("cached", "*", Map{"ckey": key}) cached := that.Db.Get("cached", "*", Map{"ckey": key})
if cached == nil { if cached == nil {
return nil return nil
@ -84,7 +85,7 @@ func (this *CacheDb) get(key string) interface{} {
//data:=cacheMap[key]; //data:=cacheMap[key];
if cached.GetInt64("endtime") <= time.Now().Unix() { if cached.GetInt64("endtime") <= time.Now().Unix() {
this.Db.Delete("cached", Map{"id": cached.GetString("id")}) that.Db.Delete("cached", Map{"id": cached.GetString("id")})
return nil return nil
} }
@ -95,65 +96,65 @@ func (this *CacheDb) get(key string) interface{} {
} }
//key value ,时间为时间戳 //key value ,时间为时间戳
func (this *CacheDb) set(key string, value interface{}, tim int64) { func (that *CacheDb) set(key string, value interface{}, tim int64) {
bte, _ := json.Marshal(Map{"data": value}) bte, _ := json.Marshal(Map{"data": value})
num := this.Db.Update("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"ckey": key}) num := that.Db.Update("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"ckey": key})
if num == int64(0) { if num == int64(0) {
this.Db.Insert("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "ckey": key}) that.Db.Insert("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "ckey": key})
} }
//随机执行删除命令 //随机执行删除命令
if Rand(1000) > 950 { if Rand(1000) > 950 {
this.Db.Delete("cached", Map{"endtime[<]": time.Now().Unix()}) that.Db.Delete("cached", Map{"endtime[<]": time.Now().Unix()})
} }
} }
func (this *CacheDb) delete(key string) { func (that *CacheDb) delete(key string) {
del := strings.Index(key, "*") del := strings.Index(key, "*")
//如果通配删除 //如果通配删除
if del != -1 { if del != -1 {
key = Substr(key, 0, del) key = Substr(key, 0, del)
this.Db.Delete("cached", Map{"ckey": key + "%"}) that.Db.Delete("cached", Map{"ckey": key + "%"})
} else { } else {
this.Db.Delete("cached", Map{"ckey": key}) that.Db.Delete("cached", Map{"ckey": key})
} }
} }
func (this *CacheDb) Cache(key string, data ...interface{}) *Obj { func (that *CacheDb) Cache(key string, data ...interface{}) *Obj {
this.initDbTable() that.initDbTable()
if len(data) == 0 { if len(data) == 0 {
return &Obj{Data: this.get(key)} return &Obj{Data: that.get(key)}
} }
tim := time.Now().Unix() tim := time.Now().Unix()
if len(data) == 1 && data[0] == nil { if len(data) == 1 && data[0] == nil {
this.delete(key) that.delete(key)
return &Obj{Data: nil} return &Obj{Data: nil}
} }
if len(data) == 1 { if len(data) == 1 {
if this.Time == 0 { if that.Time == 0 {
//this.Time = Config.GetInt64("cacheLongTime") //that.Time = Config.GetInt64("cacheLongTime")
} }
tim += this.Time tim += that.Time
} }
if len(data) == 2 { if len(data) == 2 {
this.SetError(nil) that.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error) tempt := ObjToInt64(data[1], &that.Error)
if tempt > tim { if tempt > tim {
tim = tempt tim = tempt
} else if this.GetError() == nil { } else if that.GetError() == nil {
tim = tim + tempt tim = tim + tempt
} }
} }
this.set(key, data[0], tim) that.set(key, data[0], tim)
return &Obj{Data: nil} return &Obj{Data: nil}
} }

View File

@ -10,6 +10,7 @@ import (
type CacheMemory struct { type CacheMemory struct {
Time int64 Time int64
Map Map
Error
ContextBase ContextBase
mutex *sync.RWMutex mutex *sync.RWMutex
} }

View File

@ -13,6 +13,7 @@ type CacheRedis struct {
Time int64 Time int64
conn redis.Conn conn redis.Conn
tag int64 tag int64
ContextBase
Error Error
} }

2
cache/type.go vendored
View File

@ -8,6 +8,8 @@ type CacheIns interface {
//set(key string, value interface{}, time int64) //set(key string, value interface{}, time int64)
//get(key string) interface{} //get(key string) interface{}
//delete(key string) //delete(key string)
GetError() error
SetError(err error)
Cache(key string, data ...interface{}) *Obj Cache(key string, data ...interface{}) *Obj
} }

View File

@ -5,7 +5,6 @@ import (
) )
type ContextBase struct { type ContextBase struct {
Error
tag string tag string
} }

View File

@ -18,7 +18,7 @@ func (that *Error) GetError() error {
func (that *Error) SetError(err error) { func (that *Error) SetError(err error) {
that.error = err that.error = err
if that.Logger != nil { if that.Logger != nil && err != nil {
//that.Logger=log.GetLog("",false) //that.Logger=log.GetLog("",false)
that.Logger.Warn(err) that.Logger.Warn(err)
} }

View File

@ -3,91 +3,92 @@ package common
//对象封装方便取用 //对象封装方便取用
type Obj struct { type Obj struct {
Data interface{} Data interface{}
Error
ContextBase ContextBase
} }
func (this *Obj) Put(data interface{}) { func (that *Obj) Put(data interface{}) {
this.Data = data that.Data = data
} }
func (this *Obj) ToInt(err ...Error) int { func (that *Obj) ToInt(err ...Error) int {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToInt(this.Data, &this.Error) return ObjToInt(that.Data, &that.Error)
} }
func (this *Obj) ToInt64(err ...Error) int64 { func (that *Obj) ToInt64(err ...Error) int64 {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToInt64(this.Data, &this.Error) return ObjToInt64(that.Data, &that.Error)
} }
func (this *Obj) ToFloat64(err ...Error) float64 { func (that *Obj) ToFloat64(err ...Error) float64 {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToFloat64(this.Data, &this.Error) return ObjToFloat64(that.Data, &that.Error)
} }
//获取向上取整float64 // ToCeilFloat64 获取向上取整float64
func (this *Obj) ToCeilFloat64(err ...*Error) float64 { func (that *Obj) ToCeilFloat64(err ...*Error) float64 {
if len(err) != 0 { if len(err) != 0 {
this.Error = *err[0] that.Error = *err[0]
} }
v := ObjToCeilFloat64(this.Data, err...) v := ObjToCeilFloat64(that.Data, err...)
return v return v
} }
func (this *Obj) ToStr() string { func (that *Obj) ToStr() string {
return ObjToStr(this.Data) return ObjToStr(that.Data)
} }
func (this *Obj) ToMap(err ...Error) Map { func (that *Obj) ToMap(err ...Error) Map {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToMap(this.Data, &this.Error) return ObjToMap(that.Data, &that.Error)
} }
func (this *Obj) ToSlice(err ...Error) Slice { func (that *Obj) ToSlice(err ...Error) Slice {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToSlice(this.Data, &this.Error) return ObjToSlice(that.Data, &that.Error)
} }
func (this *Obj) ToMapArray(err ...Error) []Map { func (that *Obj) ToMapArray(err ...Error) []Map {
if len(err) != 0 { if len(err) != 0 {
this.Error = err[0] that.Error = err[0]
} }
return ObjToMapArray(this.Data, &this.Error) return ObjToMapArray(that.Data, &that.Error)
} }
func (this *Obj) ToObj() interface{} { func (that *Obj) ToObj() interface{} {
return this.Data return that.Data
} }
//获取向上取整Int64 //获取向上取整Int64
func (this *Obj) ToCeilInt64(err ...*Error) int64 { func (that *Obj) ToCeilInt64(err ...*Error) int64 {
if len(err) != 0 { if len(err) != 0 {
this.Error = *err[0] that.Error = *err[0]
} }
v := ObjToCeilInt64(this.Data, err...) v := ObjToCeilInt64(that.Data, err...)
return v return v
} }
//获取向上取整Int //获取向上取整Int
func (this *Obj) ToCeilInt(err ...*Error) int { func (that *Obj) ToCeilInt(err ...*Error) int {
if len(err) != 0 { if len(err) != 0 {
this.Error = *err[0] that.Error = *err[0]
} }
v := ObjToCeilInt(this.Data, err...) v := ObjToCeilInt(that.Data, err...)
return v return v
} }

View File

@ -1,4 +1,4 @@
package hotime package hotime
//session储存头 // HEAD_SESSION_ADD session储存头
const HEAD_SESSION_ADD = "session#" const HEAD_SESSION_ADD = "session#"

View File

@ -10,29 +10,30 @@ import (
) )
//下载文件 //下载文件
func Down(url, path, name string) bool { func Down(url, path, name string, e ...*Error) bool {
os.MkdirAll(path, os.ModeDir) os.MkdirAll(path, os.ModeDir)
if Substr(path, len(path)-1, len(path)) != "/" { if Substr(path, len(path)-1, len(path)) != "/" {
path = path + "/" path = path + "/"
} }
out, err := os.Create(path + name) out, err := os.Create(path + name)
e := Error{}
if err != nil { if err != nil && e[0] != nil {
e.SetError(err) e[0].SetError(err)
return false return false
} }
defer out.Close() defer out.Close()
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil && e[0] != nil {
e[0].SetError(err)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
pix, err := ioutil.ReadAll(resp.Body) pix, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(out, bytes.NewReader(pix)) _, err = io.Copy(out, bytes.NewReader(pix))
if err != nil { if err != nil && e[0] != nil {
e.SetError(err) e[0].SetError(err)
return false return false
} }
return true return true