重构代码:移动Role缓存处理到RoleHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-31 11:15:43 +08:00
parent dfd4913deb
commit a0803b5179
5 changed files with 150 additions and 23 deletions

View File

@ -92,6 +92,17 @@ namespace Bootstrap.DataAccess.MongoDB
} }
} }
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.Role> Roles
{
get
{
return DBAccess.GetCollection<DataAccess.Role>("Roles");
}
}
private static void InitDb() private static void InitDb()
{ {
var connectString = DbAdapterManager.GetConnectionString("ba"); var connectString = DbAdapterManager.GetConnectionString("ba");

View File

@ -1,5 +1,6 @@
using MongoDB.Driver; using MongoDB.Driver;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB namespace Bootstrap.DataAccess.MongoDB
{ {
@ -8,6 +9,52 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary> /// </summary>
public class Role : DataAccess.Role public class Role : DataAccess.Role
{ {
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRoles()
{
return MongoDbAccessManager.Roles.Find(FilterDefinition<DataAccess.Role>.Empty).ToList();
}
/// <summary>
///
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveRole(DataAccess.Role p)
{
if (p.Id == "0")
{
p.Id = null;
MongoDbAccessManager.Roles.InsertOne(p);
return true;
}
else
{
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<DataAccess.Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteRole(IEnumerable<string> value)
{
var list = new List<WriteModel<DataAccess.Role>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<DataAccess.Role>(Builders<DataAccess.Role>.Filter.Eq(g => g.Id, id)));
}
MongoDbAccessManager.Roles.BulkWrite(list);
return true;
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -15,23 +62,49 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUserName(string userName) public override IEnumerable<string> RetrieveRolesByUserName(string userName)
{ {
return new List<string>() { "Administrators" }; var roles = new List<string>();
var user = UserHelper.RetrieveUsers().Cast<User>().FirstOrDefault(u => u.UserName == userName);
var role = RoleHelper.RetrieveRoles();
roles.AddRange(user.Roles.Select(r => role.FirstOrDefault(rl => rl.Id == r).RoleName));
if (roles.Count == 0) roles.Add("Default");
return roles;
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="url"></param> /// <param name="url"></param>
/// <returns></returns> /// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUrl(string url) => new List<string>() { "Administrators" }; public override IEnumerable<string> RetrieveRolesByUrl(string url)
{
// TODO: 需要菜单完成后处理此函数
return new List<string>() { "Administrators" };
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="userId"></param>
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRoles() public override IEnumerable<DataAccess.Role> RetrieveRolesByUserId(string userId)
{ {
var roles = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Role>("Roles"); var roles = RoleHelper.RetrieveRoles();
return roles.Find(FilterDefinition<DataAccess.Role>.Empty).ToList(); var user = UserHelper.RetrieveUsers().Cast<User>().FirstOrDefault(u => u.Id == userId);
roles.ToList().ForEach(r => r.Checked = user.Roles.Any(id => id == r.Id) ? "checked" : "");
return roles;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public override bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds)
{
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
return true;
} }
} }
} }

View File

@ -39,7 +39,6 @@ namespace Bootstrap.DataAccess.SQLite
}); });
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -50,6 +49,7 @@ namespace Bootstrap.DataAccess.SQLite
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
@ -76,7 +76,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction(); transaction.CommitTransaction();
CacheCleanUtility.ClearCache(roleIds: value);
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -88,6 +87,7 @@ namespace Bootstrap.DataAccess.SQLite
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -115,7 +115,6 @@ namespace Bootstrap.DataAccess.SQLite
}); });
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -126,6 +125,7 @@ namespace Bootstrap.DataAccess.SQLite
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary> /// </summary>
@ -154,7 +154,6 @@ namespace Bootstrap.DataAccess.SQLite
}); });
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -1,7 +1,6 @@
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
{ {
@ -16,67 +15,103 @@ namespace Bootstrap.DataAccess
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId"; public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName"; public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl"; public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
/// <summary> /// <summary>
/// 查询所有角色 /// 查询所有角色
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRoles() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create<Role>().RetrieveRoles()); public static IEnumerable<Role> RetrieveRoles() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create<Role>().RetrieveRoles());
/// <summary> /// <summary>
/// 保存用户角色关系 /// 保存用户角色关系
/// </summary> /// </summary>
/// <param name="userId"></param> /// <param name="userId"></param>
/// <param name="roleIds"></param> /// <param name="roleIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByUserId(userId, roleIds); public static bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SaveRolesByUserId(userId, roleIds);
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
return ret;
}
/// <summary> /// <summary>
/// 查询某个用户所拥有的角色 /// 查询某个用户所拥有的角色
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserId(string userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbAdapterManager.Create<Role>().RetrieveRolesByUserId(userId), RetrieveRolesByUserIdDataKey); public static IEnumerable<Role> RetrieveRolesByUserId(string userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbAdapterManager.Create<Role>().RetrieveRolesByUserId(userId), RetrieveRolesByUserIdDataKey);
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
public static bool DeleteRole(IEnumerable<string> value) => DbAdapterManager.Create<Role>().DeleteRole(value); public static bool DeleteRole(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<Role>().DeleteRole(value);
if (ret) CacheCleanUtility.ClearCache(roleIds: value);
return ret;
}
/// <summary> /// <summary>
/// 保存新建/更新的角色信息 /// 保存新建/更新的角色信息
/// </summary> /// </summary>
/// <param name="p"></param> /// <param name="p"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveRole(Role p) => DbAdapterManager.Create<Role>().SaveRole(p); public static bool SaveRole(Role p)
{
var ret = DbAdapterManager.Create<Role>().SaveRole(p);
if (ret) CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string> { p.Id });
return ret;
}
/// <summary> /// <summary>
/// 查询某个菜单所拥有的角色 /// 查询某个菜单所拥有的角色
/// </summary> /// </summary>
/// <param name="menuId"></param> /// <param name="menuId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByMenuId(string menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbAdapterManager.Create<Role>().RetrieveRolesByMenuId(menuId), RetrieveRolesByMenuIdDataKey); public static IEnumerable<Role> RetrieveRolesByMenuId(string menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbAdapterManager.Create<Role>().RetrieveRolesByMenuId(menuId), RetrieveRolesByMenuIdDataKey);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="menuId"></param>
/// <param name="roleIds"></param> /// <param name="roleIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SavaRolesByMenuId(string id, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SavaRolesByMenuId(id, roleIds); public static bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SavaRolesByMenuId(menuId, roleIds);
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
return ret;
}
/// <summary> /// <summary>
/// 根据GroupId查询和该Group有关的所有Roles /// 根据GroupId查询和该Group有关的所有Roles
/// </summary> /// </summary>
/// <param name="groupId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbAdapterManager.Create<Role>().RetrieveRolesByGroupId(groupId), RetrieveRolesByGroupIdDataKey); public static IEnumerable<Role> RetrieveRolesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbAdapterManager.Create<Role>().RetrieveRolesByGroupId(groupId), RetrieveRolesByGroupIdDataKey);
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="groupId"></param>
/// <param name="roleIds"></param> /// <param name="roleIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveRolesByGroupId(string id, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByGroupId(id, roleIds); public static bool SaveRolesByGroupId(string groupId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SaveRolesByGroupId(groupId, roleIds);
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
return ret;
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbAdapterManager.Create<Role>().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey); public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbAdapterManager.Create<Role>().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -18,18 +18,22 @@ namespace Bootstrap.DataAccess
/// 获得/设置 角色主键ID /// 获得/设置 角色主键ID
/// </summary> /// </summary>
public string Id { get; set; } public string Id { get; set; }
/// <summary> /// <summary>
/// 获得/设置 角色名称 /// 获得/设置 角色名称
/// </summary> /// </summary>
public string RoleName { get; set; } public string RoleName { get; set; }
/// <summary> /// <summary>
/// 获得/设置 角色描述 /// 获得/设置 角色描述
/// </summary> /// </summary>
public string Description { get; set; } public string Description { get; set; }
/// <summary> /// <summary>
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联 /// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
/// </summary> /// </summary>
public string Checked { get; set; } public string Checked { get; set; }
/// <summary> /// <summary>
/// 查询所有角色 /// 查询所有角色
/// </summary> /// </summary>
@ -53,6 +57,7 @@ namespace Bootstrap.DataAccess
} }
return roles; return roles;
} }
/// <summary> /// <summary>
/// 保存用户角色关系 /// 保存用户角色关系
/// </summary> /// </summary>
@ -89,7 +94,6 @@ namespace Bootstrap.DataAccess
} }
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -100,6 +104,7 @@ namespace Bootstrap.DataAccess
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 查询某个用户所拥有的角色 /// 查询某个用户所拥有的角色
/// </summary> /// </summary>
@ -125,6 +130,7 @@ namespace Bootstrap.DataAccess
} }
return roles; return roles;
} }
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
@ -138,9 +144,9 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
} }
CacheCleanUtility.ClearCache(roleIds: value);
return ret; return ret;
} }
/// <summary> /// <summary>
/// 保存新建/更新的角色信息 /// 保存新建/更新的角色信息
/// </summary> /// </summary>
@ -161,9 +167,9 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAdapterManager.ToDBValue(p.Description))); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAdapterManager.ToDBValue(p.Description)));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
} }
CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string> { p.Id });
return ret; return ret;
} }
/// <summary> /// <summary>
/// 查询某个菜单所拥有的角色 /// 查询某个菜单所拥有的角色
/// </summary> /// </summary>
@ -190,6 +196,7 @@ namespace Bootstrap.DataAccess
} }
return roles; return roles;
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -225,7 +232,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
} }
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -236,6 +242,7 @@ namespace Bootstrap.DataAccess
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 根据GroupId查询和该Group有关的所有Roles /// 根据GroupId查询和该Group有关的所有Roles
/// </summary> /// </summary>
@ -262,6 +269,7 @@ namespace Bootstrap.DataAccess
} }
return roles; return roles;
} }
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary> /// </summary>
@ -297,7 +305,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
} }
CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -308,12 +315,14 @@ namespace Bootstrap.DataAccess
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<string> RetrieveRolesByUserName(string userName) => DbHelper.RetrieveRolesByUserName(userName); public virtual IEnumerable<string> RetrieveRolesByUserName(string userName) => DbHelper.RetrieveRolesByUserName(userName);
/// <summary> /// <summary>
/// 根据菜单url查询某个所拥有的角色 /// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查 /// 从NavigatorRole表查