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

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)
}
主站蜘蛛池模板: 拜城县| 辽宁省| 恩施市| 镇安县| 贞丰县| 道孚县| 松阳县| 宁津县| 丽水市| 新疆| 喀什市| 东宁县| 邛崃市| 普兰店市| 眉山市| 成武县| 惠水县| 白城市| 达州市| 射阳县| 青川县| 黎城县| 武汉市| 汉阴县| 青河县| 健康| 都昌县| 武义县| 长寿区| 垦利县| 延长县| 准格尔旗| 沁阳市| 咸宁市| 岳普湖县| 个旧市| 定日县| 江油市| 信宜市| 舟山市| 朝阳区|