Skip to content

Commit

Permalink
add setnx avoid get more than one
Browse files Browse the repository at this point in the history
  • Loading branch information
Sianao committed Dec 1, 2024
1 parent ee9c15b commit bcd3d42
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"time"

"github.com/redis/go-redis/v9"
)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion router/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion service/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bcd3d42

Please sign in to comment.