using Longbow.Cache;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
///
/// 缓存清理操作类
///
public static class CacheCleanUtility
{
private const string RetrieveAllRolesDataKey = "BootstrapAdminRoleMiddleware-RetrieveRoles";
///
/// 清理缓存
///
///
///
///
///
///
///
///
public static void ClearCache(IEnumerable roleIds = null, IEnumerable userIds = null, IEnumerable groupIds = null, IEnumerable menuIds = null, IEnumerable appIds = null, IEnumerable dictIds = null, string cacheKey = null)
{
var cacheKeys = new List();
var corsKeys = new List();
if (roleIds != null)
{
roleIds.ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByRoleIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByRoleIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByRoleIdDataKey, id));
});
cacheKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*");
corsKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
corsKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}
if (userIds != null)
{
userIds.ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIdDataKey, id));
cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
});
cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*");
cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
corsKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}
if (groupIds != null)
{
groupIds.ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByGroupIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIdDataKey, id));
});
cacheKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*");
corsKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
corsKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}
if (menuIds != null)
{
menuIds.ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id));
});
cacheKeys.Add(MenuHelper.RetrieveMenusByRoleIdDataKey + "*");
cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
corsKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
corsKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}
if (appIds != null)
{
cacheKeys.Add("AppHelper-RetrieveAppsBy*");
corsKeys.Add("AppHelper-RetrieveAppsBy*");
}
if (dictIds != null)
{
cacheKeys.Add(DictHelper.RetrieveDictsDataKey + "*");
cacheKeys.Add(DictHelper.RetrieveCategoryDataKey);
corsKeys.Add(DictHelper.RetrieveDictsDataKey + "*");
}
if (cacheKey != null)
{
cacheKeys.Add(cacheKey);
corsKeys.Add(cacheKey);
}
CacheManager.Clear(cacheKeys);
CacheManager.CorsClear(corsKeys.Distinct());
}
}
}