- Machine Learning With Go
- Daniel Whitenack
- 135字
- 2021-07-08 10:37:28
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)
}
推薦閱讀
- OpenDaylight Cookbook
- 深度學習經典案例解析:基于MATLAB
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- 趣學Python算法100例
- MATLAB應用與實驗教程
- 程序員修煉之道:通向務實的最高境界(第2版)
- 區塊鏈底層設計Java實戰
- Machine Learning in Java
- 深度探索Go語言:對象模型與runtime的原理特性及應用
- 物聯網系統架構設計與邊緣計算(原書第2版)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- SEO教程:搜索引擎優化入門與進階(第3版)
- 實戰Python網絡爬蟲
- C/C++代碼調試的藝術(第2版)