gva-pms/server/service/monitor_service/monitor.go

48 lines
1.0 KiB
Go

package monitor_service
import (
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
"sync"
)
var (
MonitorCache = make(map[string]map[string]bool) // 缓存结构: table -> field -> exists
CacheMutex sync.RWMutex
)
// 初始化缓存(在应用启动时调用)
func InitMonitorCache() {
refreshCache()
}
// 刷新缓存(定时任务或手动触发)
func refreshCache() {
var configs []system.SysMonitorConfig
global.GVA_DB.Find(&configs)
newCache := make(map[string]map[string]bool)
for _, c := range configs {
if newCache[c.SysTableName] == nil {
newCache[c.SysTableName] = make(map[string]bool)
}
newCache[c.SysTableName][c.FieldName] = true
}
CacheMutex.Lock()
MonitorCache = newCache
CacheMutex.Unlock()
}
// 检查字段是否被监控
func IsFieldMonitored(table, field string) bool {
CacheMutex.RLock()
defer CacheMutex.RUnlock()
fields, ok := MonitorCache[table]
if !ok {
return false
}
return fields[field]
}