英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论