cache2go Go 缓存库开源项目

我要开发同款
匿名用户2017年03月03日
122阅读

技术信息

开源地址
https://github.com/muesli/cache2go
授权协议
BSD

作品详情

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

示例代码:

package maiimport (    "github.com/muesli/cache2go"    "fmt"    "time")// Keys & values i cache2go ca be off arbitrary types, e.g. a struct.type myStruct struct {    text     strig    moreData []byte}fuc mai() {    // Accessig a ew cache table for the first time will create it.    cache := cache2go.Cache("myCache")    // We will put a ew item i the cache. It will expire after    // ot beig accessed via Value(key) for more tha 5 secods.    val := myStruct{"This is a test!", []byte{}}    cache.Add("someKey", 5*time.Secod, &val)    // Let's retrieve the item from the cache.    res, err := cache.Value("someKey")    if err == il {        fmt.Pritl("Foud value i cache:", res.Data().(*myStruct).text)    } else {        fmt.Pritl("Error retrievig value from cache:", err)    }    // Wait for the item to expire i cache.    time.Sleep(6 * time.Secod)    res, err = cache.Value("someKey")    if err != il {        fmt.Pritl("Item is ot cached (aymore).")    }    // Add aother item that ever expires.    cache.Add("someKey", 0, &val)    // cache2go supports a few hady callbacks ad loadig mechaisms.    cache.SetAboutToDeleteItemCallback(fuc(e *cache2go.CacheItem) {        fmt.Pritl("Deletig:", e.Key(), e.Data().(*myStruct).text, e.CreatedO())    })    // Remove the item from the cache.    cache.Delete("someKey")    // Ad wipe the etire cache table.    cache.Flush()}

 

go ru mycachedapp.go

功能介绍

cache2go 是一个 Go 的并发安全缓存库,具有到期和访问计数器的功能。 示例代码:
package main

import (
    "github.com/muesli/...

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

评论