package cfg import ( "context" "strings" "sync" "go.mongodb.org/mongo-driver/bson" ) var appKeyCache struct { sync.RWMutex value string ready bool } // CacheAppKey 把系统配置里的 appKey 放进内存,供媒资写接口读取。 func CacheAppKey(appKey string) { appKeyCache.Lock() appKeyCache.value = strings.TrimSpace(appKey) appKeyCache.ready = true appKeyCache.Unlock() } // CachedAppKey 返回已缓存的 appKey。缓存未建立时读取 sys_cfg 并写入缓存。 func CachedAppKey(ctx context.Context) string { appKeyCache.RLock() if appKeyCache.ready { value := appKeyCache.value appKeyCache.RUnlock() return value } appKeyCache.RUnlock() sysCfg, serr := FindOneSysCfg(ctx, bson.M{FN.SysCfg.Id: 1}) if serr != nil || sysCfg == nil { return "" } value := strings.TrimSpace(sysCfg.AppKey) CacheAppKey(value) return value }