feat: 使用新的缓存组件

This commit is contained in:
Argo-Tianyi 2022-01-13 10:56:24 +08:00
parent 4695b8e17e
commit d683f392f2
6 changed files with 53 additions and 7 deletions

View File

@ -8,6 +8,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BootstrapAdmin.Caching\BootstrapAdmin.Caching.csproj" />
<ProjectReference Include="..\BootstrapAdmin.Web.Core\BootstrapAdmin.Web.Core.csproj" />
</ItemGroup>

View File

@ -1,4 +1,5 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using BootstrapBlazor.Components;
using Longbow.Security.Cryptography;
@ -9,6 +10,8 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
class DictService : IDict
{
private const string DictServiceCacheKey = "DictService-GetAll";
private IDatabase Database { get; }
private string AppId { get; set; }
@ -24,7 +27,7 @@ class DictService : IDict
AppId = configuration.GetValue("AppId", "BA");
}
public List<Dict> GetAll() => Database.Fetch<Dict>();
public List<Dict> GetAll() => CacheManager.GetOrAdd(DictServiceCacheKey, entry => Database.Fetch<Dict>());
public Dictionary<string, string> GetApps()
{
@ -168,7 +171,16 @@ class DictService : IDict
return ret;
}
private bool SaveDict(Dict dict) => Database.Update<Dict>("set Code = @Code where Category = @Category and Name = @Name", dict) == 1;
private bool SaveDict(Dict dict)
{
var ret = Database.Update<Dict>("set Code = @Code where Category = @Category and Name = @Name", dict) == 1;
if (ret)
{
// 更新缓存
CacheManager.Clear(DictServiceCacheKey);
}
return ret;
}
public bool SaveLogin(string login) => SaveDict(new Dict { Category = "网站设置", Name = "登录界面", Code = login });

View File

@ -1,4 +1,5 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using PetaPoco;
@ -24,8 +25,11 @@ class NavigationService : INavigation
/// <returns>未层次化的菜单集合</returns>
public List<Navigation> GetAllMenus(string userName)
{
var order = Database.Provider.EscapeSqlIdentifier("Order");
return Database.Fetch<Models.Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = @Define left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.{order}", new { UserName = userName, Category = "菜单", RoleName = "Administrators", Define = EnumDictDefine.System });
return CacheManager.GetOrAdd($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-{userName}", entry =>
{
var order = Database.Provider.EscapeSqlIdentifier("Order");
return Database.Fetch<Models.Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = @Define left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.{order}", new { UserName = userName, Category = "菜单", RoleName = "Administrators", Define = EnumDictDefine.System });
});
}
/// <summary>
@ -50,7 +54,10 @@ class NavigationService : INavigation
Database.Execute("delete from NavigationRole where RoleID = @0", roleId);
Database.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId }));
Database.CompleteTransaction();
// 通知缓存更新
ret = true;
CacheManager.Clear($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-*");
}
catch (Exception)
{

View File

@ -0,0 +1,26 @@
using BootstrapAdmin.DataAccess.PetaPoco.Services;
using Microsoft.Extensions.Primitives;
namespace BootstrapAdmin.Caching;
/// <summary>
///
/// </summary>
public static class TokenManager
{
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static IChangeToken GetOrAdd(string key)
{
IChangeToken? token = null;
if (key.StartsWith($"{nameof(NavigationService)}-"))
{
// 菜单需要更新
//token = new CompositeChangeToken();
}
return token;
}
}

View File

@ -35,7 +35,7 @@ public partial class Healths
[Inject]
[NotNull]
private BootstrapBlazor.Web.Core.ICacheManager? CacheManager { get; set; }
private BootstrapAdmin.Caching.ICacheManager? CacheManager { get; set; }
[NotNull]
private HttpClient? Client { get; set; }