首页折线图添加缓存
This commit is contained in:
@@ -8,7 +8,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/astaxie/beego/cache"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TaskLog struct {
|
||||
@@ -23,6 +27,8 @@ type TaskLog struct {
|
||||
CreateTime int64
|
||||
}
|
||||
|
||||
var RunNumCache, _ = cache.NewCache("memory", `{"interval":60}`)
|
||||
|
||||
func (t *TaskLog) TableName() string {
|
||||
return TableName("task_log")
|
||||
}
|
||||
@@ -81,12 +87,32 @@ type SumDays struct {
|
||||
}
|
||||
|
||||
func SumByDays(limit int, status string) orm.Params {
|
||||
|
||||
var m = map[string]string{
|
||||
"0": "okNum",
|
||||
"-1": "errNum",
|
||||
"-2": "expiredRun"}
|
||||
|
||||
res := make(orm.Params)
|
||||
key := m[status]
|
||||
|
||||
if RunNumCache.IsExist(key) {
|
||||
json.Unmarshal(RunNumCache.Get(key).([]byte), &res)
|
||||
logs.Info("cache")
|
||||
return res
|
||||
}
|
||||
_, err := orm.NewOrm().Raw("SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d') days,COUNT(id) count FROM pp_task_log WHERE status in(?) GROUP BY days ORDER BY days DESC limit ?;",
|
||||
status, limit).RowsToMap(&res, "days", "count")
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
RunNumCache.Put(key, data, 2*time.Hour)
|
||||
return res
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user