pool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

Pool

一个简单的对象池

License GoDoc Go Report Card

特性

  • 支持最大空闲、最大缓存对象数限制
  • 支持无可用对象时阻塞等待
  • 支持按对象使用次数或时间进行销毁
  • 支持对象数据使用前reset对象内数据

快速开始

type SearchContext struct {
	No    int32
	Param int32
}

func (this *SearchContext) Reset() {
	this.Param = 0
}

p := &Pool{
    New: func() Obj {
        return &SearchContext{No: i}
    },
    Test: func(o Obj) bool {
        if o.(*SearchContext).No == 2 {
            return false
        }
        return true
    },
    MaxActive: 2,
    Wait:      true,
}

// 获取对象
entry := p.Get()
// 使用对象
fmt.Println("---", entry.Obj().Param)
// 将对象重置到缓存池
entry.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

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

func (*Entry) Close

func (this *Entry) Close()

func (*Entry) Obj added in v1.0.0

func (this *Entry) Obj() Obj

type Obj added in v1.0.0

type Obj interface {
	Reset()
}

type Pool

type Pool struct {
	New         func() Obj
	Test        func(Obj) bool
	MaxIdle     int32         // 最大空闲对象数,0为不限制。超出限制的对象Put时将丢弃
	MaxActive   int32         // 最大缓存对象数,0为不限制。超出限制将无实例返回
	MaxUsage    int32         // 对象最大使用次数,0为不限制。超出限制将丢弃
	MaxLifetime time.Duration // 对象最大生存时间,0为不限制。超出限制将丢弃
	Wait        bool          // 当无可用对象返回时,Get是否等待
	// contains filtered or unexported fields
}

func New

func New(fn func() Obj) *Pool

func (*Pool) Active added in v1.0.0

func (this *Pool) Active() int32

func (*Pool) Get

func (this *Pool) Get() *Entry

func (*Pool) Idle added in v1.0.0

func (this *Pool) Idle() int32

Jump to

Keyboard shortcuts

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