深圳网站建设服务平台,天津网站推广优化,长沙网站建设的公司,做效果图挣钱的网站系列文章使用 abp cli 搭建项目给项目瘦身#xff0c;让它跑起来完善与美化#xff0c;Swagger登场数据访问和代码优先自定义仓储之增删改查统一规范API#xff0c;包装返回模型再说Swagger#xff0c;分组、描述、小绿锁接入GitHub#xff0c;用JWT保护你的API异常处理和… 系列文章使用 abp cli 搭建项目给项目瘦身让它跑起来完善与美化Swagger登场数据访问和代码优先自定义仓储之增删改查统一规范API包装返回模型再说Swagger分组、描述、小绿锁接入GitHub用JWT保护你的API异常处理和日志记录使用Redis缓存数据集成Hangfire实现定时任务处理用AutoMapper搞定对象映射定时任务最佳实战一定时任务最佳实战二定时任务最佳实战三博客接口实战篇一博客接口实战篇二博客接口实战篇三上篇文章完成了文章增删改的接口和友情链接列表的接口本篇继续。善于思考的同学肯定发现在执行增删改操作后Redis缓存中的数据还是存在的也就意味着查询接口返回的数据还是旧的所以在写接口之前先完成一下清缓存的操作。移除缓存移除缓存我这里找了一个新的包Caching.CSRedis选他是因为微软的包Microsoft.Extensions.Caching.StackExchangeRedis没有给我们实现批量删除的功能。Caching.CSRedis开源地址https://github.com/2881099/csredis 在这不做过多介绍感兴趣的自己去看。在.Application.Caching层添加包Caching.CSRedisInstall-Package Caching.CSRedis然后在模块类MeowvBlogApplicationCachingModule中进行配置。//MeowvBlogApplicationCachingModule.cs
...
public override void ConfigureServices(ServiceConfigurationContext context)
{...var csredis new CSRedis.CSRedisClient(AppSettings.Caching.RedisConnectionString);RedisHelper.Initialization(csredis);context.Services.AddSingletonIDistributedCache(new CSRedisCache(RedisHelper.Instance));
}
...
直接新建一个移除缓存的接口ICacheRemoveService添加移除缓存的方法RemoveAsync()。代码较少可以直接写在缓存基类CachingServiceBase中。public interface ICacheRemoveService
{/// summary/// 移除缓存/// /summary/// param namekey/param/// param namecursor/param/// returns/returnsTask RemoveAsync(string key, int cursor 0);
}
然后可以在基类中实现这个接口。public async Task RemoveAsync(string key, int cursor 0)
{var scan await RedisHelper.ScanAsync(cursor);var keys scan.Items;if (keys.Any() key.IsNotNullOrEmpty()){keys keys.Where(x x.StartsWith(key)).ToArray();await RedisHelper.DelAsync(keys);}
}
简单说一下这个操作过程使用ScanAsync()获取到所有的Redis key值返回的是一个string数组然后根据参数找到符合此前缀的所有key最后调用DelAsync(keys)删除缓存。在需要有移除缓存功能的接口上继承ICacheRemoveService这里就是IBlogCacheService。//IBlogCacheService.cs
namespace Meowv.Blog.Application.Caching.Blog
{public partial interface IBlogCacheService : ICacheRemoveService{}
}
在基类中已经实现了这个接口所以现在所有基层基类的缓存实现类都可以调用移除缓存方法了。在MeowvBlogConsts中添加缓存前缀的常量。//MeowvBlogConsts.cs
/// summary
/// 缓存前缀
/// /summary
public static class CachePrefix
{public const string Authorize Authorize;public const string Blog Blog;public const string Blog_Post Blog :Post;public const string Blog_Tag Blog :Tag;public const string Blog_Category Blog :Category;public const string Blog_FriendLink Blog :FriendLink;
}
然后在BlogService.Admin.cs服务执行增删改后调用移除缓存的方法。//BlogService.Admin.cs// 执行清除缓存操作
await _blogCacheService.RemoveAsync(CachePrefix.Blog_Post);
因为是小项目采用这种策略直接删除缓存这样就搞定了当在执行增删改操作后前台接口可以实时查询出最后的结果。文章详情当我们修改文章数据的时候是需要把当前数据库中的数据带出来显示在界面上的因为有可能只是个别地方需要修改所以这还需要一个查询文章详情的接口当然这里的详情和前端的是不一样的这里是需要根据Id主键去查询。添加模型类PostForAdminDto.cs直接继承PostDto然后添加一个Tags列表就行好像和上一篇文章中的EditPostInput字段是一模一样的。顺手将EditPostInput改一下吧具体代码如下//PostForAdminDto.cs
using System.Collections.Generic;namespace Meowv.Blog.Application.Contracts.Blog
{public class PostForAdminDto : PostDto{/// summary/// 标签列表/// /summarypublic IEnumerablestring Tags { get; set; }}
}//EditPostInput.cs
namespace Meowv.Blog.Application.Contracts.Blog.Params
{public class EditPostInput : PostForAdminDto{}
}
在IBlogService.Admin.cs中添加接口。/// summary
/// 获取文章详情
/// /summary
/// param nameid/param
/// returns/returns
TaskServiceResultPostForAdminDto GetPostForAdminAsync(int id);
实现这个接口。/// summary
/// 获取文章详情
/// /summary
/// param nameid/param
/// returns/returns
public async TaskServiceResultPostForAdminDto GetPostForAdminAsync(int id)
{var result new ServiceResultPostForAdminDto();var post await _postRepository.GetAsync(id);var tags from post_tags in await _postTagRepository.GetListAsync()join tag in await _tagRepository.GetListAsync()on post_tags.TagId equals tag.Idwhere post_tags.PostId.Equals(post.Id)select tag.TagName;var detail ObjectMapper.MapPost, PostForAdminDto(post);detail.Tags tags;detail.Url post.Url.Split(/).Where(x !string.IsNullOrEmpty(x)).Last();result.IsSuccess(detail);return result;
}
先根据Id查出文章数据再通过联合查询找出标签数据。CreateMapPost, PostForAdminDto().ForMember(x x.Tags, opt opt.Ignore());
新建一条AutoMapper配置将Post转换成PostForAdminDto忽略Tags。然后将查出来的标签、Url赋值给DTO输出即可。在BlogController.Admin中添加API。/// summary
/// 获取文章详情
/// /summary
/// param nameid/param
/// returns/returns
[HttpGet]
[Authorize]
[Route(admin/post)]
[ApiExplorerSettings(GroupName Grouping.GroupName_v2)]
public async TaskServiceResultPostForAdminDto GetPostForAdminAsync([Required] int id)
{return await _blogService.GetPostForAdminAsync(id);
}
至此完成了关于文章的所有接口。接下来按照以上方式依次完成分类、标签、友链的增删改查接口我觉得如果你有跟着我一起做剩下的可以自己完成。开源地址https://github.com/Meowv/Blog/tree/blog_tutorial搭配下方课程学习更佳 ↓ ↓ ↓