feat: NavigationService 增加缓存

This commit is contained in:
Argo-Tianyi 2022-01-27 10:45:34 +08:00
parent 195802e87f
commit 3c99d1e76a
1 changed files with 14 additions and 11 deletions

View File

@ -14,6 +14,10 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
/// </summary> /// </summary>
class NavigationService : INavigation class NavigationService : INavigation
{ {
private const string NavigationServiceGetAllCacheKey = "NavigationService-GetAll";
private const string NavigationServiceGetMenusByRoleIdCacheKey = "NavigationService-GetMenusByRoleId";
private IDatabase Database { get; } private IDatabase Database { get; }
/// <summary> /// <summary>
@ -27,21 +31,19 @@ class NavigationService : INavigation
/// </summary> /// </summary>
/// <param name="userName">当前用户名</param> /// <param name="userName">当前用户名</param>
/// <returns>未层次化的菜单集合</returns> /// <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 =>
{ {
// 缓存所有菜单数据移除 SQL 语句降低复杂度
var order = Database.Provider.EscapeSqlIdentifier("Order"); 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" }); 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>
/// ///
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <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> /// <summary>
/// ///
@ -58,16 +60,17 @@ class NavigationService : INavigation
Database.Execute("delete from NavigationRole where RoleID = @0", roleId); Database.Execute("delete from NavigationRole where RoleID = @0", roleId);
Database.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId })); Database.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId }));
Database.CompleteTransaction(); Database.CompleteTransaction();
// 通知缓存更新
ret = true; ret = true;
CacheManager.Clear($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-*");
} }
catch (Exception) catch (Exception)
{ {
Database.AbortTransaction(); Database.AbortTransaction();
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
} }