cache2go Go 缓存库开源项目

我要开发同款
匿名用户2017年03月03日
36阅读
所属分类Google Go、程序开发、缓存系统
授权协议BSD

作品详情

cache2go是一个Go的并发安全缓存库,具有到期和访问计数器的功能。

示例代码:

package mainimport (    "github.com/muesli/cache2go"    "fmt"    "time")// Keys & values in cache2go can be off arbitrary types, e.g. a struct.type myStruct struct {    text     string    moreData []byte}func main() {    // Accessing a new cache table for the first time will create it.    cache := cache2go.Cache("myCache")    // We will put a new item in the cache. It will expire after    // not being accessed via Value(key) for more than 5 seconds.    val := myStruct{"This is a test!", []byte{}}    cache.Add("someKey", 5*time.Second, &val)    // Let's retrieve the item from the cache.    res, err := cache.Value("someKey")    if err == nil {        fmt.Println("Found value in cache:", res.Data().(*myStruct).text)    } else {        fmt.Println("Error retrieving value from cache:", err)    }    // Wait for the item to expire in cache.    time.Sleep(6 * time.Second)    res, err = cache.Value("someKey")    if err != nil {        fmt.Println("Item is not cached (anymore).")    }    // Add another item that never expires.    cache.Add("someKey", 0, &val)    // cache2go supports a few handy callbacks and loading mechanisms.    cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {        fmt.Println("Deleting:", e.Key(), e.Data().(*myStruct).text, e.CreatedOn())    })    // Remove the item from the cache.    cache.Delete("someKey")    // And wipe the entire cache table.    cache.Flush()}

 

go run mycachedapp.go
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论