[英]What is the best way to cache in memory and update with go-routine?
The Case: Weather API - I will assume that the task is simple and I just want to make an API to return the weather based on another API 案例:天气API-我假设任务很简单,我只想制作一个API以根据另一个API返回天气
The code 编码
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
type ResponseBody struct {
CurrentObservation struct {
Weather string `json:"weather"`
Temperature string `json:"temperature_string"`
DisplayLocation struct {
City string `json:"city"`
} `json:"display_location"`
} `json:"current_observation"`
}
var weather ResponseBody
func main() {
// start the api
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", r)
}
// handler
func HomeHandler(w http.ResponseWriter, r *http.Request) {
// load the weather first
weather = getWeather()
b, _ := json.Marshal(weather)
w.Write(b)
}
// get wether from wunderground api
func getWeather() ResponseBody {
url := "http://api.wunderground.com/api/MY_API_KEY/conditions/q/CA/San_Francisco.json"
req, err := http.NewRequest("GET", url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var rb ResponseBody
json.Unmarshal([]byte(body), &rb)
return rb
}
Now every time someone hits the API it will send a request to the weather API, but this won't be efficient when I will have concurrent requests, so I will cache it in memory and will update the data in a go-routine every one second 现在,每次有人点击该API时,它将向天气API发送一个请求,但是当我有并发请求时,这将不会很有效,因此我会将其缓存在内存中,并在每个例程中更新数据第二
First: I will move the getWeather call to the main function 首先:我将把getWeather调用移至主要功能
func main() {
// load the weather first
weather = getWeather()
// start the api
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", r)
}
// handler
func HomeHandler(w http.ResponseWriter, r *http.Request) {
b, _ := json.Marshal(weather)
w.Write(b)
}
and will start a go-routine in the main function too 并且也会在主函数中启动go-routine
func main() {
// load the weather first
weather = getWeather()
// update data every 1 second
go func() {
for {
time.Sleep(time.Second)
weather = getWeather()
}
}()
// start the api
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", r)
}
so now the application could handle concurrent requests up to minimum 250 concurrent after testing with siege tool 因此,使用攻城工具进行测试后,应用程序现在最多可以处理250个并发请求
Transactions: 250 hits
Availability: 100.00 %
Elapsed time: 0.47 secs
Data transferred: 0.03 MB
Response time: 0.00 secs
Transaction rate: 531.91 trans/sec
Throughput: 0.07 MB/sec
Concurrency: 2.15
Successful transactions: 250
Failed transactions: 0
Longest transaction: 0.04
Shortest transaction: 0.00
So is it right to cache and update data in this way? 那么以这种方式缓存和更新数据是否正确? Or there is something wrong and I should do it in a better way?
还是有什么问题,我应该以更好的方式来做?
The basic approach is OK, but there's a data race on weather
. 基本方法是可以的,但是关于
weather
有一场数据竞赛。 Use a mutex to protect the variable: 使用互斥量来保护变量:
var mu sync.RWMutex
var weather ResponseBody
func main() {
// load the weather first
weather = getWeather()
// update data every 1 second
go func() {
for {
time.Sleep(time.Second)
mu.Lock()
weather = getWeather()
mu.Unlock()
}
}()
// start the api
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", r)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
b, _ := json.Marshal(weather)
mu.RUnlock()
w.Write(b)
}
It is not necessary to guard the first assignment to weather
in main
because the assignment is guaranteed to happen before the updating goroutine and the request handlers started by ListenAndServer
. 不必在
main
维护对weather
的第一个分配,因为可以确保在更新goroutine和ListenAndServer
启动的请求处理程序之前进行ListenAndServer
。
An improvement is to cache the response body bytes: 一种改进是缓存响应主体字节:
var mu sync.RWMutex
var resp []byte
func main() {
// load the weather first
weather := getWeather()
resp, _ = json.Marshal(weather)
// update data every 1 second
go func() {
for {
time.Sleep(time.Second)
mu.Lock()
weather = getWeather()
resp, _ = json.Marshal(weather)
mu.Unlock()
}
}()
// start the api
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", r)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
b := resp
mu.RUnlock()
w.Write(b)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.