7
Golang:go-cache基于内存的键值存储缓存库
source link: https://blog.51cto.com/mouday/5765230
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Golang:go-cache基于内存的键值存储缓存库
精选 原创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 github.com/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"
"github.com/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
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK