Просмотр исходного кода

更新用户时刷新缓存数据

CaIon 2 лет назад
Родитель
Сommit
7e4fe14871
1 измененных файлов с 6 добавлено и 1 удалено
  1. 6 1
      model/user.go

+ 6 - 1
model/user.go

@@ -6,6 +6,7 @@ import (
 	"gorm.io/gorm"
 	"one-api/common"
 	"strings"
+	"time"
 )
 
 // User if you add sensitive fields, don't forget to clean them in setupLogin function.
@@ -202,9 +203,13 @@ func (user *User) Update(updatePassword bool) error {
 		}
 	}
 	newUser := *user
-
 	DB.First(&user, user.Id)
 	err = DB.Model(user).Updates(newUser).Error
+	if err == nil {
+		if common.RedisEnabled {
+			_ = common.RedisSet(fmt.Sprintf("user_group:%d", user.Id), user.Group, time.Duration(UserId2GroupCacheSeconds)*time.Second)
+		}
+	}
 	return err
 }