mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-18 11:50:26 +08:00
修复访问日志可能无法写入当天数据表的Bug
This commit is contained in:
@@ -180,7 +180,7 @@ Loop:
|
|||||||
|
|
||||||
// CreateHTTPAccessLog 写入单条访问日志
|
// CreateHTTPAccessLog 写入单条访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
||||||
var day = timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
var day = timeutil.FormatTime("Ymd", accessLog.Timestamp)
|
||||||
tableDef, err := SharedHTTPAccessLogManager.FindTable(dao.Instance, day, true)
|
tableDef, err := SharedHTTPAccessLogManager.FindTable(dao.Instance, day, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -240,7 +240,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLog
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if accessLogEnableAutoPartial && accessLogRowsPerTable > 0 && lastId%accessLogRowsPerTable == 0 {
|
if accessLogEnableAutoPartial && accessLogRowsPerTable > 0 && lastId >= accessLogRowsPerTable {
|
||||||
SharedHTTPAccessLogManager.ResetTable(dao.Instance, day)
|
SharedHTTPAccessLogManager.ResetTable(dao.Instance, day)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,15 @@ func (this *HTTPAccessLogManager) FindTableNames(db *dbs.DB, day string) ([]stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 排序
|
// 排序
|
||||||
sort.Strings(results)
|
// 这里不能直接使用sort.Strings(),因为表名里面可能大小写混合
|
||||||
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
var name1 = results[i]
|
||||||
|
var name2 = results[j]
|
||||||
|
if len(name1) < len(name2) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return strings.ToLower(name1) < strings.ToLower(name2)
|
||||||
|
})
|
||||||
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
@@ -132,7 +140,8 @@ func (this *HTTPAccessLogManager) FindTable(db *dbs.DB, day string, force bool)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var cacheKey = config.Dsn
|
var cachePrefix = config.Dsn
|
||||||
|
var cacheKey = this.composeTableCacheKey(cachePrefix, day)
|
||||||
def, ok := this.currentTableMapping[cacheKey]
|
def, ok := this.currentTableMapping[cacheKey]
|
||||||
if ok {
|
if ok {
|
||||||
return def, nil
|
return def, nil
|
||||||
@@ -146,6 +155,18 @@ func (this *HTTPAccessLogManager) FindTable(db *dbs.DB, day string, force bool)
|
|||||||
// 只有存在的表格才缓存
|
// 只有存在的表格才缓存
|
||||||
if def != nil && def.Exists {
|
if def != nil && def.Exists {
|
||||||
this.currentTableMapping[cacheKey] = def
|
this.currentTableMapping[cacheKey] = def
|
||||||
|
|
||||||
|
// 清除过时缓存
|
||||||
|
for oldCacheKey := range this.currentTableMapping {
|
||||||
|
var dayIndex = strings.LastIndex(oldCacheKey, "_")
|
||||||
|
if dayIndex > 0 {
|
||||||
|
var oldPrefix = oldCacheKey[:dayIndex]
|
||||||
|
var oldDay = oldCacheKey[dayIndex+1:]
|
||||||
|
if oldPrefix == cachePrefix && oldDay < day {
|
||||||
|
delete(this.currentTableMapping, oldCacheKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return def, nil
|
return def, nil
|
||||||
}
|
}
|
||||||
@@ -173,7 +194,7 @@ func (this *HTTPAccessLogManager) ResetTable(db *dbs.DB, day string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(this.currentTableMapping, config.Dsn)
|
delete(this.currentTableMapping, this.composeTableCacheKey(config.Dsn, day))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找某个表格
|
// 查找某个表格
|
||||||
@@ -290,3 +311,9 @@ func (this *HTTPAccessLogManager) checkTableFields(db *dbs.DB, tableName string)
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 组合表格的缓存Key
|
||||||
|
func (this *HTTPAccessLogManager) composeTableCacheKey(dsn string, day string) string {
|
||||||
|
// 注意:格式一定要固定,下面清除缓存的时候需要用到
|
||||||
|
return dsn + "_" + day
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user