feat(缓存): 添加批量清除用户相关缓存功能并优化缓存键命名
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m11s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m11s
添加 BatchClearRelatedCache 方法用于批量清除用户相关缓存 优化设备相关缓存键的命名格式以提高一致性 简化设备登录逻辑中孤儿认证方法的处理流程
This commit is contained in:
parent
1bcfa321b7
commit
9d52826555
@ -86,31 +86,9 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
||||
}
|
||||
|
||||
if authMethod != nil {
|
||||
// 认证方法存在但设备记录不存在,可能是数据不一致,先检查用户是否存在
|
||||
// 认证方法存在但设备记录不存在,可能是数据不一致,获取用户信息并重新创建设备记录
|
||||
userInfo, err = l.svcCtx.UserModel.FindOne(l.ctx, authMethod.UserId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 用户不存在,说明是孤立的认证方法记录,需要清理
|
||||
l.Errorw("found orphaned auth method record, cleaning up",
|
||||
logger.Field("auth_method_id", authMethod.Id),
|
||||
logger.Field("user_id", authMethod.UserId),
|
||||
logger.Field("identifier", req.Identifier),
|
||||
)
|
||||
|
||||
// 删除孤立的认证方法记录
|
||||
if deleteErr := l.svcCtx.UserModel.DeleteUserAuthMethods(l.ctx, authMethod.UserId, authMethod.AuthType); deleteErr != nil {
|
||||
l.Errorw("failed to delete orphaned auth method",
|
||||
logger.Field("auth_method_id", authMethod.Id),
|
||||
logger.Field("error", deleteErr.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
// 创建新用户和设备
|
||||
userInfo, err = l.registerUserAndDevice(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
l.Errorw("query user by auth method failed",
|
||||
logger.Field("user_id", authMethod.UserId),
|
||||
logger.Field("identifier", req.Identifier),
|
||||
@ -118,8 +96,8 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
||||
)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user failed: %v", err.Error())
|
||||
}
|
||||
} else {
|
||||
// 用户存在,重新创建缺失的设备记录
|
||||
|
||||
// 重新创建缺失的设备记录
|
||||
deviceInfo := &user.Device{
|
||||
Ip: req.IP,
|
||||
UserId: userInfo.Id,
|
||||
@ -142,7 +120,6 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
||||
logger.Field("identifier", req.Identifier),
|
||||
logger.Field("device_id", deviceInfo.Id),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// 设备和认证方法都不存在,创建新用户和设备
|
||||
userInfo, err = l.registerUserAndDevice(req)
|
||||
|
||||
@ -274,10 +274,13 @@ func (l *BindEmailWithVerificationLogic) transferDeviceToEmailUser(deviceUserId,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 5. 清除邮箱用户缓存(确保获取最新数据)
|
||||
// 5. 强制清除邮箱用户的所有相关缓存(确保获取最新数据)// 清除邮箱用户缓存
|
||||
emailUser, _ := l.svcCtx.UserModel.FindOne(l.ctx, emailUserId)
|
||||
if emailUser != nil {
|
||||
l.svcCtx.UserModel.ClearUserCache(l.ctx, emailUser)
|
||||
// 清除用户的批量相关缓存(包括设备、认证方法等)
|
||||
if err := l.svcCtx.UserModel.BatchClearRelatedCache(l.ctx, emailUser); err != nil {
|
||||
l.Errorw("清理邮箱用户相关缓存失败", logger.Field("error", err.Error()), logger.Field("user_id", emailUser.Id))
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 清除设备相关缓存
|
||||
@ -388,11 +391,13 @@ func (l *BindEmailWithVerificationLogic) createEmailUser(email string) (int64, e
|
||||
func (l *BindEmailWithVerificationLogic) clearDeviceRelatedCache(deviceIdentifier string, oldUserId, newUserId int64) {
|
||||
// 清除设备相关的缓存键
|
||||
deviceCacheKeys := []string{
|
||||
fmt.Sprintf("device:%s", deviceIdentifier),
|
||||
fmt.Sprintf("user_device:%d", oldUserId),
|
||||
fmt.Sprintf("user_device:%d", newUserId),
|
||||
fmt.Sprintf("user_auth:%d", oldUserId),
|
||||
fmt.Sprintf("user_auth:%d", newUserId),
|
||||
fmt.Sprintf("cache:device:identifier:%s", deviceIdentifier),
|
||||
fmt.Sprintf("cache:user:devices:%d", oldUserId),
|
||||
fmt.Sprintf("cache:user:devices:%d", newUserId),
|
||||
fmt.Sprintf("cache:user:auth_methods:%d", oldUserId),
|
||||
fmt.Sprintf("cache:user:auth_methods:%d", newUserId),
|
||||
fmt.Sprintf("cache:user:%d", oldUserId),
|
||||
fmt.Sprintf("cache:user:%d", newUserId),
|
||||
}
|
||||
|
||||
for _, key := range deviceCacheKeys {
|
||||
|
||||
@ -105,6 +105,7 @@ type customUserLogicModel interface {
|
||||
|
||||
ClearSubscribeCache(ctx context.Context, data ...*Subscribe) error
|
||||
ClearUserCache(ctx context.Context, data ...*User) error
|
||||
BatchClearRelatedCache(ctx context.Context, user *User) error
|
||||
|
||||
QueryDailyUserStatisticsList(ctx context.Context, date time.Time) ([]UserStatisticsWithDate, error)
|
||||
QueryMonthlyUserStatisticsList(ctx context.Context, date time.Time) ([]UserStatisticsWithDate, error)
|
||||
@ -243,6 +244,10 @@ func (m *customUserModel) FindOneByReferCode(ctx context.Context, referCode stri
|
||||
return &data, err
|
||||
}
|
||||
|
||||
func (m *customUserModel) BatchClearRelatedCache(ctx context.Context, user *User) error {
|
||||
return m.defaultUserModel.BatchClearRelatedCache(ctx, user)
|
||||
}
|
||||
|
||||
func (m *customUserModel) FindOneSubscribeDetailsById(ctx context.Context, id int64) (*SubscribeDetails, error) {
|
||||
var data SubscribeDetails
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user