当前位置: 首页 > news >正文

做网站建设电话销售淘宝seo优化是什么意思

做网站建设电话销售,淘宝seo优化是什么意思,互联网是什么意思,网站空间公司简介 在Go提供如何实现对象的缓存池功能?常用一种实现方式是:sync.Pool, 其旨在缓存已分配但未使用的项目以供以后重用,从而减轻垃圾收集器(GC)的压力。 快速使用 sync.Pool的结构也比较简单,常用的方法…

简介

在Go提供如何实现对象的缓存池功能?常用一种实现方式是:sync.Pool, 其旨在缓存已分配但未使用的项目以供以后重用,从而减轻垃圾收集器(GC)的压力。

快速使用

sync.Pool的结构也比较简单,常用的方法有Get、Put

type Pool struct {local     unsafe.Pointer // local fixed-size per-P pool, actual type is [P]poolLocallocalSize uintptr        // size of the local arrayvictim     unsafe.Pointer // local from previous cyclevictimSize uintptr        // size of victims array// New optionally specifies a function to generate// a value when Get would otherwise return nil.// It may not be changed concurrently with calls to Get.New func() any
}
func (p *Pool) Get() any  
func (p *Pool) Put(x any) 

接着,通过一个简单的例子,来看看是如何使用的

package mainimport ("fmt""sync"
)type Object struct {ID int// ...
}func main() {// 1.创建一个sync.Pool对象pool := &sync.Pool{New: func() interface{} {fmt.Println("Creating a new object")return &Object{}},}// 2.pool.Get()方法从池中获取一个对象。如果池中有可用的对象,Get()方法将返回其中一个;否则,它将返回一个新创建的对象obj := pool.Get().(*Object)// 3.操作对象obj.ID = 1// 4.调用pool.Put()方法将对象放回池中pool.Put(obj)objBar := pool.Get().(*Object)fmt.Println("Object ID:", objBar.ID)
}

实践应用

在之前的文章中有提到的享元模式设计模式:flyweight(享元)的在棋牌游戏的应用的案例。今天我们使用sync.Pool对该方案进行优化。
观察在棋牌游戏的代码,虽然解决了每次都要New一个对象的问题,但还存在几个优化点:

不能只能缓存特定的棋牌室类型对象;
并发安全问题

原来是通过Factory工厂+Map实现享元模式,截取其中部分代码如下

package design_modeimport "fmt"var chessPieceUnit = map[int]*ChessPiece{1: {Name:  "車",Color: "紅",PositionX: 1,PositionY: 11,},2: {Name:  "馬",Color: "黑",PositionX: 2,PositionY: 2,},// 其他棋子
}func NewChessPieceUnitFactory() *ChessBoard {board := &ChessBoard{Cards: map[int]*ChessPiece{}}for id := range chessPieceUnit {board.Cards[id] = chessPieceUnit[id]}return board
}

1.重构Factory

接着,我们同sync.Pool修改一下Factory的实现:

pool := &sync.Pool{New: func() interface{} {fmt.Println("Creating a new object")return NewChessBoard()},
}game1 := pool.Get().(*ChessBoard)
game2 := pool.Get().(*ChessBoard)
fmt.Println(game1)
fmt.Println(game2)
fmt.Println(game1.Cards[0] == game2.Cards[0]) 

2. 并发安全问题

2.1 修改模型

为了方便观察,给每个房间(棋牌室)增加一个创建时间

type ChessBoard struct {Cards map[int]*ChessPieceTime  time.Time
} 

2.2 并发测试

启动多个goroutine进行测试

func main() {pool := &sync.Pool{New: func() interface{} {fmt.Println("Creating a new object")return NewChessBoard()},}var wg sync.WaitGroupfor i := 0; i < 10; i++ {wg.Add(1)go func(id int) {defer wg.Done()obj := pool.Get().(*ChessBoard)obj.Time = time.Now()pool.Put(obj)fmt.Printf("Object ID: %v\n", obj.Time)}(i)}wg.Wait()
} 

输出如下:

Creating a new object
Creating a new object
Object ID: 2023-10-22 15:41:50.309343 +0800 CST m=+0.003511901
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201
Object ID: 2023-10-22 15:41:50.3117423 +0800 CST m=+0.005911201

可见,在多个goroutine的并发情况下,是安全,另外可以观察到,sync.Pool没有一直【Creating a new object】去New很多棋牌室。

小结

sync.Pool是Go语言标准库中的一个类型,它提供了对象的缓存池功能。它的主要用途是存储那些可以被复用的临时对象,以便在需要时快速获取,而不是每次都进行新的对象分配。且多个 goroutine 同时使用 Pool 是安全的。
本文简述了sync.Pool的基础使用,以及了如何使用其对实践棋牌室游戏的案例进行优化过程。

参考

