首页折线图添加缓存

This commit is contained in:
georgehao
2019-08-13 23:19:38 +08:00
parent 07ba83e1ab
commit 77c079665a
2 changed files with 27 additions and 2 deletions

View File

@@ -15,8 +15,6 @@ import (
"runtime"
"sort"
"strconv"
//"strconv"
"time"
)
@@ -173,6 +171,7 @@ func (self *HomeController) Start() {
self.Data["okNum"] = okNum
self.Data["errNum"] = errNum
self.Data["expiredNum"] = expiredNum
self.Data["cpuNum"] = runtime.NumCPU()
//系统运行信息

View File

@@ -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
}