官术网_书友最值得收藏!

Caching data in memory

To cache a series of values in memory, we will use github.com/patrickmn/go-cache. With this package, we can create an in-memory cache of keys and corresponding values. We can even specify things, such as the time to live, in the cache for specific key-value pairs.

To create a new in-memory cache and set a key-value pair in the cache, we do the following:

// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 30 seconds
c := cache.New(5*time.Minute, 30*time.Second)

// Put a key and value into the cache.
c.Set("mykey", "myvalue", cache.DefaultExpiration)

To then retrieve the value for mykey out of the cache, we just need to use the Get method:

v, found := c.Get("mykey")
if found {
fmt.Printf("key: mykey, value: %s\n", v)
}
主站蜘蛛池模板: 时尚| SHOW| 古浪县| 健康| 彰化市| 泽普县| 通山县| 明水县| 偏关县| 安岳县| 广丰县| 美姑县| 滦平县| 湖北省| 盐源县| 西城区| 麦盖提县| 明水县| 吉安县| 郴州市| 桂阳县| 炎陵县| 长寿区| 五指山市| 子洲县| 文山县| 龙川县| 宜昌市| 邻水| 徐闻县| 宜兴市| 长武县| 远安县| 鄂州市| 建湖县| 赤城县| 承德县| 合作市| 子长县| 浑源县| 海原县|