簡體   English   中英

緩存在內存中並使用go-routine更新的最佳方法是什么?

[英]What is the best way to cache in memory and update with go-routine?

案例:天氣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

那么以這種方式緩存和更新數據是否正確? 還是有什么問題,我應該以更好的方式來做?

基本方法是可以的,但是關於weather有一場數據競賽。 使用互斥量來保護變量:

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的第一個分配,因為可以確保在更新goroutine和ListenAndServer啟動的請求處理程序之前進行ListenAndServer

一種改進是緩存響應主體字節:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM