67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
config2 "SciencesServer/app/basic/config"
|
|
"SciencesServer/app/common/model"
|
|
"SciencesServer/config"
|
|
"SciencesServer/serve/orm"
|
|
"fmt"
|
|
)
|
|
|
|
type Cache struct {
|
|
}
|
|
|
|
type CacheHandle func() *Cache
|
|
|
|
type caches struct {
|
|
iModel model.IModel
|
|
iValues func() interface{}
|
|
toCache func(values interface{})
|
|
}
|
|
|
|
func (this *Cache) Init() {
|
|
db := orm.GetDB()
|
|
|
|
function := func(cache ...*caches) {
|
|
for _, v := range cache {
|
|
if db.Migrator().HasTable(v.iModel) {
|
|
if v.iValues != nil {
|
|
if values := v.iValues(); values != nil {
|
|
v.toCache(values)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function(
|
|
&caches{iModel: model.NewSysConfig(), iValues: func() interface{} {
|
|
out := make([]*model.SysConfig, 0)
|
|
_ = db.Table(model.NewSysConfig().TableName()).Find(&out)
|
|
return out
|
|
}, toCache: func(values interface{}) {
|
|
out := values.([]*model.SysConfig)
|
|
for _, v := range out {
|
|
config.SystemConfig[v.Key] = v.Value
|
|
}
|
|
}},
|
|
)
|
|
function(
|
|
&caches{iModel: model.NewSysIndustry(), iValues: func() interface{} {
|
|
out := make([]*model.SysIndustry, 0)
|
|
_ = db.Table(model.NewSysIndustry().TableName()).Select("id", "name").Scan(&out)
|
|
return out
|
|
}, toCache: func(values interface{}) {
|
|
out := values.([]*model.SysIndustry)
|
|
for _, v := range out {
|
|
config2.MemoryForIndustryInfo[fmt.Sprintf("%d", v.ID)] = v.Name
|
|
}
|
|
}},
|
|
)
|
|
}
|
|
|
|
func NewCache() CacheHandle {
|
|
return func() *Cache {
|
|
return &Cache{}
|
|
}
|
|
}
|