重构代码:移动Group缓存处理到GroupHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-31 09:51:28 +08:00
parent 771dbed0a6
commit dfd4913deb
6 changed files with 127 additions and 16 deletions

View File

@ -23,6 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api
{
return value.RetrieveData();
}
/// <summary>
///
/// </summary>
@ -33,6 +34,7 @@ namespace Bootstrap.Admin.Controllers.Api
{
return GroupHelper.RetrieveGroups().FirstOrDefault(t => t.Id == id);
}
/// <summary>
///
/// </summary>
@ -42,6 +44,7 @@ namespace Bootstrap.Admin.Controllers.Api
{
return GroupHelper.SaveGroup(value);
}
/// <summary>
///
/// </summary>
@ -51,8 +54,9 @@ namespace Bootstrap.Admin.Controllers.Api
{
return GroupHelper.DeleteGroup(value);
}
/// <summary>
///
/// 获取部门授权
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
@ -74,8 +78,9 @@ namespace Bootstrap.Admin.Controllers.Api
}
return ret;
}
/// <summary>
///
/// 保存部门授权
/// </summary>
/// <param name="id"></param>
/// <param name="groupIds"></param>

View File

@ -1,5 +1,6 @@
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
@ -14,8 +15,79 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroups()
{
var groups = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Group>("Groups");
return groups.Find(FilterDefinition<DataAccess.Group>.Empty).ToList();
return MongoDbAccessManager.Groups.Find(FilterDefinition<DataAccess.Group>.Empty).ToList();
}
/// <summary>
///
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveGroup(DataAccess.Group p)
{
if (p.Id == "0")
{
p.Id = null;
MongoDbAccessManager.Groups.InsertOne(p);
return true;
}
else
{
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<DataAccess.Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteGroup(IEnumerable<string> value)
{
var list = new List<WriteModel<DataAccess.Group>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<DataAccess.Group>(Builders<DataAccess.Group>.Filter.Eq(g => g.Id, id)));
}
MongoDbAccessManager.Groups.BulkWrite(list);
return true;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroupsByUserId(string userId)
{
var groups = GroupHelper.RetrieveGroups();
var user = UserHelper.RetrieveUsers().Cast<User>().FirstOrDefault(u => u.Id == userId);
groups.ToList().ForEach(g => g.Checked = user.Groups.Any(id => id == g.Id) ? "checked" : "");
return groups;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public override bool SaveGroupsByUserId(string userId, IEnumerable<string> groupIds)
{
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Groups, groupIds));
return true;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public override bool SaveGroupsByRoleId(string roleId, IEnumerable<string> groupIds)
{
return base.SaveGroupsByRoleId(roleId, groupIds);
}
}
}

View File

@ -16,6 +16,7 @@ namespace Bootstrap.DataAccess.MongoDB
{
private static IMongoDatabase _db = null;
private static bool _register = false;
/// <summary>
///
/// </summary>
@ -36,6 +37,7 @@ namespace Bootstrap.DataAccess.MongoDB
return _db;
}
}
/// <summary>
///
/// </summary>
@ -46,6 +48,7 @@ namespace Bootstrap.DataAccess.MongoDB
return DBAccess.GetCollection<DataAccess.Log>("Logs");
}
}
/// <summary>
///
/// </summary>
@ -66,6 +69,7 @@ namespace Bootstrap.DataAccess.MongoDB
return DBAccess.GetCollection<BootstrapDict>("Dicts");
}
}
/// <summary>
///
/// </summary>
@ -76,7 +80,18 @@ namespace Bootstrap.DataAccess.MongoDB
return DBAccess.GetCollection<User>("Users");
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.Group> Groups
{
get
{
return DBAccess.GetCollection<DataAccess.Group>("Groups");
}
}
private static void InitDb()
{
var connectString = DbAdapterManager.GetConnectionString("ba");

View File

@ -36,7 +36,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(groupIds: value);
ret = true;
}
catch (Exception ex)
@ -76,7 +75,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List<string>() { userId });
ret = true;
}
catch (Exception ex)
@ -113,7 +111,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)

View File

@ -66,7 +66,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
}
CacheCleanUtility.ClearCache(groupIds: value);
return ret;
}
/// <summary>
@ -89,7 +88,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAdapterManager.ToDBValue(p.Description)));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
return ret;
}
/// <summary>
@ -153,7 +151,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List<string>() { userId });
ret = true;
}
catch (Exception ex)
@ -223,7 +220,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)

View File

@ -13,23 +13,36 @@ namespace Bootstrap.DataAccess
public const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId";
public const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId";
public const string RetrieveGroupsByUserNameDataKey = "GroupHelper-RetrieveGroupsByUserName";
/// <summary>
/// 查询所有群组信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Group> RetrieveGroups() => CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbAdapterManager.Create<Group>().RetrieveGroups());
/// <summary>
/// 删除群组信息
/// </summary>
/// <param name="ids"></param>
public static bool DeleteGroup(IEnumerable<string> value) => DbAdapterManager.Create<Group>().DeleteGroup(value);
public static bool DeleteGroup(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<Group>().DeleteGroup(value);
if (ret) CacheCleanUtility.ClearCache(groupIds: value);
return ret;
}
/// <summary>
/// 保存新建/更新的群组信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveGroup(Group p) => DbAdapterManager.Create<Group>().SaveGroup(p);
public static bool SaveGroup(Group p)
{
var ret = DbAdapterManager.Create<Group>().SaveGroup(p);
if (ret) CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
return ret;
}
/// <summary>
/// 根据用户查询部门信息
/// </summary>
@ -43,20 +56,33 @@ namespace Bootstrap.DataAccess
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public static bool SaveGroupsByUserId(string userId, IEnumerable<string> groupIds) => DbAdapterManager.Create<Group>().SaveGroupsByUserId(userId, groupIds);
public static bool SaveGroupsByUserId(string userId, IEnumerable<string> groupIds)
{
var ret = DbAdapterManager.Create<Group>().SaveGroupsByUserId(userId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List<string>() { userId });
return ret;
}
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public static IEnumerable<Group> RetrieveGroupsByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbAdapterManager.Create<Group>().RetrieveGroupsByRoleId(roleId), RetrieveGroupsByRoleIdDataKey);
/// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表
/// </summary>
/// <param name="roleId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public static bool SaveGroupsByRoleId(string roleId, IEnumerable<string> groupIds) => DbAdapterManager.Create<Group>().SaveGroupsByRoleId(roleId, groupIds);
public static bool SaveGroupsByRoleId(string roleId, IEnumerable<string> groupIds)
{
var ret = DbAdapterManager.Create<Group>().SaveGroupsByRoleId(roleId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List<string>() { roleId });
return ret;
}
/// <summary>
///
/// </summary>