8.go开源cache2go项目笔记——callbacks调用
2017-09-26 22:01
295 查看
8.go开源cache2go项目笔记——callbacks调用
调用CALLBACK测试,包含main函数
import (
"fmt"
"time"
"cache2go-master"
)
func main() {
cache:= cache2go.Cache("myCache")
cache.SetAddedItemCallback(func(entry*cache2go.CacheItem) {
fmt.Println("Added:",entry.Key(), entry.Data(), entry.CreatedOn())
})
cache.SetAboutToDeleteItemCallback(func(entry*cache2go.CacheItem) {
fmt.Println("Deleting:",entry.Key(), entry.Data(), entry.CreatedOn())
})
cache.Add("someKey",0, "This is a test!")
res,err := cache.Value("someKey")
iferr == nil {
fmt.Println("Foundvalue in cache:", res.Data())
}else {
fmt.Println("Errorretrieving value from cache:", err)
}
cache.Delete("someKey")
res= cache.Add("anotherKey", 3*time.Second, "This is anothertest")
res.SetAboutToExpireCallback(func(keyinterface{}) {
fmt.Println("Aboutto expire:", key.(string))
})
time.Sleep(5* time.Second)
}
Found value in cache: This is a test!
Deleting: someKey This is a test! 2016-07-16 23:46:08.1364223+0800 CST
Added: anotherKey This is another test 2016-07-1623:46:08.1634223 +0800 CST
Deleting: anotherKey This is another test 2016-07-1623:46:08.1634223 +0800 CST
About toexpire: anotherKey
cache. SetAboutToDeleteItemCallback函数是在cachetable.go中定义中实现的接口函数。实现的函数入参为CacheItem的指针,然后输出删除的键、值和创建时间。
cache.Add("someKey", 0,"This is a test!") 函数向CACHE TABLE中加入一个缓存,不过期。键为someKey,值为This is a test.返回cache item指针。
res, err :=cache.Value("someKey") 从cache table中获取键为someKey的值。
cache.Delete("someKey")从cache table中删除键为someKey的值。
res=cache.Add("anotherKey",3*time.Second,"Thisisanothertest")增加一个键为anotherKey,键值This
is another test,持续时间为3秒。返回cache item指针。
res.SetAboutToExpireCallback设置该CACHE ITEM的过期回调函数,定义在cacheitem.go文件中。当过期后调用,调用则会输出键过期。
调用CALLBACK测试,包含main函数
1 代码如下:
package mainimport (
"fmt"
"time"
"cache2go-master"
)
func main() {
cache:= cache2go.Cache("myCache")
cache.SetAddedItemCallback(func(entry*cache2go.CacheItem) {
fmt.Println("Added:",entry.Key(), entry.Data(), entry.CreatedOn())
})
cache.SetAboutToDeleteItemCallback(func(entry*cache2go.CacheItem) {
fmt.Println("Deleting:",entry.Key(), entry.Data(), entry.CreatedOn())
})
cache.Add("someKey",0, "This is a test!")
res,err := cache.Value("someKey")
iferr == nil {
fmt.Println("Foundvalue in cache:", res.Data())
}else {
fmt.Println("Errorretrieving value from cache:", err)
}
cache.Delete("someKey")
res= cache.Add("anotherKey", 3*time.Second, "This is anothertest")
res.SetAboutToExpireCallback(func(keyinterface{}) {
fmt.Println("Aboutto expire:", key.(string))
})
time.Sleep(5* time.Second)
}
2 执行如下:
Added: someKey This is a test! 2016-07-16 23:46:08.1364223+0800 CSTFound value in cache: This is a test!
Deleting: someKey This is a test! 2016-07-16 23:46:08.1364223+0800 CST
Added: anotherKey This is another test 2016-07-1623:46:08.1634223 +0800 CST
Deleting: anotherKey This is another test 2016-07-1623:46:08.1634223 +0800 CST
About toexpire: anotherKey
3 代码说明:
其中cache.SetAddedItemCallback 函数是在cachetable.go中定义中实现的接口函数,将具体的函数指针赋值给cache table结构中的addedItem。本次实现的函数入参为CacheItem的指针,然后输出键、值和创建时间。cache. SetAboutToDeleteItemCallback函数是在cachetable.go中定义中实现的接口函数。实现的函数入参为CacheItem的指针,然后输出删除的键、值和创建时间。
cache.Add("someKey", 0,"This is a test!") 函数向CACHE TABLE中加入一个缓存,不过期。键为someKey,值为This is a test.返回cache item指针。
res, err :=cache.Value("someKey") 从cache table中获取键为someKey的值。
cache.Delete("someKey")从cache table中删除键为someKey的值。
res=cache.Add("anotherKey",3*time.Second,"Thisisanothertest")增加一个键为anotherKey,键值This
is another test,持续时间为3秒。返回cache item指针。
res.SetAboutToExpireCallback设置该CACHE ITEM的过期回调函数,定义在cacheitem.go文件中。当过期后调用,调用则会输出键过期。
相关文章推荐
- 9.go开源cache2go项目笔记——dataloader调用
- 10.go开源cache2go项目笔记——mycachedapp调用
- 7.go开源cache2go项目笔记——benchmark_test文件
- go开源cache2go项目蛤蟆笔记——简单使用-
- 8.go开源groupcache项目笔记——groupcachepb代码
- 1.go开源cache2go项目笔记——简单使用-
- 2.go开源cache2go项目笔记——项目介绍
- 4.go开源cache2go项目笔记——cacheitem文件
- 3.go开源cache2go项目笔记——cachetable文件
- 6.go开源cache2go项目笔记——cache_test文件
- 5.go开源cache2go项目笔记——cache文件
- Android(java)学习笔记207:开源项目使用之gif view
- IOS开源项目Coding学习笔记(2):IntroductionViewController (简介View)
- C白白的游戏服务器开发笔记:创建开源项目LCServer
- iOS学习笔记17-直接拿来用!最火的iOS开源项目(三)
- iOS笔记之项目构成及方法调用
- 开源项目OkHttpPlus——支持GET、POST、UI线程回调、JSON格式解析、链式调用、文件上传下载
- MVC学习笔记---WebViewPage(nop等开源项目的@T)
- Scrapy爬虫系列笔记之九:反爬虫之Useragent设置以及开源项目的结合_by_书訢
- jiecaovideo开源项目笔记-JCVideoPlayer分析-播放逻辑