重构代码:重写梳理BASQLHelper静态类方法缓存逻辑移动到Helper类中

This commit is contained in:
Argo-MacBookPro 2018-10-28 20:50:01 +08:00
parent 2a39f1db4c
commit 53b2a2e666
5 changed files with 56 additions and 70 deletions

View File

@ -58,7 +58,7 @@ namespace Bootstrap.Admin.Controllers.Api
ret = MenuHelper.RetrieveMenusByRoleId(id).ToList(); ret = MenuHelper.RetrieveMenusByRoleId(id).ToList();
break; break;
case "user": case "user":
ret = MenuHelper.RetrieveAllMenus(User.Identity.Name).ToList(); ret = MenuHelper.RetrieveMenus(User.Identity.Name).ToList();
break; break;
default: default:
break; break;

View File

@ -93,7 +93,7 @@
}, },
{ {
"Enabled": true, "Enabled": true,
"Key": "BootstrapMenu-RetrieveMenusByUserName", "Key": "BootstrapMenu-RetrieveMenus",
"Interval": 600, "Interval": 600,
"SlidingExpiration": true, "SlidingExpiration": true,
"Desc": "用户所有菜单数据缓存" "Desc": "用户所有菜单数据缓存"

View File

@ -32,9 +32,9 @@ namespace Bootstrap.DataAccess
cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByRoleIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByRoleIdDataKey, id));
}); });
cacheKeys.Add(RoleHelper.RetrieveRolesDataKey + "*"); cacheKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*");
corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
} }
if (userIds != null) if (userIds != null)
{ {
@ -42,8 +42,8 @@ namespace Bootstrap.DataAccess
{ {
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIdDataKey, id));
cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}); });
cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*"); cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*");
cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*"); cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
@ -57,8 +57,8 @@ namespace Bootstrap.DataAccess
cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIdDataKey, id));
}); });
cacheKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*"); cacheKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*");
} }
if (menuIds != null) if (menuIds != null)
@ -68,8 +68,8 @@ namespace Bootstrap.DataAccess
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id));
}); });
cacheKeys.Add(MenuHelper.RetrieveMenusByRoleIdDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusByRoleIdDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
} }
if (dictIds != null) if (dictIds != null)
{ {

View File

@ -1,7 +1,9 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using Longbow.Cache; using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -17,10 +19,6 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public const string RetrieveMenusDataKey = "BootstrapMenu-RetrieveMenusByUserName";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary> /// <summary>
/// ///
@ -35,20 +33,17 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public static bool DeleteMenu(IEnumerable<int> value) => DbAdapterManager.Create<Menu>().DeleteMenu(value); public static bool DeleteMenu(IEnumerable<int> value) => DbAdapterManager.Create<Menu>().DeleteMenu(value);
/// <summary> /// <summary>
/// /// 通过用户名获得所有菜单
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName) => DbAdapterManager.Create<Menu>().RetrieveMenusByUserName(userName); public static IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId) public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
{
return CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -60,21 +55,50 @@ namespace Bootstrap.DataAccess
/// ///
/// </summary> /// </summary>
/// <param name="name"></param> /// <param name="name"></param>
/// <param name="url"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string url) => DbAdapterManager.Create<Menu>().RetrieveAppMenus(appId, userName, url);
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="activeUrl"></param> /// <param name="activeUrl"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveSystemMenus(string userName, string activeUrl = null) => DbAdapterManager.Create<Menu>().RetrieveSystemMenus(userName, activeUrl); public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl)
{
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0);
if (appId != "0") menus = menus.Where(m => m.ApplicationCode == appId);
BASQLHelper.ActiveMenu(null, menus, activeUrl);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary> /// <summary>
/// /// 通过当前用户名获得后台菜单,层次化后集合
/// </summary> /// </summary>
/// <param name="name"></param> /// <param name="db"></param>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="connName">连接字符串名称默认为ba</param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveAllMenus(string name) => DbAdapterManager.Create<Menu>().RetrieveAllMenus(name); public static IEnumerable<BootstrapMenu> RetrieveSystemMenus(string userName, string activeUrl = null)
{
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0);
BASQLHelper.ActiveMenu(null, menus, activeUrl);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得所有菜单,层次化后集合
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenus(string userName)
{
var menus = RetrieveAllMenus(userName);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过用户获得所有菜单
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
private static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbAdapterManager.Create<Menu>().RetrieveAllMenus(userName), RetrieveMenusAll);
} }
} }

View File

@ -136,45 +136,7 @@ namespace Bootstrap.DataAccess
/// 通过当前用户名获得所有菜单,层次化后集合 /// 通过当前用户名获得所有菜单,层次化后集合
/// </summary> /// </summary>
/// <param name="userName">当前登陆的用户名</param> /// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName, string activeUrl = null) public virtual IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => BASQLHelper.RetrieveAllMenus(userName);
{
var menus = RetrieveMenusByUserName(userName, activeUrl);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.Category).ThenBy(m => m.ApplicationCode).ThenBy(m => m.Order);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得前台菜单,层次化后集合
/// </summary>
/// <param name="appId"></param>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl = null) => BASQLHelper.RetrieveAppMenus(appId, userName, activeUrl);
/// <summary>
/// 通过当前用户名获得后台菜单,层次化后集合
/// </summary>
/// <param name="db"></param>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="connName">连接字符串名称默认为ba</param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveSystemMenus(string userName, string activeUrl = null)
{
var menus = RetrieveMenusByUserName(userName, activeUrl).Where(m => m.Category == "0" && m.IsResource == 0);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得所有菜单
/// </summary>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="appId"></param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName, string activeUrl = null, string appId = "0") => BASQLHelper.RetrieveMenusByUserName(userName, activeUrl, appId);
} }
} }