From c538324e4a05ac6246ba4008170ce1fdf3ebce77 Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Fri, 26 Jan 2024 21:29:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(redis=5Fconfig):=E4=BF=AE=E5=A4=8DRedis?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + config/redis.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index b959317..d7e99ad 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,7 @@ REDIS_HOST=localhost REDIS_PORT=6379 REDIS_PASSWORD= +REDIS_DB=0 CAPTCHA_BREAKER_NEW_URL=http:// LIBRARY_URL=http:// diff --git a/config/redis.go b/config/redis.go index 5a6251c..e73da65 100644 --- a/config/redis.go +++ b/config/redis.go @@ -1,9 +1,10 @@ package config import ( - "github.com/go-redis/redis" "os" "strconv" + + "github.com/go-redis/redis" ) var Redis = redis.Client{} @@ -11,11 +12,13 @@ var Redis = redis.Client{} func RedisInit() *redis.Client { REDIS_HOST := "localhost" REDIS_PORT := "6379" + REDIS_PASSWORD := "" REDIS_DB := 0 if os.Getenv("REDIS_HOST") != "" { REDIS_HOST = os.Getenv("REDIS_HOST") } + if os.Getenv("REDIS_PORT") != "" { REDIS_PORT = os.Getenv("REDIS_PORT") } @@ -24,9 +27,14 @@ func RedisInit() *redis.Client { REDIS_DB, _ = strconv.Atoi(os.Getenv("REDIS_DB")) } + if os.Getenv("REDIS_PASSWORD") != "" { + REDIS_PASSWORD = os.Getenv("REDIS_PASSWORD") + } + RedisClient := redis.NewClient(&redis.Options{ - Addr: REDIS_HOST + ":" + REDIS_PORT, - DB: REDIS_DB, + Addr: REDIS_HOST + ":" + REDIS_PORT, + Password: REDIS_PASSWORD, + DB: REDIS_DB, }) _, err := RedisClient.Ping().Result() From c3823de30d72a9d24458468708a44f70f4144f52 Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Fri, 26 Jan 2024 21:29:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(redis=5Fservice):=E4=BF=AE=E5=A4=8DRedi?= =?UTF-8?q?s=E7=BC=93=E5=AD=98=E6=97=B6=E9=97=B4=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/serviceUniFunction.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/service/serviceUniFunction.go b/app/service/serviceUniFunction.go index cdcc144..ec62376 100644 --- a/app/service/serviceUniFunction.go +++ b/app/service/serviceUniFunction.go @@ -37,13 +37,14 @@ func GetUser(prefix string, username string, password string) (*model.User, erro func SetUser(prefix string, username string, password string, sessionCookie *http.Cookie, routeCookie *http.Cookie) (*model.User, error) { user := model.User{Username: username, Password: password, Session: *sessionCookie, Route: *routeCookie} userJson, _ := json.Marshal(user) - config.Redis.Set(getRediskey(prefix, username, password), string(userJson), sessionCookie.Expires.Sub(time.Now().Add(time.Minute*5))) + config.Redis.Set(getRediskey(prefix, username, password), string(userJson), 5*time.Minute) return &user, nil } func ForgetUser(prefix string, username string, password string) { config.Redis.Del(getRediskey(prefix, username, password)) } + func ForgetAllUser(prefix string) { res, _ := config.Redis.Keys(prefix + "*").Result() for _, v := range res {