gpt4 book ai didi

caching - 在内存中缓存并使用 go-routine 更新的最佳方法是什么?

转载 作者:IT王子 更新时间:2023-10-29 01:26:14 25 4
gpt4 key购买 nike

案例:天气 API - 我假设任务很简单,我只想制作一个 API 以根据另一个 API 返回天气

代码

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
}

现在,每次有人点击 API 时,它都会向天气 API 发送请求,但是当我有并发请求时,这不会有效,所以我会将它缓存在内存中,并在运行中更新数据-每一秒例行

首先:我会将 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)
}

并且也会在主函数中启动一个 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)
}

所以现在应用程序在使用攻城工具测试后可以处理最多 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

那么这样缓存和更新数据是否正确呢?或者有什么地方不对,我应该用更好的方式来做?

最佳答案

基本方法没问题,但天气 存在数据竞争。使用mutex保护变量:

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)
}

没有必要在 main 中保护对 weather 的第一个赋值,因为赋值是 guaranteed to happen before ListenAndServer 启动的更新 goroutine 和请求处理程序。

一个改进是缓存响应体字节:

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)
}

关于caching - 在内存中缓存并使用 go-routine 更新的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com