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
}

参考
Go 每日一库之 go-cache 缓存

猜你喜欢

转载自blog.csdn.net/mouday/article/details/127381888
今日推荐