重构缓存清理模块,集中到CacheClearUtility静态类中统一处理系统缓存,增强可读性与通用性

This commit is contained in:
Argo-Lenovo 2016-11-05 12:11:16 +08:00
parent 8aa8ba1069
commit aa4bf3c989
9 changed files with 245 additions and 178 deletions

View File

@ -56,6 +56,7 @@
<Compile Include="..\bldver.cs">
<Link>bldver.cs</Link>
</Compile>
<Compile Include="CacheCleanUtility.cs" />
<Compile Include="DBAccessManager.cs" />
<Compile Include="Dict.cs" />
<Compile Include="DictHelper.cs" />

View File

@ -0,0 +1,81 @@
using Longbow.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
internal static class CacheCleanUtility
{
/// <summary>
///
/// </summary>
/// <param name="roleIds"></param>
/// <param name="userIds"></param>
/// <param name="groupIds"></param>
/// <param name="menuIds"></param>
/// <param name="dictIds"></param>
/// <param name="logIds"></param>
internal static void ClearCache(string roleIds = null, string userIds = null, string groupIds = null, string menuIds = null, string dictIds = null, string logIds = null)
{
var cacheKeys = new List<string>();
if (roleIds != null)
{
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByRoleIDDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByRoleIDDataKey, id));
});
// final cleanup
CacheManager.Clear(key => cacheKeys.Any(k => k == key) || key.Contains(RoleHelper.RetrieveRolesDataKey) || key.Contains(MenuHelper.RetrieveMenusDataKey));
cacheKeys.Clear();
}
if (userIds != null)
{
userIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(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}", MenuHelper.RetrieveMenusByUserIDDataKey, id));
});
// final cleanup
CacheManager.Clear(key => cacheKeys.Any(k => k == key) || key.Contains(UserHelper.RetrieveUsersDataKey) || key.Contains(RoleHelper.RetrieveRolesByUserNameDataKey));
cacheKeys.Clear();
}
if (groupIds != null)
{
groupIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByGroupIDDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIDDataKey, id));
});
// final cleanup
CacheManager.Clear(key => cacheKeys.Any(k => k == key) || key.Contains(GroupHelper.RetrieveGroupsDataKey) || key.Contains(MenuHelper.RetrieveMenusDataKey));
cacheKeys.Clear();
}
if (menuIds != null)
{
menuIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIDDataKey, id));
});
// final cleanup
CacheManager.Clear(key => cacheKeys.Any(k => k == key) || key.Contains(MenuHelper.RetrieveMenusDataKey));
cacheKeys.Clear();
}
if (dictIds != null)
{
// final cleanup
CacheManager.Clear(key => key.Contains(DictHelper.RetrieveDictsDataKey));
cacheKeys.Clear();
}
if (logIds != null)
{
// final cleanup
CacheManager.Clear(key => key.Contains(LogHelper.RetrieveLogsDataKey));
cacheKeys.Clear();
}
}
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="Bootstrap.DataAccess.DictHelper">
<Position X="0.5" Y="5.5" Width="1.5" />
<Position X="0.5" Y="5.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AAAQAAACAAAAAAAAAAAACAAQAAAAAAAAAAAACAAAAAA=</HashCode>
<FileName>DictHelper.cs</FileName>
@ -14,10 +14,10 @@
<FileName>GroupHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.LogHelper" Collapsed="true">
<Position X="3" Y="5.5" Width="1.5" />
<Class Name="Bootstrap.DataAccess.LogHelper">
<Position X="3.25" Y="5.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAIACACEAAAAAAAAAAAAAAACAAAAAA=</HashCode>
<HashCode>AAAAAAAAAAAAAAAKACACEAAAAAAAAAAAAAAACAAAAAA=</HashCode>
<FileName>LogHelper.cs</FileName>
</TypeIdentifier>
</Class>
@ -42,5 +42,12 @@
<FileName>UserHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.CacheCleanUtility">
<Position X="8.75" Y="3.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAA=</HashCode>
<FileName>CacheCleanUtility.cs</FileName>
</TypeIdentifier>
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>

View File

@ -12,8 +12,7 @@ namespace Bootstrap.DataAccess
{
public static class DictHelper
{
private const string RetrieveDictsDataKey = "DictHelper-RetrieveDicts";
internal const string RetrieveDictsDataKey = "DictHelper-RetrieveDicts";
/// <summary>
/// 查询所有字典信息
/// </summary>
@ -49,7 +48,6 @@ namespace Bootstrap.DataAccess
}, CacheSection.RetrieveDescByKey(RetrieveDictsDataKey));
return id == 0 ? ret : ret.Where(t => id == t.ID);
}
/// <summary>
/// 删除字典中的数据
/// </summary>
@ -65,7 +63,7 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ClearCache();
CacheCleanUtility.ClearCache(dictIds: ids);
ret = true;
}
}
@ -102,7 +100,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
CacheCleanUtility.ClearCache(dictIds: p.ID == 0 ? "" : p.ID.ToString());
}
catch (DbException ex)
{
@ -110,12 +108,5 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 更新缓存
/// </summary>
private static void ClearCache()
{
CacheManager.Clear(key => key == RetrieveDictsDataKey);
}
}
}

