欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > >内容正文

asp.net

基于 abp vnext 和 .net core 开发博客项目 -凯发k8官方网

发布时间:2025/1/21 18 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 基于 abp vnext 和 .net core 开发博客项目 - 博客接口实战篇(四) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

基于 abp vnext 和 .net core 开发博客项目 - 博客接口实战篇(四)

转载于:https://github.com/meowv/blog

上篇文章完成了文章增删改的接口和凯发k8官方网的友情链接列表的接口,本篇继续。

善于思考的同学肯定发现,在执行增删改操作后,redis缓存中的数据还是存在的,也就意味着查询接口返回的数据还是旧的,所以在写接口之前,先完成一下清缓存的操作。

移除缓存

移除缓存我这里找了一个新的包:caching.csredis,选他是因为微软的包microsoft.extensions.caching.stackexchangeredis没有给我们实现批量删除的功能。

caching.csredis开源地址,https://github.com/2881099/csredis 在这不做过多介绍,感兴趣的自己去看。

在.application.caching层添加包caching.csredis,install-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.addsingleton(new csrediscache(redishelper.instance));

}

直接新建一个移除缓存的接口:icacheremoveservice,添加移除缓存的方法removeasync()。代码较少,可以直接写在缓存基类cachingservicebase中。

public interface icacheremoveservice
{
///
/// 移除缓存
///
///
///
///
task 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
///
/// 缓存前缀
///
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
{
///
/// 标签列表
///
public ienumerable tags { get; set; }
}
}

//editpostinput.cs
namespace meowv.blog.application.contracts.blog.params
{
public class editpostinput : postforadmindto
{
}
}
在iblogservice.admin.cs中添加接口。

///
/// 获取文章详情
///
///
///
task getpostforadminasync(int id);
实现这个接口。

///
/// 获取文章详情
///
///
///
public async task getpostforadminasync(int id)
{
var result = new serviceresult();

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.map(post); detail.tags = tags; detail.url = post.url.split("/").where(x => !string.isnullorempty(x)).last();result.issuccess(detail); return result;

}
先根据id查出文章数据,再通过联合查询找出标签数据。

createmap().formember(x => x.tags, opt => opt.ignore());
新建一条automapper配置,将post转换成postforadmindto,忽略tags。

然后将查出来的标签、url赋值给dto,输出即可。在blogcontroller.admin中添加api。

///
/// 获取文章详情
///
///
///
[httpget]
[authorize]
[route(“admin/post”)]
[apiexplorersettings(groupname = grouping.groupname_v2)]
public async task getpostforadminasync([required] int id)
{
return await _blogservice.getpostforadminasync(id);
}

图片

至此,完成了关于文章的所有接口。

接下来按照以上方式依次完成分类、标签、友链的增删改查接口,我觉得如果你有跟着我一起做,剩下的可以自己完成。

开源地址:https://github.com/meowv/blog/tree/blog_tutorial

总结

以上是凯发k8官方网为你收集整理的基于 abp vnext 和 .net core 开发博客项目 - 博客接口实战篇(四)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图