go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major advantage is that, being essentially a thread-safe map[string]interface{} with expiration times, it doesn’t need to serialize or transmit its contents over the network.
Any object can be stored, for a given duration or forever, and the cache can be safely used by multiple goroutines.
Although go-cache isn’t meant to be used as a persistent datastore, the entire cache can be saved to and loaded from a file (using c.Items() to retrieve the items map to serialize, and NewFrom() to create a cache from a deserialized one) to recover from downtime quickly. (See the docs for NewFrom() for caveats.)
Go-cache 是一个类似于 memcached 的内存键值存储/缓存,适用于运行在单机上的应用步调。它的主要优势在于,它本质上是一个支持线程安全的 map[string]interface{},并具有过期时间,因此不需要序列化或通过网络传输其内容。
任何对象都可以存储,可以设置存储时长或永久存储,并且缓存可以被多个 goroutine 安全使用。
只管 go-cache 并非计划为持久化数据存储,但整个缓存可以保存到文件中或从文件中加载(使用 c.Items() 检索要序列化的项映射,并使用 NewFrom() 从反序列化的缓存创建缓存),以便快速从停机状态规复。(有关 NewFrom() 的留意事项,请拜见文档。)
安装使用
Installation
go get github.com/patrickmn/go-cache
Usage
import (
"fmt"
"github.com/patrickmn/go-cache"
"time"
)
func main() {
// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 10 minutes
c := cache.New(5*time.Minute, 10*time.Minute)
// Set the value of the key "foo" to "bar", with the default expiration time
c.Set("foo", "bar", cache.DefaultExpiration)
// Set the value of the key "baz" to 42, with no expiration time
// (the item won't be removed until it is re-set, or removed using
// c.Delete("baz")
c.Set("baz", 42, cache.NoExpiration)
// Get the string associated with the key "foo" from the cache
foo, found := c.Get("foo")
if found {
fmt.Println(foo)
}
// Since Go is statically typed, and cache values can be anything, type
// assertion is needed when values are being passed to functions that don't
// take arbitrary types, (i.e. interface{}). The simplest way to do this for
// values which will only be used once--e.g. for passing to another
// function--is:
foo, found := c.Get("foo")
if found {
MyFunction(foo.(string))
}
// This gets tedious if the value is used several times in the same function.
// You might do either of the following instead:
if x, found := c.Get("foo"); found {
foo := x.(string)
// ...
}
// or
var foo string
if x, found := c.Get("foo"); found {
foo = x.(string)
}
// ...
// foo can then be passed around freely as a string
groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.
groupcache旨在更换memcached,groupcache 的计划初衷是作为一种分布式缓存办理方案,特殊适用于避免使用外部缓存服务的环境,例如Memcached或Redis。
十分风趣的一点是:does not require running a separate set of servers, thus massively reducing deployment/configuration pain. groupcache is a client library as well as a server. It connects to its own peers, forming a distributed cache.
就是与Redis等其他常用cache实现不同,groupcache并不运行在单独的server上,而是作为library和app运行在同一进程中。所以groupcache既是server也是client。
安装使用
Install:
go get github.com/golang/groupcache
复制代码
Usage:
package main
import (
"fmt"
"log"
"net/http"
"github.com/golang/groupcache"
)
func main() {
// 定义一个名为 "example" 的缓存组,最大缓存容量为 64MB
var cacheGroup = groupcache.NewGroup("example", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
// 模拟缓存失效时的数据获取逻辑
value := "Value for " + key
dest.SetString(value)
return nil
}))
http.HandleFunc("/cache/", func(w http.ResponseWriter, r *http.Request) {