feat: NavigationService 增加缓存
This commit is contained in:
parent
195802e87f
commit
3c99d1e76a
|
@ -14,6 +14,10 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
|
|||
/// </summary>
|
||||
class NavigationService : INavigation
|
||||
{
|
||||
private const string NavigationServiceGetAllCacheKey = "NavigationService-GetAll";
|
||||
|
||||
private const string NavigationServiceGetMenusByRoleIdCacheKey = "NavigationService-GetMenusByRoleId";
|
||||
|
||||
private IDatabase Database { get; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -27,21 +31,19 @@ class NavigationService : INavigation
|
|||
/// </summary>
|
||||
/// <param name="userName">当前用户名</param>
|
||||
/// <returns>未层次化的菜单集合</returns>
|
||||
public List<Navigation> GetAllMenus(string userName)
|
||||
public List<Navigation> GetAllMenus(string userName) => CacheManager.GetOrAdd($"{NavigationServiceGetAllCacheKey}-{userName}", entry =>
|
||||
{
|
||||
return CacheManager.GetOrAdd($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-{userName}", entry =>
|
||||
{
|
||||
var order = Database.Provider.EscapeSqlIdentifier("Order");
|
||||
return Database.Fetch<Models.Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.{order}", new { UserName = userName, RoleName = "Administrators" });
|
||||
});
|
||||
}
|
||||
// 缓存所有菜单数据移除 SQL 语句降低复杂度
|
||||
var order = Database.Provider.EscapeSqlIdentifier("Order");
|
||||
return Database.Fetch<Models.Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.{order}", new { UserName = userName, RoleName = "Administrators" });
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetMenusByRoleId(string? roleId) => Database.Fetch<string>("select NavigationID from NavigationRole where RoleID = @0", roleId);
|
||||
public List<string> GetMenusByRoleId(string? roleId) => CacheManager.GetOrAdd($"{NavigationServiceGetMenusByRoleIdCacheKey}-{roleId}", entry => Database.Fetch<string>("select NavigationID from NavigationRole where RoleID = @0", roleId));
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -58,16 +60,17 @@ class NavigationService : INavigation
|
|||
Database.Execute("delete from NavigationRole where RoleID = @0", roleId);
|
||||
Database.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId }));
|
||||
Database.CompleteTransaction();
|
||||
|
||||
// 通知缓存更新
|
||||
ret = true;
|
||||
CacheManager.Clear($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-*");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Database.AbortTransaction();
|
||||
throw;
|
||||
}
|
||||
if (ret)
|
||||
{
|
||||
CacheManager.Clear();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue