gf/.example/container/gpool/gpool.go

27 lines
504 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"time"
"github.com/gogf/gf/container/gpool"
)
func main() {
// 创建一个对象池过期时间为1000毫秒
p := gpool.New(1000*time.Millisecond, nil)
// 从池中取一个对象返回nil及错误信息
fmt.Println(p.Get())
// 丢一个对象到池中
p.Put(1)
// 重新从池中取一个对象返回1
fmt.Println(p.Get())
// 等待1秒后重试发现对象已过期返回nil及错误信息
time.Sleep(time.Second)
fmt.Println(p.Get())
}