整理系统缓存框架

This commit is contained in:
Argo-Lenovo 2016-11-04 16:06:40 +08:00
parent 20b92010cb
commit e6f310fe89
14 changed files with 403 additions and 152 deletions

View File

@ -112,6 +112,7 @@
<Content Include="Content\css\bootstrap.min.css" />
<Content Include="Content\css\font-awesome.min.css" />
<Content Include="Content\css\jquery.nestable.css" />
<Content Include="Content\css\tasks.css" />
<Content Include="Content\js\jquery.nestable.js" />
<Content Include="Content\css\site-responsive.css" />
<Content Include="Content\fonts\fontawesome-webfont.svg" />
@ -164,6 +165,7 @@
<Content Include="Scripts\Login.js" />
<Content Include="Scripts\Logs.js" />
<Content Include="Scripts\Menus.js" />
<Content Include="Scripts\Profiles.js" />
<Content Include="Scripts\Roles.js" />
<Content Include="Scripts\Users.js" />
<Content Include="Content\js\jquery-1.10.2.js" />

View File

@ -0,0 +1,182 @@
/*--------------Tasks Widget--------------*/
.task-content {
margin-bottom: 30px;
}
.tasks-widget .task-content:after {
clear: both;
}
.tasks-widget .task-footer {
margin-top: 5px;
}
.tasks-widget .task-footer:after,
.tasks-widget .task-footer:before {
content: "";
display: table;
line-height: 0;
}
.tasks-widget .task-footer:after {
clear: both;
}
.tasks-widget .task-list {
padding: 0;
margin: 0;
}
.tasks-widget .task-list > li {
position: relative;
padding: 10px 5px;
border-bottom: 1px dashed #eaeaea;
}
.tasks-widget .task-list li.last-line {
border-bottom: none;
}
.tasks-widget .task-list li > .task-bell {
margin-left: 10px;
}
.tasks-widget .task-list li > .task-checkbox {
float: left;
width: 30px;
}
.tasks-widget .task-list li > .task-title {
overflow: hidden;
margin-right: 10px;
}
.tasks-widget .task-list li > .task-title .task-value {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
position: absolute;
left: 190px;
right: 40px;
display: inline-block;
}
.tasks-widget .task-list li > .task-title .badge {
position: absolute;
display: inline-block;
left: 25px;
top: 2px;
}
.tasks-widget .task-list li > .task-title .label-success {
background-color: #5cb85c;
}
.tasks-widget .task-list li > .task-config {
position: absolute;
top: 10px;
right: 10px;
}
.tasks-widget .task-list li .task-title .task-title-sp {
margin-right: 5px;
max-width: 170px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
display: inline-block;
}
.tasks-widget .task-list li.task-done .task-title-sp {
text-decoration: line-through;
color: #bbbbbb;
}
.tasks-widget .task-list li.task-done {
background: #f6f6f6;
}
.tasks-widget .task-list li.task-done:hover {
background: #f4f4f4;
}
.tasks-widget .task-list li:hover {
background: #f9f9f9;
}
.tasks-widget .task-list li .task-config {
display: none;
}
.tasks-widget .task-list li:hover > .task-config {
display: block;
margin-bottom: 0 !important;
}
#sortable {
list-style-type: none;
margin: 0 0 20px 0;
padding: 0;
width: 100%;
}
#sortable li {
padding-left: 3em;
font-size: 12px;
}
#sortable li i {
position: absolute;
left: 6px;
padding: 4px 10px 0 10px;
cursor: pointer;
}
#sortable li input[type=checkbox] {
margin-top: 0;
}
.ui-sortable > li {
padding: 18px 0 15px 35px !important;
position: relative;
background: #f5f6f8;
margin-bottom: 2px;
border-bottom: none !important;
}
.ui-sortable li.list-primary {
border-left: 3px solid #41CAC0;
}
.ui-sortable li.list-success {
border-left: 3px solid #78CD51;
}
.ui-sortable li.list-danger {
border-left: 3px solid #FF6C60;
}
.ui-sortable li.list-warning {
border-left: 3px solid #F1C500;
}
.ui-sortable li.list-info {
border-left: 3px solid #58C9F3;
}
.ui-sortable li.list-inverse {
border-left: 3px solid #BEC3C7;
}
@media only screen and (max-width: 320px) {
.tasks-widget .task-config-btn {
float: inherit;
display: block;
}
.tasks-widget .task-list-projects li > .label {
margin-bottom: 5px;
}
}

