This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhuyujia-yydns/att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/metadata/README.md
2023-11-24 10:33:18 +08:00

1.2 KiB
Raw Blame History

metadata

简介

metadata包提供了一个 API允许插件将元数据添加到上下文中。每个元数据都存储在格式为<plugin>/<name> 的标签下。每个元数据作为 Func 返回。调用 Func 时返回元数据。如果某个 Func 执行时间很长,就需要自行提供某种形式的缓存。在处理一个查询时的元数据应该保持不变。

用例

Basic example:

//
// Implement the Provider interface for a plugin p:
//
func (p P) Metadata(ctx context.Context, state request.Request) context.Context {
	metadata.SetValueFunc(ctx, "test/something", func() string { 
		   return "myvalue" 
	   })
	return ctx
    }

Basic example with caching:

func (p P) Metadata(ctx context.Context, state request.Request) context.Context {
	cached := ""
	f := func() string {
	    if cached != "" {
		    return cached
	    }
	    cached = expensiveFunc()
	    return cached
	}
	metadata.SetValueFunc(ctx, "test/something", f)
	return ctx
    }

If you need access to this metadata from another plugin:


    // ...
    valueFunc := metadata.ValueFunc(ctx, "test/something")
    value := valueFunc()
    // use 'value'