mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
为`gcfg`添加配置文件变更自定义回调,实现了`WatcherAdapter`接口,以下是`AdapterFile`的用法
test.yaml
```
b: "b"
```
```
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
ctx := gctx.New()
file, _ := gcfg.NewAdapterFile("test.yaml")
file.Data(ctx)
file.AddWatcher("test", func() {
value := file.MustGet(ctx, "b")
fmt.Println(value.String())
})
server := g.Server()
server.Run()
}
```
使用`g`和默认配置文件
```
file := g.Cfg().GetAdapter().(*gcfg.AdapterFile)
file.AddWatcher("test", func() {
})
file := g.Cfg().GetAdapter().(*gcfg.AdapterFile)
file.RemoveWatcher("test")
```
注意:由于`gf`的`AdapterFile`使用的监听到文件变化删除缓存下一次重新初始化的懒加载方案,所有除了默认加载的`config.xxx`文件外,自定义的配置文件像`test.yaml`之类的都需要在`AddWatcher`前主动读取一次数据进行初始化监听(
`g.Cfg("test").Data(ctx)`)
---------
Co-authored-by: hailaz <739476267@qq.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Hunk Zhu <hunk@joy999.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
158 lines
4.6 KiB
Go
158 lines
4.6 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package gcfg provides reading, caching and managing for configuration.
|
|
package gcfg
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
)
|
|
|
|
// AdapterFileCtx is the context for AdapterFile.
|
|
type AdapterFileCtx struct {
|
|
// Ctx is the context with configuration values
|
|
Ctx context.Context
|
|
}
|
|
|
|
// NewAdapterFileCtxWithCtx creates and returns a new AdapterFileCtx with the given context.
|
|
func NewAdapterFileCtxWithCtx(ctx context.Context) *AdapterFileCtx {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return &AdapterFileCtx{Ctx: ctx}
|
|
}
|
|
|
|
// NewAdapterFileCtx creates and returns a new AdapterFileCtx.
|
|
// If ctx is provided, it uses that context, otherwise it creates a background context.
|
|
func NewAdapterFileCtx(ctx ...context.Context) *AdapterFileCtx {
|
|
if len(ctx) > 0 {
|
|
return NewAdapterFileCtxWithCtx(ctx[0])
|
|
}
|
|
return NewAdapterFileCtxWithCtx(context.Background())
|
|
}
|
|
|
|
// GetAdapterFileCtx creates and returns an AdapterFileCtx with the given context.
|
|
func GetAdapterFileCtx(ctx context.Context) *AdapterFileCtx {
|
|
return NewAdapterFileCtxWithCtx(ctx)
|
|
}
|
|
|
|
// WithFileName sets the file name in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithFileName(fileName string) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyFileName, fileName)
|
|
return a
|
|
}
|
|
|
|
// WithFilePath sets the file path in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithFilePath(filePath string) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyFilePath, filePath)
|
|
return a
|
|
}
|
|
|
|
// WithFileType sets the file type in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithFileType(fileType string) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyFileType, fileType)
|
|
return a
|
|
}
|
|
|
|
// WithOperation sets the operation in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithOperation(operation OperationType) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyOperation, operation)
|
|
return a
|
|
}
|
|
|
|
// WithKey sets the key in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithKey(key string) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyKey, key)
|
|
return a
|
|
}
|
|
|
|
// WithValue sets the value in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithValue(value any) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyValue, value)
|
|
return a
|
|
}
|
|
|
|
// WithContent sets the content in the context and returns the updated AdapterFileCtx.
|
|
func (a *AdapterFileCtx) WithContent(content any) *AdapterFileCtx {
|
|
a.Ctx = context.WithValue(a.Ctx, ContextKeyContent, content)
|
|
return a
|
|
}
|
|
|
|
// GetFileName retrieves the file name from the context.
|
|
// Returns empty string if not found.
|
|
func (a *AdapterFileCtx) GetFileName() string {
|
|
if v := a.Ctx.Value(ContextKeyFileName); v != nil {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetFilePath retrieves the file path from the context.
|
|
// Returns empty string if not found.
|
|
func (a *AdapterFileCtx) GetFilePath() string {
|
|
if v := a.Ctx.Value(ContextKeyFilePath); v != nil {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetFileType retrieves the file type from the context.
|
|
// Returns empty string if not found.
|
|
func (a *AdapterFileCtx) GetFileType() string {
|
|
if v := a.Ctx.Value(ContextKeyFileType); v != nil {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetOperation retrieves the operation from the context.
|
|
// Returns empty string if not found.
|
|
func (a *AdapterFileCtx) GetOperation() OperationType {
|
|
if v := a.Ctx.Value(ContextKeyOperation); v != nil {
|
|
if s, ok := v.(OperationType); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetKey retrieves the key from the context.
|
|
// Returns empty string if not found.
|
|
func (a *AdapterFileCtx) GetKey() string {
|
|
if v := a.Ctx.Value(ContextKeyKey); v != nil {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetValue retrieves the value from the context.
|
|
// Returns nil if not found.
|
|
func (a *AdapterFileCtx) GetValue() *gvar.Var {
|
|
if v := a.Ctx.Value(ContextKeyValue); v != nil {
|
|
return gvar.New(v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetContent retrieves the set content from the context.
|
|
// Returns nil if not found.
|
|
func (a *AdapterFileCtx) GetContent() *gvar.Var {
|
|
if v := a.Ctx.Value(ContextKeyContent); v != nil {
|
|
return gvar.New(v)
|
|
}
|
|
return nil
|
|
}
|