View File

@ -0,0 +1,22 @@
$(function () {
var html = '<li class="list-primary"><i class="fa fa-ellipsis-v"></i><div class="task-title"><span class="task-title-sp">{2}</span><span class="badge badge-sm label-success">{0}</span><span class="task-value">{3}</span><div class="pull-right hidden-phone"><button class="btn btn-danger btn-xs fa fa-trash-o" data-val="{1}"></button></div></div></li>';
$.ajax({
url: '../../CacheList.axd',
type: 'GET',
success: function (result) {
if (result) {
result = $.parseJSON(result);
if ($.isArray(result)) {
var content = result.map(function (ele) {
return $.format(html, ele.Interval, ele.Key, ele.Desc, ele.Value);
}).join('');
$('#sortable').append(content);
}
}
else {
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
});

View File

@ -4,7 +4,10 @@
Layout = "~/Views/Shared/_Admin.cshtml";
}
@section Javascript {
<script src="~/Scripts/Profiles.js"></script>
}
@section css {
<link href="~/Content/css/tasks.css" rel="stylesheet" />
}
@section header {
@Html.Partial("Header", Model)
@ -34,5 +37,14 @@
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">网站缓存</div>
<div class="panel-body">
<div class="tasks-widget">
<ul id="sortable" class="task-list ui-sortable">
</ul>
</div>
</div>
</div>

View File

@ -23,21 +23,23 @@
</connectionStrings>
<cacheManager>
<add key="TerminalData-CodeTerminalHelper" interval="600" desc="输入口信息缓存" />
<add key="GroupData-CodeGroupHelper" interval="600" desc="群组信息缓存" />
<add key="UserData-CodeUserHelper-Role-" interval="600" desc="角色用户信息缓存" />
<add key="GroupData-CodeGroupHelper-Role-" interval="600" desc="角色部门信息缓存" />
<add key="UserData-CodeUserHelper" interval="600" desc="用户信息缓存" />
<add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" />
<add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" />
<add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" />
<add key="RoleHelper-RetrieveRolesByMenuId-menuId" interval="600" desc="菜单角色信息缓存"/>
<add key="MenuHelper-RetrieveMenus" interval="600" desc="菜单信息缓存" />
<add key="MenuHelper-RetrieveMenusByUserId-userId" interval="600" desc="用户菜单信息缓存"/>
<add key="LogData-CodeMenuHelper" interval="600" desc="日志信息缓存" />
<add key="DictData-CodeDictHelper" interval="600" desc="字典信息缓存" />
<add key="UserData-CodeUserHelper-Group-" interval="600" desc="用户信息缓存" />
<add key="GroupData-CodeGroupHelper-" interval="600" desc="用户部门信息缓存"/>
<add key="MenuHelper-RetrieveMenus" interval="600" desc="所有菜单数据缓存" />
<add key="MenuHelper-RetrieveMenusByUserId" interval="600" desc="指定用户菜单数据缓存"/>
<add key="UserHelper-RetrieveUsers" interval="600" desc="所有用户数据缓存"/>
<add key="UserHelper-RetrieveUsersByName" interval="600" desc="指定用户名的用户数据缓存"/>
<add key="UserHelper-RetrieveUsersByRoleId" interval="600" desc="指定角色的用户数据缓存"/>
<add key="UserHelper-RetrieveUsersByGroupId" interval="600" desc="指定组的用户数据缓存"/>
<add key="RoleHelper-RetrieveRoles" interval="600" desc="所有角色数据缓存"/>
<add key="RoleHelper-RetrieveRolesByUserId" interval="600" desc="指定用户的角色数据缓存"/>
<add key="RoleHelper-RetrieveRolesByMenuId" interval="600" desc="指定菜单的角色数据缓存"/>
<add key="RoleHelper-RetrieveRolesByGroupId" interval="600" desc="指定组的角色数据缓存"/>
<add key="RoleHelper-RetrieveRolesByUserName" interval="600" desc="指定用户名的角色数据缓存"/>
<add key="RoleHelper-RetrieveRolesByUrl" interval="600" desc="指定请求地址的角色数据缓存"/>
<add key="GroupHelper-RetrieveGroups" interval="600" desc="所有组数据缓存"/>
<add key="GroupHelper-RetrieveGroupsByUserId" interval="600" desc="指定用户的组数据缓存"/>
<add key="GroupHelper-RetrieveGroupsByRoleId" interval="600" desc="指定角色的组数据缓存"/>
<add key="LogHelper-RetrieveLogs" interval="600" desc="所有日志数据缓存"/>
<add key="DictHelper-RetrieveDicts" interval="600" desc="所有日志数据缓存"/>
</cacheManager>
<cacheManagerList>
@ -85,8 +87,10 @@
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0"/>
<add name="CacheListHandler" verb="*" path="CacheList.axd" type="Longbow.Caching.CacheListHandler, Longbow.V4"/>
</handlers>
<modules>
<add name="ExceptionManagement" type="Longbow.ExceptionManagement.ExceptionManagementModule, Longbow.ExceptionManagement"/>
</modules>
</system.webServer>
<runtime>

View File

@ -72,6 +72,7 @@
<Compile Include="UserHelper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram1.cd" />
<None Include="Longbow.Utility.snk" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -0,0 +1,46 @@
<?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" />
<TypeIdentifier>
<HashCode>AAAQAAACAAAAAAAAAAAACAAQAAAAAAAAAAAACAAAAAA=</HashCode>
<FileName>DictHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.GroupHelper">
<Position X="3.25" Y="0.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AAAAAAABAAAIAAAAAAAAAAIAABABAAQAAIAAAABAQBA=</HashCode>
<FileName>GroupHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.LogHelper" Collapsed="true">
<Position X="3" Y="5.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAIACACEAAAAAAAAAAAAAAACAAAAAA=</HashCode>
<FileName>LogHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.MenuHelper">
<Position X="8.75" Y="0.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AEAAAAAAAhAAAIAAAAAAAAAAAAAAAEIAAAAAAAAAAAA=</HashCode>
<FileName>MenuHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.RoleHelper">
<Position X="0.5" Y="0.5" Width="2.5" />
<TypeIdentifier>
<HashCode>RQQQAAAAAAAAgEAiIAEAAERAAACAAAAAAAAggAAAAAA=</HashCode>
<FileName>RoleHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Bootstrap.DataAccess.UserHelper">
<Position X="6" Y="0.5" Width="2.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAgAAAAEIQAkAAIEgEAIEAAAAAAAAAAAAAg=</HashCode>
<FileName>UserHelper.cs</FileName>
</TypeIdentifier>
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>

View File

@ -10,9 +10,9 @@ using System.Linq;
namespace Bootstrap.DataAccess
{
public class DictHelper
public static class DictHelper
{
private const string DictDataKey = "DictData-CodeDictHelper";
private const string RetrieveDictsDataKey = "DictHelper-RetrieveDicts";
/// <summary>
/// 查询所有字典信息
@ -21,7 +21,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Dict> RetrieveDicts(int id = 0)
{
var ret = CacheManager.GetOrAdd(DictDataKey, CacheSection.RetrieveIntervalByKey(DictDataKey), key =>
var ret = CacheManager.GetOrAdd(RetrieveDictsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveDictsDataKey), key =>
{
string sql = "select ID, Category, Name, Code, Define, case Define when 0 then '系统使用' else '用户自定义' end DefineName from Dicts";
List<Dict> Dicts = new List<Dict>();
@ -46,7 +46,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Dicts;
}, CacheSection.RetrieveDescByKey(DictDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveDictsDataKey));
return id == 0 ? ret : ret.Where(t => id == t.ID);
}
@ -75,7 +75,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 保存新建/更新的字典信息
/// </summary>
@ -111,13 +110,12 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 更新缓存
/// </summary>
private static void ClearCache()
{
CacheManager.Clear(key => key == DictDataKey);
CacheManager.Clear(key => key == RetrieveDictsDataKey);
}
}
}

View File

@ -18,9 +18,9 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class GroupHelper
{
private const string GroupDataKey = "GroupData-CodeGroupHelper";
private const string GroupUserIDDataKey = "GroupData-CodeGroupHelper-";
private const string GroupRoleIDDataKey = "GroupData-CodeGroupHelper-Role-";
private const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
internal const string RetrieveGroupsByUserIDDataKey = "GroupHelper-RetrieveGroupsByUserId";
internal const string RetrieveGroupsByRoleIDDataKey = "GroupHelper-RetrieveGroupsByRoleId";
/// <summary>
/// 查询所有群组信息
/// </summary>
@ -28,9 +28,9 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Group> RetrieveGroups(string tId = null)
{
string sql = "select * from Groups";
var ret = CacheManager.GetOrAdd(GroupDataKey, CacheSection.RetrieveIntervalByKey(GroupDataKey), key =>
var ret = CacheManager.GetOrAdd(RetrieveGroupsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveGroupsDataKey), key =>
{
string sql = "select * from Groups";
List<Group> Groups = new List<Group>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
@ -50,7 +50,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Groups;
}, CacheSection.RetrieveDescByKey(GroupDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveGroupsDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
@ -67,7 +67,7 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ClearCache();
CacheManager.Clear(key => key == RetrieveGroupsDataKey);
ret = true;
}
}
@ -101,7 +101,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
CacheManager.Clear(key => key == RetrieveGroupsDataKey);
}
catch (DbException ex)
{
@ -116,10 +116,10 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Group> RetrieveGroupsByUserId(int userId)
{
string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID";
string k = string.Format("{0}{1}", GroupUserIDDataKey, userId);
var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(GroupUserIDDataKey), key =>
string key = string.Format("{0}-{1}", RetrieveGroupsByUserIDDataKey, userId);
var ret = CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveGroupsByUserIDDataKey), k =>
{
string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID";
List<Group> Groups = new List<Group>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, ParameterDirection.Input));
@ -141,7 +141,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Groups;
}, CacheSection.RetrieveDescByKey(GroupUserIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveGroupsByUserIDDataKey));
return ret;
}
/// <summary>
@ -180,8 +180,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{
@ -191,11 +192,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
// 更新缓存
private static void ClearCache(string cacheKey = null)
{
CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
}
/// <summary>
/// 根据角色ID指派部门
/// </summary>
@ -203,8 +199,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Group> RetrieveGroupsByRoleId(int roleId)
{
string k = string.Format("{0}{1}", GroupRoleIDDataKey, roleId);
return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(GroupRoleIDDataKey), key =>
string k = string.Format("{0}-{1}", RetrieveGroupsByRoleIDDataKey, roleId);
return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RetrieveGroupsByRoleIDDataKey), key =>
{
List<Group> Groups = new List<Group>();
string sql = "select g.ID,g.GroupName,g.[Description],case rg.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join RoleGroup rg on g.ID=rg.GroupID and RoleID=@RoleID";
@ -228,7 +224,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Groups;
}, CacheSection.RetrieveDescByKey(GroupRoleIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveGroupsByRoleIDDataKey));
}
/// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表
@ -264,8 +260,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{

View File

@ -1,4 +1,5 @@
using Longbow.Caching;
using Longbow;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
@ -11,9 +12,9 @@ using System.Web;
namespace Bootstrap.DataAccess
{
public class LogHelper
public static class LogHelper
{
private const string LogDataKey = "LogData-CodeLogHelper";
private const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
/// <summary>
/// 查询所有日志信息
/// </summary>
@ -21,9 +22,9 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Log> RetrieveLogs(string tId = null)
{
string sql = "select * from Logs";
var ret = CacheManager.GetOrAdd(LogDataKey, CacheSection.RetrieveIntervalByKey(LogDataKey), key =>
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
@ -47,7 +48,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Logs;
}, CacheSection.RetrieveDescByKey(LogDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
@ -109,7 +110,7 @@ namespace Bootstrap.DataAccess
//更新缓存
private static void ClearCache()
{
CacheManager.Clear(key => key == LogDataKey);
CacheManager.Clear(key => key == RetrieveLogsDataKey);
}
/// <summary>

View File

@ -14,8 +14,8 @@ namespace Bootstrap.DataAccess
{
public static class MenuHelper
{
private const string MenuDataKey = "MenuHelper-RetrieveMenus";
private const string MenuByUserDataKey = "MenuHelper-RetrieveMenusByUserId-userId";
private const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus";
internal const string RetrieveMenusByUserIDDataKey = "MenuHelper-RetrieveMenusByUserId";
/// <summary>
/// 查询所有菜单信息
/// </summary>
@ -24,7 +24,7 @@ namespace Bootstrap.DataAccess
public static IEnumerable<Menu> RetrieveMenus(string tId = null)
{
string sql = "select n.*, d.Name as CategoryName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = N'菜单' and d.Define = 0";
var ret = CacheManager.GetOrAdd(MenuDataKey, CacheSection.RetrieveIntervalByKey(MenuDataKey), key =>
var ret = CacheManager.GetOrAdd(RetrieveMenusDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMenusDataKey), key =>
{
List<Menu> Menus = new List<Menu>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
@ -50,7 +50,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Menus;
}, CacheSection.RetrieveDescByKey(MenuDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveMenusDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
@ -60,9 +60,10 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Menu> RetrieveMenusByUserId(int userId)
{
string sql = "select distinct n.* from UserRole ur,NavigationRole nr,Navigations n where ur.RoleID=nr.RoleID and nr.NavigationID=n.ID and ur.UserID = @UserID";
return CacheManager.GetOrAdd(MenuByUserDataKey, CacheSection.RetrieveIntervalByKey(MenuByUserDataKey), key =>
string key = string.Format("{0}-{1}", RetrieveMenusByUserIDDataKey, userId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusByUserIDDataKey), k =>
{
string sql = "select distinct n.* from UserRole ur,NavigationRole nr,Navigations n where ur.RoleID=nr.RoleID and nr.NavigationID=n.ID and ur.UserID = @UserID";
List<Menu> Menus = new List<Menu>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
@ -87,7 +88,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Menus;
}, CacheSection.RetrieveDescByKey(MenuByUserDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveMenusByUserIDDataKey));
}
/// <summary>
/// 删除菜单信息
@ -104,7 +105,7 @@ namespace Bootstrap.DataAccess
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ClearCache();
ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id => CacheManager.Clear(key => key == string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIDDataKey, id)));
ret = true;
}
catch (Exception ex)
@ -143,7 +144,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
CacheManager.Clear(key => key == RetrieveMenusDataKey);
}
catch (DbException ex)
{
@ -151,10 +152,5 @@ namespace Bootstrap.DataAccess
}
return ret;
}
// 更新缓存
private static void ClearCache()
{
CacheManager.Clear(key => key == MenuDataKey);
}
}
}

View File

@ -16,12 +16,14 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
public class RoleHelper
public static class RoleHelper
{
private const string RoleDataKey = "RoleData-CodeRoleHelper";
private const string RolebyGroupDataKey = "RoleData-CodeRoleHelper-";
private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-";
private const string RoleNavigationIDDataKey = "RoleHelper-RetrieveRolesByMenuId-menuId";
private const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
private const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
private const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
internal const string RetrieveRolesByUserIDDataKey = "RoleHelper-RetrieveRolesByUserId";
internal const string RetrieveRolesByMenuIDDataKey = "RoleHelper-RetrieveRolesByMenuId";
internal const string RetrieveRolesByGroupIDDataKey = "RoleHelper-RetrieveRolesByGroupId";
/// <summary>
/// 查询所有角色
/// </summary>
@ -29,9 +31,9 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRoles(string tId = null)
{
string sql = "select * from Roles";
var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
var ret = CacheManager.GetOrAdd(RetrieveRolesDataKey, CacheSection.RetrieveIntervalByKey(RetrieveRolesDataKey), key =>
{
string sql = "select * from Roles";
List<Role> roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
@ -51,7 +53,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles;
}, CacheSection.RetrieveDescByKey(RoleDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
@ -90,8 +92,9 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByUserIDDataKey, id) || key == string.Format("{0}-{1}", UserHelper.RetrieveUsersByRoleIDDataKey, r)));
ret = true;
ClearCache();
}
catch (Exception ex)
{
@ -107,8 +110,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserId(int userId)
{
string k = string.Format("{0}{1}", RoleUserIDDataKey, userId);
return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleUserIDDataKey), key =>
string key = string.Format("{0}-{1}", RetrieveRolesByUserIDDataKey, userId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByUserIDDataKey), k =>
{
List<Role> Roles = new List<Role>();
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @UserID";
@ -132,9 +135,8 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RoleUserIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesByUserIDDataKey));
}
/// <summary>
/// 删除角色表
/// </summary>
@ -149,7 +151,7 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ClearCache();
CacheManager.Clear(key => key == RetrieveRolesDataKey);
ret = true;
}
}
@ -182,8 +184,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheManager.Clear(key => key == RetrieveRolesDataKey);
ret = true;
ClearCache();
}
catch (DbException ex)
{
@ -191,7 +193,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
@ -199,10 +200,10 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByMenuId(int menuId)
{
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID";
string k = string.Format("{0}{1}", RoleNavigationIDDataKey, menuId);
var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleNavigationIDDataKey), key =>
string key = string.Format("{0}-{1}", RetrieveRolesByMenuIDDataKey, menuId);
var ret = CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByMenuIDDataKey), k =>
{
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID";
List<Role> Roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input));
@ -224,7 +225,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesByMenuIDDataKey));
return ret;
}
public static bool SavaRolesByMenuId(int id, string roleIds)
@ -257,8 +258,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{
@ -268,12 +270,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
// 更新缓存
private static void ClearCache(string cacheKey = null)
{
CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
}
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// author:liuchun
@ -281,8 +277,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByGroupId(int groupID)
{
string key = string.Format("{0}{1}", RolebyGroupDataKey, groupID);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RolebyGroupDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveRolesByGroupIDDataKey, groupID);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByGroupIDDataKey), k =>
{
List<Role> Roles = new List<Role>();
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID";
@ -306,7 +302,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RolebyGroupDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesByGroupIDDataKey));
}
/// <summary>
@ -344,8 +340,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{
@ -355,7 +352,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 根据用户名查询某个用户所拥有的角色
/// 从UserRole表查
@ -364,8 +360,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserName(string username)
{
string key = string.Format("{0}{1}", RoleDataKey, username);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RoleDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, username);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByUserNameDataKey), k =>
{
List<Role> Roles = new List<Role>();
try
@ -388,7 +384,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RoleDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesByUserNameDataKey));
}
/// <summary>
/// 根据菜单url查询某个所拥有的角色
@ -398,8 +394,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUrl(string url)
{
string key = string.Format("{0}{1}", RoleDataKey, url);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RoleDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveRolesDataKey, url);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesDataKey), 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>();
@ -422,7 +418,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RoleDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveRolesDataKey));
}
}
}

