go开源cache2go项目蛤蟆笔记——简单使用-

网友投稿 241 2022-11-03


go开源cache2go项目蛤蟆笔记——简单使用-

1     下载开源

下载路径:import( "fmt" "time" "cache2go-master") //Keys&valuesincache2gocanbeoffarbitrarytypes,e.g.astruct.typemyStructstruct{ text string moreData[]byte} funcmain(){ //Accessinganewcachetableforthefirsttimewillcreateit. cache:=cache2go.Cache("myCache") //Wewillputanewiteminthecache.Itwillexpireafter //notbeingaccessedviaValue(key)formorethan5seconds. val:=myStruct{"Thisisatest!",[]byte{}} cache.Add("someKey",5*time.Second,&val) //Let'sretrievetheitemfromthecache. res,err:=cache.Value("someKey") iferr==nil{ fmt.Println("Foundvalueincache:",res.Data().(*myStruct).text) }else{ fmt.Println("Errorretrievingvaluefromcache:",err) } //Waitfortheitemtoexpireincache. time.Sleep(6*time.Second) res,err=cache.Value("someKey") iferr!=nil{ fmt.Println("Itemisnotcached(anymore).") } //Addanotheritemthatneverexpires. cache.Add("someKey",0,&val) //cache2gosupportsafewhandycallbacksandloadingmechanisms. cache.SetAboutToDeleteItemCallback(func(e*cache2go.CacheItem){ fmt.Println("Deleting:",e.Key(),e.Data().(*myStruct).text,e.CreatedOn()) }) //Removetheitemfromthecache. cache.Delete("someKey") //Andwipetheentirecachetable. cache.Flush()}

3     执行如下

Found value in cache: Thisis a test!

Item is not cached(anymore).

Deleting: someKey This is a test! 2016-07-12 16:31:57.0289334 +0800 CST


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:美国两起并购引发思考:传统行业转型及创业机会何在?
下一篇:邮政快递单号查询API(邮政快递单号查询号码查询物流)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~