View File

@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class GroupHelper
{
private const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
internal const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
internal const string RetrieveGroupsByUserIDDataKey = "GroupHelper-RetrieveGroupsByUserId";
internal const string RetrieveGroupsByRoleIDDataKey = "GroupHelper-RetrieveGroupsByRoleId";
/// <summary>
@ -67,7 +67,7 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
CacheManager.Clear(key => key == RetrieveGroupsDataKey);
CacheCleanUtility.ClearCache(groupIds: ids);
ret = true;
}
}
@ -100,8 +100,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(groupIds: p.ID == 0 ? "" : p.ID.ToString());
ret = true;
CacheManager.Clear(key => key == RetrieveGroupsDataKey);
}
catch (DbException ex)
{
@ -180,8 +180,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
groupIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(g => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveGroupsByUserIDDataKey, id) || key == string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIDDataKey, g)));
CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: id.ToString());
ret = true;
}
catch (Exception ex)
@ -260,8 +259,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
groupIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(g => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveGroupsByRoleIDDataKey, id) || key == string.Format("{0}-{1}", RoleHelper.RetrieveRolesByGroupIDDataKey, g)));
CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: id.ToString());
ret = true;
}
catch (Exception ex)

View File

@ -1,133 +1,126 @@
using Longbow;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Web;
namespace Bootstrap.DataAccess
{
public static class LogHelper
{
private const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
/// <summary>
/// 查询所有日志信息
/// </summary>
/// <param name="tId"></param>
/// <returns></returns>
public static IEnumerable<Log> RetrieveLogs(string tId = null)
{
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveLogsDataKey), key =>
{
string sql = "select * from Logs";
List<Log> Logs = new List<Log>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Logs.Add(new Log()
{
ID = (int)reader[0],
OperationType = (int)reader[1],
UserName = (string)reader[2],
OperationTime = (DateTime)reader[3],
OperationIp = (string)reader[4],
Remark = (string)reader[5],
OperationModule = (string)reader[6]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Logs;
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 删除日志信息
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public static bool DeleteLog(string ids)
{
bool ret = false;
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
try
{
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ClearCache();
ret = true;
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 保存新增的日志信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveLog(Log p)
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
string sql = "Insert Into Logs (OperationType, UserName,OperationTime,OperationIp,Remark,OperationModule) Values (@OperationType, @UserName,@OperationTime,@OperationIp,@Remark,@OperationModule)";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp == null ? "" : p.OperationIp, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Remark", p.Remark == null ? "" : p.Remark, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationModule", p.OperationModule == null ? "" : p.OperationModule, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
//更新缓存
private static void ClearCache()
{
CacheManager.Clear(key => key == RetrieveLogsDataKey);
}
/// <summary>
/// 获取客户端IP地址
/// </summary>
/// <returns></returns>
public static string GetClientIp()
{
HttpRequest request = HttpContext.Current.Request;
string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
result = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = request.UserHostAddress;
if (string.IsNullOrEmpty(result))
result = "0.0.0.0";
return result;
}
}
}
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Web;
namespace Bootstrap.DataAccess
{
public static class LogHelper
{
internal const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
/// <summary>
/// 查询所有日志信息
/// </summary>
/// <param name="tId"></param>
/// <returns></returns>
public static IEnumerable<Log> RetrieveLogs(string tId = null)
{
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveLogsDataKey), key =>
{
string sql = "select * from Logs";
List<Log> Logs = new List<Log>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Logs.Add(new Log()
{
ID = (int)reader[0],
OperationType = (int)reader[1],
UserName = (string)reader[2],
OperationTime = (DateTime)reader[3],
OperationIp = (string)reader[4],
Remark = (string)reader[5],
OperationModule = (string)reader[6]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Logs;
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 删除日志信息
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public static bool DeleteLog(string ids)
{
bool ret = false;
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
try
{
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
CacheCleanUtility.ClearCache(logIds: ids);
ret = true;
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 保存新增的日志信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveLog(Log p)
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
string sql = "Insert Into Logs (OperationType, UserName,OperationTime,OperationIp,Remark,OperationModule) Values (@OperationType, @UserName,@OperationTime,@OperationIp,@Remark,@OperationModule)";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp == null ? "" : p.OperationIp, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Remark", p.Remark == null ? "" : p.Remark, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationModule", p.OperationModule == null ? "" : p.OperationModule, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(logIds: p.ID == 0 ? "" : p.ID.ToString());
ret = true;
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 获取客户端IP地址
/// </summary>
/// <returns></returns>
public static string GetClientIp()
{
HttpRequest request = HttpContext.Current.Request;
string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
result = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = request.UserHostAddress;
if (string.IsNullOrEmpty(result))
result = "0.0.0.0";
return result;
}
}
}

View File

@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess
{
public static class MenuHelper
{
private const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus";
internal const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus";
internal const string RetrieveMenusByUserIDDataKey = "MenuHelper-RetrieveMenusByUserId";
/// <summary>
/// 查询所有菜单信息
@ -105,7 +105,7 @@ namespace Bootstrap.DataAccess
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id => CacheManager.Clear(key => key == string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIDDataKey, id)));
CacheCleanUtility.ClearCache(menuIds: ids);
ret = true;
}
catch (Exception ex)
@ -143,8 +143,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(menuIds: p.ID == 0 ? "" : p.ID.ToString());
ret = true;
CacheManager.Clear(key => key == RetrieveMenusDataKey);
}
catch (DbException ex)
{

View File

@ -18,9 +18,9 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class RoleHelper
{
private const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
internal const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
private const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
private const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
internal const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
internal const string RetrieveRolesByUserIDDataKey = "RoleHelper-RetrieveRolesByUserId";
internal const string RetrieveRolesByMenuIDDataKey = "RoleHelper-RetrieveRolesByMenuId";
internal const string RetrieveRolesByGroupIDDataKey = "RoleHelper-RetrieveRolesByGroupId";
@ -151,7 +151,7 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
CacheManager.Clear(key => key == RetrieveRolesDataKey);
CacheCleanUtility.ClearCache(roleIds: IDs);
ret = true;
}
}
@ -184,7 +184,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheManager.Clear(key => key == RetrieveRolesDataKey);
CacheCleanUtility.ClearCache(roleIds: p.ID == 0 ? "" : p.ID.ToString());
ret = true;
}
catch (DbException ex)
@ -258,8 +258,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByMenuIDDataKey, id) || key.Contains(MenuHelper.RetrieveMenusByUserIDDataKey)));
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: id.ToString());
ret = true;
}
catch (Exception ex)
@ -340,8 +339,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByGroupIDDataKey, id) || key == string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByRoleIDDataKey, r)));
CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: id.ToString());
ret = true;
}
catch (Exception ex)
@ -394,8 +392,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUrl(string url)
{
string key = string.Format("{0}-{1}", RetrieveRolesDataKey, url);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByUrlDataKey), k =>
{
string sql = "select r.ID, r.RoleName, r.[Description] from Roles r inner join NavigationRole nr on r.ID = nr.RoleID inner join Navigations n on nr.NavigationID = n.ID and n.Url = @URl";
List<Role> Roles = new List<Role>();
@ -418,7 +416,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RetrieveRolesDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesByUrlDataKey));
}
}
}