View File

@ -3,6 +3,7 @@ using Longbow.Caching.Configuration;
using Longbow.Data;
using Longbow.ExceptionManagement;
using Longbow.Security;
using Longbow.Security.Principal;
using System;
using System.Collections.Generic;
using System.Data;
@ -18,10 +19,10 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class UserHelper
{
private const string UserDataKey = "UserData-CodeUserHelper";
private const string UserDisplayNameDataKey = "UserData-CodeUserHelper-";
private const string UserRoleIDDataKey = "UserData-CodeUserHelper-Role-";
private const string UserGroupIDDataKey = "UserData-CodeUserHelper-Group-";
private const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers";
internal const string RetrieveUsersByNameDataKey = "UserHelper-RetrieveUsersByName";
internal const string RetrieveUsersByRoleIDDataKey = "UserHelper-RetrieveUsersByRoleId";
internal const string RetrieveUsersByGroupIDDataKey = "UserHelper-RetrieveUsersByGroupId";
/// <summary>
/// 查询所有用户
/// </summary>
@ -30,7 +31,7 @@ namespace Bootstrap.DataAccess
public static IEnumerable<User> RetrieveUsers(string tId = null)
{
string sql = "select ID, UserName, DisplayName from Users";
var ret = CacheManager.GetOrAdd(UserDataKey, CacheSection.RetrieveIntervalByKey(UserDataKey), key =>
var ret = CacheManager.GetOrAdd(RetrieveUsersDataKey, CacheSection.RetrieveIntervalByKey(RetrieveUsersDataKey), key =>
{
List<User> Users = new List<User>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
@ -51,7 +52,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Users;
}, CacheSection.RetrieveDescByKey(UserDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveUsersDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
@ -61,9 +62,9 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static User RetrieveUsersByName(string userName)
{
if (Longbow.Security.Principal.LgbPrincipal.IsAdmin(userName)) return new User() { DisplayName = "网站管理员", UserName = userName };
string key = string.Format("{0}{1}", UserDisplayNameDataKey, userName);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(UserDisplayNameDataKey), k =>
if (LgbPrincipal.IsAdmin(userName)) return new User() { DisplayName = "网站管理员", UserName = userName };
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveUsersByNameDataKey), k =>
{
User user = null;
string sql = "select ID, UserName, [Password], PassSalt, DisplayName from Users where UserName = @UserName";
@ -88,7 +89,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return user;
}, CacheSection.RetrieveDescByKey(UserDisplayNameDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveUsersByNameDataKey));
}
/// <summary>
/// 删除用户
@ -105,7 +106,7 @@ namespace Bootstrap.DataAccess
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ClearCache();
CacheManager.Clear(key => key == RetrieveUsersDataKey);
ret = true;
}
catch (Exception ex)
@ -142,7 +143,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
CacheManager.Clear(key => key == RetrieveUsersDataKey || key == RetrieveUsersByNameDataKey);
}
catch (DbException ex)
{
@ -161,12 +162,6 @@ namespace Bootstrap.DataAccess
var user = RetrieveUsersByName(userName);
return user != null && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt);
}
// 更新缓存
private static void ClearCache(string cacheKey = null)
{
CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
}
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
@ -174,9 +169,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<User> RetrieveUsersByRoleId(int roleId)
{
string key = string.Format("{0}{1}", UserRoleIDDataKey, roleId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(UserDisplayNameDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveUsersByRoleIDDataKey, roleId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveUsersByNameDataKey), k =>
{
List<User> Users = new List<User>();
string sql = "select u.ID,u.UserName,u.DisplayName,case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID=ur.UserID and RoleID =@RoleID";
@ -200,7 +194,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Users;
}, CacheSection.RetrieveDescByKey(UserRoleIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveUsersByRoleIDDataKey));
}
/// <summary>
/// 通过角色ID保存当前授权用户插入
@ -235,8 +229,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{
@ -246,7 +241,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 通过groupId获取所有用户
/// </summary>
@ -254,9 +248,8 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<User> RetrieveUsersByGroupId(int groupId)
{
string key = string.Format("{0}{1}", UserGroupIDDataKey, groupId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(UserGroupIDDataKey), k =>
string key = string.Format("{0}-{1}", RetrieveUsersByGroupIDDataKey, groupId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveUsersByGroupIDDataKey), k =>
{
List<User> Users = new List<User>();
string sql = "select u.ID,u.UserName,u.DisplayName,case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID=ur.UserID and GroupID =@groupId";
@ -280,7 +273,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Users;
}, CacheSection.RetrieveDescByKey(UserRoleIDDataKey));
}, CacheSection.RetrieveDescByKey(RetrieveUsersByRoleIDDataKey));
}
/// <summary>
/// 通过部门ID保存当前授权用户插入
@ -315,8 +308,9 @@ 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)));
ret = true;
ClearCache();
}
catch (Exception ex)
{