日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegpool-基本使用

基本使用

package main

import (
    "github.com/GOgf/gf/v2/container/gpool"
    "fmt"
    "time"
)

func main () {
    // 創(chuàng)建一個對象池,過期時間為1秒
    p := gpool.New(time.Second, nil)

    // 從池中取一個對象,返回nil及錯誤信息
    fmt.Println(p.Get())

    // 丟一個對象到池中
    p.Put(1)

    // 重新從池中取一個對象,返回1
    fmt.Println(p.Get())

    // 等待2秒后重試,發(fā)現對象已過期,返回nil及錯誤信息
    time.Sleep(2*time.Second)
    fmt.Println(p.Get())
}

創(chuàng)建及銷毀方法

我們可以給定動態(tài)創(chuàng)建及銷毀方法。

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gpool"
	"github.com/gogf/gf/v2/net/gtcp"
	"github.com/gogf/gf/v2/os/glog"
	"time"
)

func main() {
	// 創(chuàng)建對象復用池,對象過期時間為3秒,并給定創(chuàng)建及銷毀方法
	p := gpool.New(3*time.Second, func() (interface{}, error) {
		return gtcp.NewConn("www.baidu.com:80")
	}, func(i interface{}) {
		glog.Println("expired")
		i.(*gtcp.Conn).Close()
	})
	conn, err := p.Get()
	if err != nil {
		panic(err)
	}
	result, err := conn.(*gtcp.Conn).SendRecv([]byte("HEAD / HTTP/1.1\n\n"), -1)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(result))
	// 丟回池中以便重復使用
	p.Put(conn)
	// 等待一定時間觀察過期方法調用
	time.Sleep(4*time.Second)
}

執(zhí)行后,終端輸出結果:

HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Wed, 29 May 2019 11:23:20 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18


2019-05-29 19:23:24.732 expired

名稱欄目:創(chuàng)新互聯(lián)GoFrame教程:GoFramegpool-基本使用
網站URL:http://m.5511xx.com/article/cdjesjj.html