View File

@ -19,8 +19,8 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class UserHelper
{
private const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers";
internal const string RetrieveUsersByNameDataKey = "UserHelper-RetrieveUsersByName";
internal const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers";
private const string RetrieveUsersByNameDataKey = "UserHelper-RetrieveUsersByName";
internal const string RetrieveUsersByRoleIDDataKey = "UserHelper-RetrieveUsersByRoleId";
internal const string RetrieveUsersByGroupIDDataKey = "UserHelper-RetrieveUsersByGroupId";
/// <summary>
@ -106,7 +106,7 @@ namespace Bootstrap.DataAccess
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheManager.Clear(key => key == RetrieveUsersDataKey);
CacheCleanUtility.ClearCache(userIds: ids);
ret = true;
}
catch (Exception ex)
@ -142,8 +142,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", p.DisplayName, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: p.ID == 0 ? "" : p.ID.ToString());
ret = true;
CacheManager.Clear(key => key == RetrieveUsersDataKey || key == RetrieveUsersByNameDataKey);
}
catch (DbException ex)
{
@ -229,8 +229,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
userIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(u => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveUsersByRoleIDDataKey, id) || key == string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIDDataKey, u)));
CacheCleanUtility.ClearCache(userIds: userIds, roleIds: id.ToString());
ret = true;
}
catch (Exception ex)
@ -308,8 +307,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
userIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(u => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveUsersByGroupIDDataKey, id) || key == string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIDDataKey, u)));
CacheCleanUtility.ClearCache(userIds: userIds, groupIds: id.ToString());
ret = true;
}
catch (Exception ex)