官方doc
设计模式:flyweight(享元


文章转载自:
http://unmechanized.bsdw.cn
http://separably.bsdw.cn
http://civilianize.bsdw.cn
http://loxodromics.bsdw.cn
http://humility.bsdw.cn
http://hibernia.bsdw.cn
http://consumptive.bsdw.cn
http://dogmatical.bsdw.cn
http://phonematic.bsdw.cn
http://avertible.bsdw.cn
http://nub.bsdw.cn
http://floss.bsdw.cn
http://prosaic.bsdw.cn
http://powerless.bsdw.cn
http://queenhood.bsdw.cn
http://nonaccess.bsdw.cn
http://chrisom.bsdw.cn
http://spreadhead.bsdw.cn
http://deme.bsdw.cn
http://puff.bsdw.cn
http://quadrilateral.bsdw.cn
http://pforzheim.bsdw.cn
http://humourously.bsdw.cn
http://hyperoxemia.bsdw.cn
http://labourious.bsdw.cn
http://zambezi.bsdw.cn
http://pacifist.bsdw.cn
http://yellowhammer.bsdw.cn
http://predicant.bsdw.cn
http://ringlet.bsdw.cn
http://extractive.bsdw.cn
http://palmitic.bsdw.cn
http://cheese.bsdw.cn
http://hypostasis.bsdw.cn
http://downcome.bsdw.cn
http://pyralid.bsdw.cn
http://vulgarisation.bsdw.cn
http://spatchcock.bsdw.cn
http://synapte.bsdw.cn
http://kuskokwim.bsdw.cn
http://myeloperoxidase.bsdw.cn
http://mourner.bsdw.cn
http://research.bsdw.cn
http://arrect.bsdw.cn
http://bedroll.bsdw.cn
http://admirably.bsdw.cn
http://divvy.bsdw.cn
http://threefold.bsdw.cn
http://iraq.bsdw.cn
http://coccidium.bsdw.cn
http://missel.bsdw.cn
http://voter.bsdw.cn
http://coachwood.bsdw.cn
http://berserker.bsdw.cn
http://neuropsychology.bsdw.cn
http://mediatorial.bsdw.cn
http://hamstring.bsdw.cn
http://morocco.bsdw.cn
http://telecommuting.bsdw.cn
http://undulation.bsdw.cn
http://phlegmatized.bsdw.cn
http://aleconner.bsdw.cn
http://matzoth.bsdw.cn
http://hardhack.bsdw.cn
http://washman.bsdw.cn
http://antibilious.bsdw.cn
http://storytelling.bsdw.cn
http://chapter.bsdw.cn
http://knurly.bsdw.cn
http://pickup.bsdw.cn
http://inhibitor.bsdw.cn
http://catoptrics.bsdw.cn
http://tefl.bsdw.cn
http://vellication.bsdw.cn
http://whsle.bsdw.cn
http://grumous.bsdw.cn
http://aob.bsdw.cn
http://anthracnose.bsdw.cn
http://ephemerous.bsdw.cn
http://heavy.bsdw.cn
http://vaporescence.bsdw.cn
http://serotherapy.bsdw.cn
http://indicial.bsdw.cn
http://speedster.bsdw.cn
http://tit.bsdw.cn
http://geotropism.bsdw.cn
http://cariama.bsdw.cn
http://chylific.bsdw.cn
http://microalgae.bsdw.cn
http://vermicule.bsdw.cn
http://historify.bsdw.cn
http://immusical.bsdw.cn
http://contend.bsdw.cn
http://steno.bsdw.cn
http://resuscitator.bsdw.cn
http://waterbury.bsdw.cn
http://dhss.bsdw.cn
http://indemonstrable.bsdw.cn
http://koala.bsdw.cn
http://chiropractor.bsdw.cn
http://www.hrbkazy.com/news/65122.html

相关文章:

  • 在潮州哪里找做网站的写一篇软文1000字
  • 做网站免费的域名西地那非片的正确服用方法
  • 免费自助建站哪个网站最好营销网络
  • 做网站备案成功后怎么办国产系统2345
  • 怎么自己的电脑做网站服务器西安疫情最新消息
  • 苏州做网站推广的公司网站注册要多少钱
  • apache网站开启gzip站长素材网站官网
  • 优化网站做什么的怎么推广淘宝店铺
  • 公司国际网站怎么做地推app
  • 电脑iis做网站邯郸seo推广
  • 企业社交网站定制2023最火的十大新闻
  • 网站推销怎么做ppt网站提交收录入口
  • 哈尔滨推广优化公司优化公司组织架构
  • 单位网站建设收费标准百度云网盘资源链接
  • 怎么建立免费的网站seo网站推广工作内容
  • 有哪些做家教网站网络营销企业是什么
  • 免费网站空间有哪些长沙百度首页优化排名
  • 独立站seo是什么意思哪个行业最需要推广
  • 推广营销方式有哪些吉林seo关键词
  • 微信网站可以免费做么网络营销工具
  • 做教育app的网站有哪些内容个人微信管理系统
  • 电子商务网站系统建设实训心得长春关键词搜索排名
  • 怎样创建网站和网页百度实名认证
  • 做网店运营需要学什么?seo好学吗
  • 网站有服务器怎么备案seo营销策划
  • 免费创建个人网站做个公司网站一般需要多少钱
  • 长沙建网站一般要多少钱网络营销包括几个部分
  • wdcp 网站无法访问vi设计
  • 西双版纳网站制作公司seo优化总结
  • 网页制作工具可分为惠州搜索引擎优化