From b6ddd637db897dd92968d2b1711831994c135bc4 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 27 Jan 2022 11:02:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/UserService.cs | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs index 9e1c3415..662960ba 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs @@ -2,6 +2,7 @@ // Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. // Website: https://admin.blazor.zone +using BootstrapAdmin.Caching; using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.Web.Core; using Longbow.Security.Cryptography; @@ -51,20 +52,20 @@ class UserService : IUser /// public User? GetUserByUserName(string? userName) => string.IsNullOrEmpty(userName) ? null : Database.FirstOrDefault("Where UserName = @0", userName); - /// - /// - /// - /// - /// - /// - public List GetApps(string userName) => Database.Fetch($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators"); + private const string UserServiceGetAppsByUserNameCacheKey = "UserService-GetAppsByUserName"; + + public List GetApps(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppsByUserNameCacheKey}-{userName}", entry => Database.Fetch($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators")); + + private const string UserServiceGetAppIdByUserNameCacheKey = "UserService-GetAppIdByUserName"; /// /// 通过用户名获得指定的前台 AppId /// /// /// - public string? GetAppIdByUserName(string userName) => Database.FirstOrDefault("Where UserName = @0", userName)?.App; + public string? GetAppIdByUserName(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppIdByUserNameCacheKey}-{userName}", entry => Database.FirstOrDefault("Where UserName = @0", userName)?.App); + + private const string UserServiceGetRolesByUserNameCacheKey = "UserService-GetRolesByUserName"; /// /// @@ -72,15 +73,15 @@ class UserService : IUser /// /// /// - public List GetRoles(string userName) => Database.Fetch($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @0 union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = @0", userName); + public List GetRoles(string userName) => CacheManager.GetOrAdd($"{UserServiceGetRolesByUserNameCacheKey}-{userName}", entry => Database.Fetch($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @0 union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = @0", userName)); + + private const string UserServiceGetUsersByGroupIdCacheKey = "UserService-GetUsersByGroupId"; /// /// /// - /// - /// - /// - public List GetUsersByGroupId(string? id) => Database.Fetch("select UserID from UserGroup where GroupID = @0", id); + /// + public List GetUsersByGroupId(string? groupId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByGroupIdCacheKey}-{groupId}", entry => Database.Fetch("select UserID from UserGroup where GroupID = @0", groupId)); /// /// @@ -105,15 +106,21 @@ class UserService : IUser Database.AbortTransaction(); throw; } + if (ret) + { + CacheManager.Clear(); + } return ret; } + private const string UserServiceGetUsersByRoleIdCacheKey = "UserService-GetUsersByRoleId"; + /// /// /// /// /// - public List GetUsersByRoleId(string? roleId) => Database.Fetch("select UserID from UserRole where RoleID = @0", roleId); + public List GetUsersByRoleId(string? roleId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByRoleIdCacheKey}-{roleId}", entry => Database.Fetch("select UserID from UserRole where RoleID = @0", roleId)); /// /// @@ -137,6 +144,10 @@ class UserService : IUser Database.AbortTransaction(); throw; } + if (ret) + { + CacheManager.Clear(); + } return ret; } @@ -225,6 +236,10 @@ class UserService : IUser Database.AbortTransaction(); throw; } + if (ret) + { + CacheManager.Clear(); + } return ret; } @@ -272,6 +287,10 @@ class UserService : IUser Database.Update(user); ret = true; } + if (ret) + { + CacheManager.Clear(); + } return ret; }