9.go开源cache2go项目笔记——dataloader调用
2017-09-26 22:01
316 查看
9.go开源cache2go项目笔记——dataloader调用
测试dataloader相关功能。
Found value in cache: Thisis a test with key someKey_1
Found value in cache: Thisis a test with key someKey_2
Found value in cache: Thisis a test with key someKey_3
Found value in cache: Thisis a test with key someKey_4
Found value in cache: Thisis a test with key someKey_5
Found value in cache: Thisis a test with key someKey_6
Found value in cache: Thisis a test with key someKey_7
Found value in cache: Thisis a test with key someKey_8
Found value in cache: This is a test with key someKey_9
cache.SetDataLoader(func(keyinterface{},args...interface{}) 设置函数loadData函数,入参是CACHE ITEM的指针。当从CACHE TABLE中获取不存在的键时候会调用该函数,其中调用CreateCacheItem函数来创建CACHE
ITEM。
然后一个循环10次,查找CACHE值,因为查找之前没有加载所以每次查找都不存在,然后就调用SetDataLoader(这个通过value函数来调用loadData)来创建CACHE。
测试dataloader相关功能。
1 代码
packagemain
import(
"fmt"
"strconv"
"github.com/muesli/cache2go"
)
funcmain(){
cache:=cache2go.Cache("myCache")
cache.SetDataLoader(func(keyinterface{},args...interface{})*cache2go.CacheItem{
val:="Thisisatestwithkey"+key.(string)
item:=cache2go.CreateCacheItem(key,0,val)
return&item
})
fori:=0;i<10;i++{
res,err:=cache.Value("someKey_"+strconv.Itoa(i))
iferr==nil{
fmt.Println("Foundvalueincache:",res.Data())
}else{
fmt.Println("Errorretrievingvaluefromcache:",err)
}
}
}
2 执行如下
Found value in cache: Thisis a test with key someKey_0Found value in cache: Thisis a test with key someKey_1
Found value in cache: Thisis a test with key someKey_2
Found value in cache: Thisis a test with key someKey_3
Found value in cache: Thisis a test with key someKey_4
Found value in cache: Thisis a test with key someKey_5
Found value in cache: Thisis a test with key someKey_6
Found value in cache: Thisis a test with key someKey_7
Found value in cache: Thisis a test with key someKey_8
Found value in cache: This is a test with key someKey_9
3 代码说明
cache:=cache2go.Cache("myCache")创建一个CACHE TABLE。cache.SetDataLoader(func(keyinterface{},args...interface{}) 设置函数loadData函数,入参是CACHE ITEM的指针。当从CACHE TABLE中获取不存在的键时候会调用该函数,其中调用CreateCacheItem函数来创建CACHE
ITEM。
然后一个循环10次,查找CACHE值,因为查找之前没有加载所以每次查找都不存在,然后就调用SetDataLoader(这个通过value函数来调用loadData)来创建CACHE。
相关文章推荐
- 2.go开源cache2go项目笔记——项目介绍
- 8.go开源cache2go项目笔记——callbacks调用
- 5.go开源cache2go项目笔记——cache文件
- 10.go开源cache2go项目笔记——mycachedapp调用
- go开源cache2go项目蛤蟆笔记——简单使用-
- 6.go开源cache2go项目笔记——cache_test文件
- 1.go开源cache2go项目笔记——简单使用-
- 7.go开源cache2go项目笔记——benchmark_test文件
- 14.go开源groupcache项目笔记——peers代码
- 3.go开源cache2go项目笔记——cachetable文件
- 12.go开源groupcache项目笔记——byteview代码
- 4.go开源groupcache项目笔记——关于strconv
- 5.go开源groupcache项目笔记——关于testing
- 7.go开源groupcache项目笔记——关于gocode代码自动补齐
- 4.go开源cache2go项目笔记——cacheitem文件
- 9.go开源groupcache项目笔记——lru代码
- Go语言学习笔记(5)-go工具和项目
- Go语言诞生5周年!10大Go语言开源项目推荐
- Go笔记之一:工程项目结构的注意事项
- 【Git学习笔记】使用GitHub参与开源项目