1 Star 0 Fork 0

wkk/self

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dataloader.go 918 Bytes
一键复制 编辑 原始数据 按行查看 历史
wkk 提交于 2021-06-16 10:21 +08:00 . 修改包结构
package main
import (
"fmt"
"gitee.com/wkkcool/self/cache2go"
"strconv"
)
func main() {
cache := cache2go.Cache("myCache")
// The data loader gets called automatically whenever something
// tries to retrieve a non-existing key from the cache.
cache.SetDataLoader(func(key interface{}, args ...interface{}) *cache2go.CacheItem {
// Apply some clever loading logic here, e.g. read values for
// this key from database, network or file.
val := "This is a test with key " + key.(string)
// This helper method creates the cached item for us. Yay!
item := cache2go.NewCacheItem(key, 0, val)
return item
})
// Let's retrieve a few auto-generated items from the cache.
for i := 0; i < 10; i++ {
res, err := cache.Value("someKey_" + strconv.Itoa(i))
if err == nil {
fmt.Println("Found value in cache:", res.Data())
} else {
fmt.Println("Error retrieving value from cache:", err)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wkkcool/self.git
git@gitee.com:wkkcool/self.git
wkkcool
self
self
f94b3ec4ded5

搜索帮助