utils

package
v3.1.33 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearMap added in v3.1.31

func ClearMap[M ~map[K]V, K comparable, V any](m M)

ClearMap 清空 map 中的所有 keys,保留底层存储供复用。 适用于与对象池配合使用:对象从池中取出后 map 非 nil, 归还前调用 ClearMap 而非置 nil,避免下次使用时重新 make。

var pool = utils.NewPool(func() *MyObj {
    return &MyObj{Data: make(map[string]int, 8)}
})
obj := pool.Get()
defer pool.Put(obj)
// ... 使用 obj.Data ...
defer utils.ClearMap(obj.Data) // 在 Put 前清空

Types

type BufferPool added in v3.1.31

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool 复用 *bytes.Buffer 的对象池。 Get 时自动 Reset,调用方无需再手动 Reset。

var pool = utils.NewBufferPool()
buf := pool.Get()
defer pool.Put(buf)
buf.WriteString("hello")

func NewBufferPool added in v3.1.31

func NewBufferPool() *BufferPool

NewBufferPool 创建 *bytes.Buffer 复用池。

func (*BufferPool) Get added in v3.1.31

func (p *BufferPool) Get() *bytes.Buffer

Get 获取一个已 Reset 的 *bytes.Buffer。

func (*BufferPool) Put added in v3.1.31

func (p *BufferPool) Put(buf *bytes.Buffer)

Put 归还 *bytes.Buffer。nil 将被忽略。

type BytePool added in v3.1.31

type BytePool struct {
	// contains filtered or unexported fields
}

BytePool 复用 *[]byte 的对象池。 支持最大容量限制:归还时若 cap 超过限制则丢弃,避免偶发大缓冲区常驻池中导致内存膨胀。

// 初始 cap=256,不限制归还上限
var pool = utils.NewBytePool(256)

// 初始 cap=4096,归还上限 64KB(超过则丢弃)
var pool = utils.NewBytePoolWithMax(4096, 64*1024)
buf := pool.Get()
defer pool.Put(buf)
*buf = append((*buf)[:0], data...)

func NewBytePool added in v3.1.31

func NewBytePool(initCap int) *BytePool

NewBytePool 创建 *[]byte 复用池,初始容量为 initCap。 归还时不限制缓冲区容量。

func NewBytePoolWithMax added in v3.1.31

func NewBytePoolWithMax(initCap, maxCap int) *BytePool

NewBytePoolWithMax 创建 *[]byte 复用池,并设置归还时的容量上限。 若归还时 cap(*buf) > maxCap,缓冲区将被丢弃(不归还), 防止偶发大缓冲区常驻池中导致内存膨胀。

func (*BytePool) Get added in v3.1.31

func (p *BytePool) Get() *[]byte

Get 获取一个长度为 0 的 *[]byte(容量保持为池化时的容量)。

func (*BytePool) Put added in v3.1.31

func (p *BytePool) Put(b *[]byte)

Put 归还 *[]byte。nil 或容量超过 maxCap 的缓冲区将被丢弃。

type CircularBuffer

type CircularBuffer[T comparable] struct {
	// contains filtered or unexported fields
}

func NewCircularBuffer

func NewCircularBuffer[T comparable](capacity int) *CircularBuffer[T]

func (*CircularBuffer[T]) Contains

func (cb *CircularBuffer[T]) Contains(value T) bool

func (*CircularBuffer[T]) Delete

func (cb *CircularBuffer[T]) Delete(value T)

func (*CircularBuffer[T]) Save

func (cb *CircularBuffer[T]) Save(value T) bool

func (*CircularBuffer[T]) Values

func (cb *CircularBuffer[T]) Values() []T

Values 返回当前缓冲区中的所有值(按插入顺序)

type CircularMapBuffer

type CircularMapBuffer[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewInt64CircularMapBuffer

func NewInt64CircularMapBuffer[T any](capacity int) *CircularMapBuffer[int64, T]

func NewStringCircularMapBuffer

func NewStringCircularMapBuffer[T any](capacity int) *CircularMapBuffer[string, T]

func (*CircularMapBuffer[K, T]) Delete

func (cb *CircularMapBuffer[K, T]) Delete(id K)

func (*CircularMapBuffer[K, T]) Find

func (cb *CircularMapBuffer[K, T]) Find(fn func(key K, value T) bool) (K, T, bool)

添加回调函数处理查找数据

func (*CircularMapBuffer[K, T]) Get

func (cb *CircularMapBuffer[K, T]) Get(id K) (T, bool)

func (*CircularMapBuffer[K, T]) Save

func (cb *CircularMapBuffer[K, T]) Save(id K, value T)

func (*CircularMapBuffer[K, T]) Values

func (cb *CircularMapBuffer[K, T]) Values() []T

Values 返回当前缓冲区中的所有值(按插入顺序)

type Pool added in v3.1.31

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool 泛型对象池,封装 sync.Pool 提供类型安全的 Get/Put。 适用于任意需要复用的对象(如 *struct、*Decoder 等)。

var decoderPool = utils.NewPool(func() *schema.Decoder {
    d := schema.NewDecoder()
    d.IgnoreUnknownKeys(true)
    return d
})
decoder := decoderPool.Get()
defer decoderPool.Put(decoder)

注意:归还前应由调用方清理对象状态,避免引用泄漏。

func NewPool added in v3.1.31

func NewPool[T any](newFn func() T) *Pool[T]

NewPool 创建泛型对象池。newFn 必须返回新的实例,nil 会 panic。

func (*Pool[T]) Get added in v3.1.31

func (p *Pool[T]) Get() T

Get 从池中获取一个对象,如果池为空则调用 newFn 创建新的。

func (*Pool[T]) Put added in v3.1.31

func (p *Pool[T]) Put(x T)

Put 将对象归还到池中。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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