context

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseCtx

func ReleaseCtx(ctx Context)

ReleaseCtx returns ctx acquired via AcquireCtx to Context pool.

It is forbidden accessing ctx and/or its' members after returning it to Context pool.

Types

type Context

type Context interface {
	// Get 通过 key 在上下文中获取一个字符串
	Get(key string) string
	// GetAny 通过 key 在上下文中获取一个任意类型
	GetAny(key string) interface{}
	// Put 向上下文中传入一个 key: value
	Put(key string, val interface{})
	// GetAndDelete 获取并删除一个 key
	GetAndDelete(key string) interface{}
	// Delete 在上下文中删除指定的 key
	Delete(key string)
	// ForEach 将上下文中的全部 key 和 value 用传
	// 入的函数处理后返回一个处理结果的切片
	ForEach(func(key string, val interface{}) interface{}) []interface{}
	// Clear 清空一个上下文
	Clear()
	// Length 返回上下文的长度
	Length() int
	// String 将上下文转换为 json(非标准) 字符串
	String() string
	// Bytes 格式化为 json 用的字节切片
	Bytes() []byte
}

func AcquireCtx

func AcquireCtx(ops ...CtxOp) (Context, error)

AcquireCtx returns an empty Context instance from context pool.

The returned Context instance may be passed to ReleaseCtx when it is no longer needed. This allows Context recycling, reduces GC pressure and usually improves performance.

func NewContext

func NewContext(ops ...CtxOp) (Context, error)

NewContext returns a new Context instance

type CtxOp

type CtxOp int

上下文类型

const (
	// 以读为主的上下文,
	// 适用于读操作远多于写的场景
	ReadOp CtxOp = iota
	// 适用于读写各半或写多于读的场景
	WriteOp
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL