Golang 缓存来自 net/http 处理程序的结果。

huangapple go评论85阅读模式
英文:

Golang cache result from handler net/http

问题

无法从缓存中获取结果。只能处理对基础请求的请求。我需要优化应用程序以处理多个请求。这是我使用golang的第一个应用程序,请多包涵。如何获取缓存的结果?

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/ip2location/ip2proxy-go"
	"github.com/patrickmn/go-cache"
)

func main() {
	http.HandleFunc("/", HelloHandler)
	http.ListenAndServe(":8080", nil)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func HelloHandler(w http.ResponseWriter, r *http.Request) {
	c := cache.New(5*time.Minute, 10*time.Minute)
	ip := r.URL.Query().Get("ip")
	x, found := c.Get(ip)
	if found {
		if x != "0" {
			w.WriteHeader(http.StatusOK)
			fmt.Fprintf(w, "isProxy Cache")
		} else {
			w.WriteHeader(http.StatusNoContent)
			fmt.Fprintf(w, "notProxy Cache")
		}

	} else {
		db, err := ip2proxy.OpenDB("./IP2PROXY-IP-PROXYTYPE-COUNTRY.BIN")
		if err != nil {
			return
		}
		all, err := db.GetAll(ip)
		if err != nil {
			fmt.Print(err)
			return
		}
		isProxy := all["isProxy"]
		c.Set(ip, isProxy, cache.DefaultExpiration)
		if isProxy != "0" {
			w.WriteHeader(http.StatusOK)
			fmt.Fprintf(w, "isProxy ")
		} else {
			w.WriteHeader(http.StatusNoContent)
			fmt.Fprintf(w, "notProxy ")
		}
		db.Close()
	}
}

这里的all["isProxy"]返回0、1、-1的值。

英文:

Can't get result from cache. Work only requests to base. I need optimise app for many requests. This my first app with golang, be indulgent pls. How get cached result?

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/ip2location/ip2proxy-go"
	"github.com/patrickmn/go-cache"
)

func main() {
	http.HandleFunc("/", HelloHandler)
	http.ListenAndServe(":8080", nil)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
	c := cache.New(5*time.Minute, 10*time.Minute)
	ip := r.URL.Query().Get("ip")
	x, found := c.Get(ip)
	if found {
		if x != "0" {
			w.WriteHeader(http.StatusOK)
			fmt.Fprintf(w, "isProxy Cache")
		} else {
			w.WriteHeader(http.StatusNoContent)
			fmt.Fprintf(w, "notProxy Cache")
		}

	} else {
		db, err := ip2proxy.OpenDB("./IP2PROXY-IP-PROXYTYPE-COUNTRY.BIN")
		if err != nil {
			return
		}
		all, err := db.GetAll(ip)
		if err != nil {
			fmt.Print(err)
			return
		}
		isProxy := all["isProxy"]
		c.Set(ip, isProxy, cache.DefaultExpiration)
		if isProxy != "0" {
			w.WriteHeader(http.StatusOK)
			fmt.Fprintf(w, "isProxy ")
		} else {
			w.WriteHeader(http.StatusNoContent)
			fmt.Fprintf(w, "notProxy ")
		}
		db.Close()
	}
}

This: all["isProxy"] return 0, 1, -1 values

答案1

得分: 2

创建一个全局变量并将缓存存储在其中。示例代码如下:

var globalCache *cache.Cache

func main() {
    globalCache = cache.New(5*time.Minute, 10*time.Minute)

    http.HandleFunc("/", HelloHandler)
    http.ListenAndServe(":8080", nil)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func HelloHandler(w http.ResponseWriter, r *http.Request) {
    // 使用 globalCache
}

更新:由于 cache.Cache 内部已经使用了锁,所以不需要使用 atomic。感谢您的纠正。

英文:

Make a global variable and store the cache there. Example:

var globalCache *cache.Cache
func main() {
globalCache = cache.New(5*time.Minute, 10*time.Minute)
http.HandleFunc("/", HelloHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
// use globalCache
}

Update: atomic is not needed since cache.Cache makes locks internally. Thank you for correcting me.

huangapple
  • 本文由 发表于 2022年1月18日 01:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/70745556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定