From f4686f5d73e835c7a178384d17a189028c351491 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Mon, 6 May 2024 16:19:30 +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 --- .../NPinCacheCrudAppService.cs | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 framework/NPin.Framework.Ddd.Application/NPinCacheCrudAppService.cs diff --git a/framework/NPin.Framework.Ddd.Application/NPinCacheCrudAppService.cs b/framework/NPin.Framework.Ddd.Application/NPinCacheCrudAppService.cs new file mode 100644 index 0000000..11155cc --- /dev/null +++ b/framework/NPin.Framework.Ddd.Application/NPinCacheCrudAppService.cs @@ -0,0 +1,120 @@ +using Volo.Abp.Application.Dtos; +using Volo.Abp.Caching; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories; + +namespace NPin.Framework.Ddd.Application; + +public abstract class NPinCacheCrudAppService : NPinCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto +{ + protected NPinCacheCrudAppService(IRepository repository) : base(repository) + { + } +} + +public abstract class NPinCacheCrudAppService + : NPinCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto +{ + protected NPinCacheCrudAppService(IRepository repository) : base(repository) + { + } +} + +public abstract class NPinCacheCrudAppService + : NPinCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto +{ + protected NPinCacheCrudAppService(IRepository repository) : base(repository) + { + } +} + +public abstract class NPinCacheCrudAppService + : NPinCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto +{ + protected NPinCacheCrudAppService(IRepository repository) : base(repository) + { + } +} + +public abstract class NPinCacheCrudAppService + : NPinCrudAppService + 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 NPinCacheCrudAppService(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 DeleteAsync(IEnumerable id) + { + await base.DeleteAsync(id); + foreach (var itemId in id) + { + await Cache.RemoveAsync(GetCacheKey(itemId)); + } + } +} \ No newline at end of file