From 826271c84d532be9ea820c295a0f033d4e22b129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Mon, 22 Apr 2024 15:39:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98cru?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YiCacheCrudAppService.cs | 126 ++++++++++++++++++ .../Services/DictionaryTypeService.cs | 3 +- 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCacheCrudAppService.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCacheCrudAppService.cs b/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCacheCrudAppService.cs new file mode 100644 index 00000000..36498c5d --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCacheCrudAppService.cs @@ -0,0 +1,126 @@ +using Volo.Abp.Application.Dtos; +using Volo.Abp.Caching; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.MultiTenancy; + +namespace Yi.Framework.Ddd.Application +{ + public abstract class YiCacheCrudAppService : YiCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto + { + protected YiCacheCrudAppService(IRepository repository) : base(repository) + { + } + } + + public abstract class YiCacheCrudAppService + : YiCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto + { + protected YiCacheCrudAppService(IRepository repository) : base(repository) + { + } + } + + + public abstract class YiCacheCrudAppService + : YiCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto + { + protected YiCacheCrudAppService(IRepository repository) : base(repository) + { + } + } + + public abstract class YiCacheCrudAppService + : YiCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto + { + protected YiCacheCrudAppService(IRepository repository) : base(repository) + { + } + } + + + public abstract class YiCacheCrudAppService + : YiCrudAppService + where TEntity : class, IEntity + where TGetOutputDto : IEntityDto + where TGetListOutputDto : IEntityDto + { + protected IDistributedCache Cache => LazyServiceProvider.LazyGetRequiredService>(); + + protected string GetCacheKey(TKey id) => typeof(TEntity).Name + ":" + CurrentTenant.Id ?? Guid.Empty + ":" + id.ToString(); + protected YiCacheCrudAppService(IRepository repository) : base(repository) + { + } + + public override async Task UpdateAsync(TKey id, TUpdateInput input) + { + var output = await base.UpdateAsync(id, input); + await Cache.RemoveAsync(GetCacheKey(id)); + return output; + } + + public override async Task> GetListAsync(TGetListInput input) + { + //两种方式: + //1:全表缓存,使用缓存直接查询 + //2:非全部缓存,查询到的数据直接添加到缓存 + + //判断是否该实体为全表缓存 + throw new NotImplementedException(); + + //IDistributedCache 有局限性,条件查询无法进行缓存了 + //if (true) + //{ + // return await GetListByCacheAsync(input); + //} + //else + //{ + // return await GetListByDbAsync(input); + //} + + } + + protected virtual async Task> GetListByDbAsync(TGetListInput input) + { + //如果不是全表缓存,可以走这个啦 + throw new NotImplementedException(); + } + protected virtual async Task> GetListByCacheAsync(TGetListInput input) + { + //如果是全表缓存,可以走这个啦 + throw new NotImplementedException(); + } + + + protected override async Task GetEntityByIdAsync(TKey id) + { + var output = await Cache.GetOrAddAsync(GetCacheKey(id), async () => await base.GetEntityByIdAsync(id)); + return output!; + } + + public override async Task CreateAsync(TCreateInput input) + { + var output = await base.CreateAsync(input); + await Cache.RemoveAsync(GetCacheKey(output.Id)); + return output; + } + + public override async Task DeleteAsync(IEnumerable id) + { + await base.DeleteAsync(id); + foreach (var itemId in id) + { + await Cache.RemoveAsync(GetCacheKey(itemId)); + } + + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs index ce3ca28f..441e1793 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs @@ -1,5 +1,7 @@ +using Microsoft.Extensions.DependencyInjection; using SqlSugar; using Volo.Abp.Application.Dtos; +using Volo.Abp.Caching; using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.DictionaryType; using Yi.Framework.Rbac.Application.Contracts.IServices; @@ -36,6 +38,5 @@ namespace Yi.Framework.Rbac.Application.Services Items = await MapToGetListOutputDtosAsync(entities) }; } - } }