- 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)
}
推薦閱讀
- Unreal Engine Physics Essentials
- Python數據可視化:基于Bokeh的可視化繪圖
- JavaScript+jQuery開發實戰
- MATLAB應用與實驗教程
- 基于差分進化的優化方法及應用
- Linux命令行與shell腳本編程大全(第4版)
- Unity 5 for Android Essentials
- QGIS Python Programming Cookbook(Second Edition)
- 單片機原理及應用技術
- Instant Apache Camel Messaging System
- Selenium WebDriver Practical Guide
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- Flask開發Web搜索引擎入門與實戰
- Wearable:Tech Projects with the Raspberry Pi Zero
- SQL Server 2008實用教程(第3版)