init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user