添加项目文件。
This commit is contained in:
110
Atomx.Data/CacheServices/RoleCacheService.cs
Normal file
110
Atomx.Data/CacheServices/RoleCacheService.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 重新加载角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ReloadRoleAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Role>> GetRolesAsync(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取角色
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<Role?> GetRoleById(long roleId, bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的权限点
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Permission>> GetAllPermissions(bool? reload = false);
|
||||
}
|
||||
|
||||
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 重新加载角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task ReloadRoleAsync()
|
||||
{
|
||||
|
||||
var data = _dbContext.Roles.Where(p => p.Enabled).ToList();
|
||||
if (data.Any())
|
||||
{
|
||||
await SetCacheAsync(CacheKeys.Roles, data);
|
||||
}
|
||||
}
|
||||
|
||||
// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Role>> GetRolesAsync(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Role>>(CacheKeys.Roles);
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var roles = (from p in _dbContext.Roles
|
||||
where p.Enabled
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Roles, roles);
|
||||
return roles;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取角色
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Role?> GetRoleById(long roleId, bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetRolesAsync(reload);
|
||||
return cacheData.Where(p => p.Id == roleId).SingleOrDefault();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的权限点
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Permission>> GetAllPermissions(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Permission>>(CacheKeys.Permissions);
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var permissions = await (from p in _dbContext.Permissions
|
||||
select p).ToListAsync();
|
||||
await SetCacheAsync(CacheKeys.Permissions, permissions);
|
||||
return permissions;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user