Files
2021-12-28 09:18:32 +08:00

82 lines
1.8 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.NewSysTenant(), iValues: func() interface{} {
out := make([]*model.SysConfig, 0)
_ = model.Find(model.NewSysConfig(), &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)
_ = model.ScanFields(model.NewSysIndustry(), &out, []string{"id", "name"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysIndustry)
for _, v := range out {
config2.MemoryForIndustryInfo[fmt.Sprintf("%d", v.ID)] = v.Name
}
}},
)
function(
&caches{iModel: model.NewSysPlatform(), iValues: func() interface{} {
out := make([]*model.SysPlatform, 0)
_ = model.ScanFields(model.NewSysPlatform(), &out, []string{"id", "key", "link"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysPlatform)
for _, v := range out {
if v.Link == "" {
continue
}
config2.MemoryForPlatformInfo[v.Link] = v.Key
}
}},
)
}
func NewCache() CacheHandle {
return func() *Cache {
return &Cache{}
}
}