纯函数实现双重闭包连接池懒加载功能模型,提升性能的利器
作者:
funcx | 来源:发表于
2019-01-06 11:18 被阅读24次package main
import (
"fmt"
"log"
"os"
)
var logger = log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
type conn struct {
id int
}
func main() {
connFunc := connFuncFactory()
getFunc := connFunc("get")
putFunc := connFunc("put")
fmt.Println(getFunc())
fmt.Println(getFunc())
putFunc()
fmt.Println(getFunc())
}
func connFuncFactory() func(def string) func() *conn {
var c *conn
return func(def string) func() *conn {
switch def {
case "get":
return func() *conn {
if c == nil {
logger.Println("新建连接")
c = &conn{id: 1}
}
return c
}
case "put":
return func() *conn {
logger.Println("释放连接")
c = nil
return nil
}
}
panic("panic in case")
}
}
本文标题:纯函数实现双重闭包连接池懒加载功能模型,提升性能的利器
本文链接:https://www.haomeiwen.com/subject/huxsrqtx.html
网友评论