基于 abp vNext 和 .NET Core 开发博客项目 - 使用Redis缓存数据 (2)

我们可以在DistributedCacheEntryOptions中可以配置我们的缓存过期时间,其中有一个判断条件,就是当minutes = -1的时候,不指定过期时间,那么我们的缓存就不会过期了。

GetStringAsync()、SetStringAsync()是DistributedCacheExtensions的扩展方法,最终会将缓存项cacheItem转换成JSON格式进行存储。

CacheStrategy是在.Domain.Shared层定义的缓存过期时间策略常量。

//MeowvBlogConsts.cs ... /// <summary> /// 缓存过期时间策略 /// </summary> public static class CacheStrategy { /// <summary> /// 一天过期24小时 /// </summary> public const int ONE_DAY = 1440; /// <summary> /// 12小时过期 /// </summary> public const int HALF_DAY = 720; /// <summary> /// 8小时过期 /// </summary> public const int EIGHT_HOURS = 480; /// <summary> /// 5小时过期 /// </summary> public const int FIVE_HOURS = 300; /// <summary> /// 3小时过期 /// </summary> public const int THREE_HOURS = 180; /// <summary> /// 2小时过期 /// </summary> public const int TWO_HOURS = 120; /// <summary> /// 1小时过期 /// </summary> public const int ONE_HOURS = 60; /// <summary> /// 半小时过期 /// </summary> public const int HALF_HOURS = 30; /// <summary> /// 5分钟过期 /// </summary> public const int FIVE_MINUTES = 5; /// <summary> /// 1分钟过期 /// </summary> public const int ONE_MINUTE = 1; /// <summary> /// 永不过期 /// </summary> public const int NEVER = -1; } ...

接下来去创建缓存接口类和实现类,然后再我们的引用服务层.Application中进行调用,拿上一篇中接入GitHub的几个接口来做新增缓存操作。

和.Application层格式一样,在.Application.Caching中新建Authorize文件夹,添加缓存接口IAuthorizeCacheService和实现类AuthorizeCacheService。

注意命名规范,实现类肯定要继承一个公共的CachingServiceBase基类。在.Application.Caching层根目录添加MeowvBlogApplicationCachingServiceBase.cs,继承ITransientDependency。

//MeowvBlogApplicationCachingServiceBase.cs using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.DependencyInjection; namespace Meowv.Blog.Application.Caching { public class CachingServiceBase : ITransientDependency { public IDistributedCache Cache { get; set; } } }

然后使用属性注入的方式,注入IDistributedCache。这样我们只要继承了基类:CachingServiceBase,就可以愉快的使用缓存了。

添加要缓存的接口到IAuthorizeCacheService,在这里我们使用Func()方法,我们的接口返回什么类型由Func()来决定,于是添加三个接口如下:

//IAuthorizeCacheService.cs using Meowv.Blog.ToolKits.Base; using System; using System.Threading.Tasks; namespace Meowv.Blog.Application.Caching.Authorize { public interface IAuthorizeCacheService { /// <summary> /// 获取登录地址(GitHub) /// </summary> /// <returns></returns> Task<ServiceResult<string>> GetLoginAddressAsync(Func<Task<ServiceResult<string>>> factory); /// <summary> /// 获取AccessToken /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<ServiceResult<string>> GetAccessTokenAsync(string code, Func<Task<ServiceResult<string>>> factory); /// <summary> /// 登录成功,生成Token /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<ServiceResult<string>> GenerateTokenAsync(string access_token, Func<Task<ServiceResult<string>>> factory); } }

是不是和IAuthorizeService代码很像,的确,我就是直接复制过来改的。

在AuthorizeCacheService中实现接口。

//AuthorizeCacheService.cs using Meowv.Blog.ToolKits.Base; using Meowv.Blog.ToolKits.Extensions; using System; using System.Threading.Tasks; using static Meowv.Blog.Domain.Shared.MeowvBlogConsts; namespace Meowv.Blog.Application.Caching.Authorize.Impl { public class AuthorizeCacheService : CachingServiceBase, IAuthorizeCacheService { private const string KEY_GetLoginAddress = "Authorize:GetLoginAddress"; private const string KEY_GetAccessToken = "Authorize:GetAccessToken-{0}"; private const string KEY_GenerateToken = "Authorize:GenerateToken-{0}"; /// <summary> /// 获取登录地址(GitHub) /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult<string>> GetLoginAddressAsync(Func<Task<ServiceResult<string>>> factory) { return await Cache.GetOrAddAsync(KEY_GetLoginAddress, factory, CacheStrategy.NEVER); } /// <summary> /// 获取AccessToken /// </summary> /// <param></param> /// <param></param> /// <returns></returns> public async Task<ServiceResult<string>> GetAccessTokenAsync(string code, Func<Task<ServiceResult<string>>> factory) { return await Cache.GetOrAddAsync(KEY_GetAccessToken.FormatWith(code), factory, CacheStrategy.FIVE_MINUTES); } /// <summary> /// 登录成功,生成Token /// </summary> /// <param></param> /// <param></param> /// <returns></returns> public async Task<ServiceResult<string>> GenerateTokenAsync(string access_token, Func<Task<ServiceResult<string>>> factory) { return await Cache.GetOrAddAsync(KEY_GenerateToken.FormatWith(access_token), factory, CacheStrategy.ONE_HOURS); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspxgs.html