using Atomx.Common.Constant; using Atomx.Common.Entities; using Microsoft.EntityFrameworkCore; namespace Atomx.Data.CacheServices { public partial interface ICacheService { /// /// 重新加载角色 /// /// Task ReloadRoleAsync(); /// /// 获取缓存 /// /// /// Task> GetRolesAsync(bool? reload = false); /// /// 通过ID获取角色 /// /// /// /// Task GetRoleById(long roleId, bool? reload = false); /// /// 获取所有的权限点 /// /// /// Task> GetAllPermissions(bool? reload = false); } public partial class CacheService : ICacheService { /// /// 重新加载角色 /// /// public async Task ReloadRoleAsync() { var data = _dbContext.Roles.Where(p => p.Enabled).ToList(); if (data.Any()) { await SetCacheAsync(CacheKeys.Roles, data); } } // /// 获取缓存 /// /// public async Task> GetRolesAsync(bool? reload = false) { bool reloadData = reload.HasValue ? reload.Value : false; var cacheData = await GetCacheAsync>(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; } /// /// 通过ID获取角色 /// /// /// /// public async Task GetRoleById(long roleId, bool? reload = false) { var cacheData = await GetRolesAsync(reload); return cacheData.Where(p => p.Id == roleId).SingleOrDefault(); } /// /// 获取所有的权限点 /// /// /// public async Task> GetAllPermissions(bool? reload = false) { bool reloadData = reload.HasValue ? reload.Value : false; var cacheData = await GetCacheAsync>(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; } } }