fix: Improve texture upload handling and caching logic

- Simplified caching logic by removing unnecessary nil check before setting cache.
- Enhanced error handling in texture upload process to return the original texture object if fetching the uploader information fails or returns nil.
This commit is contained in:
lafay
2026-01-10 03:23:26 +08:00
parent 2c9c6ecfc0
commit 22142db782

View File

@@ -87,9 +87,7 @@ func (s *textureService) GetByID(ctx context.Context, id int64) (*model.Texture,
} }
// 存入缓存(异步) // 存入缓存(异步)
if texture2 != nil {
s.cache.SetAsync(context.Background(), cacheKey, texture2, s.cache.Policy.TextureTTL) s.cache.SetAsync(context.Background(), cacheKey, texture2, s.cache.Policy.TextureTTL)
}
return texture2, nil return texture2, nil
} }
@@ -382,7 +380,16 @@ func (s *textureService) UploadTexture(ctx context.Context, uploaderID int64, na
s.cacheInv.BatchInvalidate(ctx, fmt.Sprintf("texture:user:%d:*", uploaderID)) s.cacheInv.BatchInvalidate(ctx, fmt.Sprintf("texture:user:%d:*", uploaderID))
// 重新查询以预加载 Uploader 关联 // 重新查询以预加载 Uploader 关联
return s.textureRepo.FindByID(ctx, texture.ID) textureWithUploader, err := s.textureRepo.FindByID(ctx, texture.ID)
if err != nil {
// 如果查询失败,返回原始创建的 texture 对象(虽然可能没有 Uploader 信息)
return texture, nil
}
if textureWithUploader == nil {
// 如果查询返回 nil极端情况如数据库复制延迟返回原始创建的 texture 对象
return texture, nil
}
return textureWithUploader, nil
} }
// parseTextureTypeInternal 解析材质类型 // parseTextureTypeInternal 解析材质类型