feat(user): enhance user service with caching and refactor menu filtering logic#655
Conversation
📝 Walkthrough""" Walkthrough本次变更主要对用户菜单过滤和用户信息获取逻辑进行了重构和优化。 Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant CurrentUser
participant MenuService
User ->> CurrentUser: filterCurrentUser()
CurrentUser ->> MenuService: getMenusByPermissions(permissions, status)
MenuService -->> CurrentUser: 返回菜单列表
CurrentUser ->> CurrentUser: 构建菜单树
CurrentUser -->> User: 返回菜单树
sequenceDiagram
actor Caller
participant UserService
participant Cache
participant UserRepository
Caller ->> UserService: getInfo(id)
UserService ->> Cache: has(id)
alt 缓存命中
Cache -->> UserService: 返回用户数据
UserService -->> Caller: 返回用户数据
else 缓存未命中
UserService ->> UserRepository: find(id)
UserRepository -->> UserService: 返回用户数据
UserService ->> Cache: set(id, 用户数据, 60秒)
UserService -->> Caller: 返回用户数据
end
Possibly related PRs
Suggested labels
Poem
""" 📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (3)
🧰 Additional context used🧬 Code Graph Analysis (1)app/Http/CurrentUser.php (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
🔇 Additional comments (10)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
app/Service/Permission/UserService.php (1)
44-53: 重置密码后需要清除缓存当用户密码被重置时,应该清除相关的缓存以避免返回过期数据。
public function resetPassword(?int $id): bool { if ($id === null) { return false; } $entity = $this->repository->findById($id); $entity->resetPassword(); $entity->save(); + // 清除用户缓存 + $this->cache->delete('user:info:' . $id); return true; }
🧹 Nitpick comments (1)
app/Http/CurrentUser.php (1)
53-80: 菜单过滤逻辑重构得很好!新的实现移除了递归,使用引用构建树形结构,性能更优。
建议考虑以下小优化:
- 添加用户空值检查:
public function filterCurrentUser(): array { + $user = $this->user(); + if ($user === null) { + return []; + } - $permissions = $this->user() + $permissions = $user ->getPermissions() ->pluck('name') ->unique() ->toArray();
- 可以考虑将树形结构构建逻辑提取为一个通用方法,便于其他地方复用。
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/Http/CurrentUser.php(3 hunks)app/Service/Permission/UserService.php(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
app/Service/Permission/UserService.php (3)
app/Model/Permission/User.php (1)
User(45-139)app/Repository/IRepository.php (1)
findById(121-124)app/Service/IService.php (1)
findById(75-78)
🔇 Additional comments (2)
app/Service/Permission/UserService.php (1)
28-32: 构造函数变更看起来不错!依赖注入的实现正确,使用了只读属性。
app/Http/CurrentUser.php (1)
27-31: 构造函数更新正确!MenuService 的依赖注入实现得当。
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #655 +/- ##
=============================
=============================
☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary by CodeRabbit
新功能
重构
测试