- 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)
}
推薦閱讀
- C# 7 and .NET Core Cookbook
- Hyper-V 2016 Best Practices
- The Modern C++ Challenge
- 趣學Python算法100例
- JavaScript by Example
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Android 應用案例開發大全(第3版)
- 自然語言處理Python進階
- Mastering Business Intelligence with MicroStrategy
- Unity 2018 Shaders and Effects Cookbook
- C++ System Programming Cookbook
- Android嵌入式系統程序開發(基于Cortex-A8)
- R語言數據分析從入門到實戰
- WCF編程(第2版)
- Appcelerator Titanium Smartphone App Development Cookbook