mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 09:46:37 +08:00
perf(config): add Otter RAM cache layer for system configs
Introduce pkg/cache/ram on top of existing Redis/DB config reads. GetByKey now checks local RAM before Redis. Unified invalidation clears RAM and Redis hash fields on admin writes, with pub/sub for multi-node RAM eviction. Add tests and wire create/update/migrator write paths.
This commit is contained in:
@@ -81,10 +81,18 @@ func (SystemConfig) TableName() string {
|
||||
return "w_system_configs"
|
||||
}
|
||||
|
||||
// GetByKey 通过 key 查询配置(带 Redis 缓存)
|
||||
// GetByKey 通过 key 查询配置(带 RAM + Redis 缓存)
|
||||
func (sc *SystemConfig) GetByKey(ctx context.Context, key string) error {
|
||||
ensureSystemConfigCacheListener()
|
||||
|
||||
if cached, ok := systemConfigRAMCache.GetIfPresent(key); ok {
|
||||
*sc = cloneSystemConfig(cached)
|
||||
return nil
|
||||
}
|
||||
|
||||
if db.Redis != nil {
|
||||
if err := db.HGetJSON(ctx, SystemConfigRedisHashKey, key, sc); err == nil {
|
||||
systemConfigRAMCache.Set(key, cloneSystemConfig(*sc))
|
||||
return nil
|
||||
} else if !errors.Is(err, redis.Nil) {
|
||||
// Redis 服务错误,返回错误
|
||||
@@ -102,10 +110,7 @@ func (sc *SystemConfig) GetByKey(ctx context.Context, key string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新 Redis Hash 缓存
|
||||
if db.Redis != nil {
|
||||
_ = db.HSetJSON(ctx, SystemConfigRedisHashKey, key, sc)
|
||||
}
|
||||
populateSystemConfigCache(ctx, *sc)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user