An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
译文:Go的内存 key:value store/cache(类似于Memcached)库,适用于单机应用程序。
安装
go get /patrickmn/go-cache
方法签名
func New(defaultExpiration, cleanupInterval time.Duration) *Cache func (c *cache) Set(k string, x interface{}, d time.Duration) func (c *cache) Get(k string) (interface{}, bool)
示例
package main import ( "fmt" "log" "time" "/patrickmn/go-cache" ) func main() { // 设置默认过期时间10秒;清理间隔30分钟 caching := cache.New(10*time.Second, 30*time.Minute) // 设置过期时间 caching.Set("key", "value", 10*time.Second) // 获取数据 value, ok := caching.Get("key") if !ok { log.Fatal() } fmt.Printf("value: %v\n", value) // value: value }