Skip to content

Commit

Permalink
Merge pull request #19 from XiMo-210/main
Browse files Browse the repository at this point in the history
fix(redis):修复Redis配置以及缓存时间设置问题
  • Loading branch information
Patrick-Star-CN authored Jan 27, 2024
2 parents b6fc622 + c3823de commit 16503ad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0

CAPTCHA_BREAKER_NEW_URL=http://
LIBRARY_URL=http://
Expand Down
3 changes: 2 additions & 1 deletion app/service/serviceUniFunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions config/redis.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package config

import (
"github.com/go-redis/redis"
"os"
"strconv"

"github.com/go-redis/redis"
)

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")
}
Expand All @@ -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()
Expand Down

0 comments on commit 16503ad

Please sign in to comment.