From bcd3d42718daede20e852589e73abf49f176052c Mon Sep 17 00:00:00 2001 From: sia Date: Sun, 1 Dec 2024 21:49:50 +0800 Subject: [PATCH] add setnx avoid get more than one --- cache/redis.go | 5 +++++ router/default.go | 1 - service/log.go | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cache/redis.go b/cache/redis.go index c79d291..850c23d 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "time" "github.com/redis/go-redis/v9" ) @@ -74,6 +75,10 @@ func (c *Redis) Upgrade(key string) { if c.Nil() { return } + res, _ := c.db.SetNX(context.Background(), key, "set", time.Minute).Result() + if !res { + return + } resp, err := http.Get("https://" + key) if err != nil { log.Println("get err ", err) diff --git a/router/default.go b/router/default.go index a726efd..2f4da30 100644 --- a/router/default.go +++ b/router/default.go @@ -55,7 +55,6 @@ func NewRouter(c *cache.Redis) *mux.Router { path := fmt.Sprintf("github.com/%s/%s/releases/download/%s/%s", userBaisc[0], userBaisc[1], vars["version"], vars["file"]) c.Incr(path) if c.Exists(path) { - fmt.Println("this way ") r.URL.Path = path http.FileServer(http.Dir("./cache")).ServeHTTP(w, r) v, ok := r.Header[http.CanonicalHeaderKey("X-Real-IP")] diff --git a/service/log.go b/service/log.go index 42f9889..1b4b295 100644 --- a/service/log.go +++ b/service/log.go @@ -128,7 +128,7 @@ func DefaultLogFormatter(param LogFormatterParams) { resetColor = param.ResetColor() } - fmt.Printf("[Proxy] %s | %d %s %s| %13v | %15s |%s %-7s %s %#v %s\n", + fmt.Printf("[Proxy]|%s %d %s %s| %13v | %15s |%s %-7s %s %#v %s\n", statusColor, param.StatusCode, resetColor, time.Now().Format("01-02 15:04:05"), param.ContentLength, param.ClientIP,