控制器基类增加支持数据缓存

This commit is contained in:
FalconFly 2023-08-07 15:05:18 +08:00
parent 88d94787b5
commit 459dcf8548

View File

@ -1,6 +1,7 @@
using Falcon.SugarApi.DatabaseDefinitions; using Falcon.SugarApi.DatabaseDefinitions;
using Falcon.SugarApi.ModelValidation; using Falcon.SugarApi.ModelValidation;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
@ -19,6 +20,10 @@ namespace Falcon.SugarApi.ApiDefinistions
[Route("[Area]/[Controller]/[Action]")] [Route("[Area]/[Controller]/[Action]")]
public abstract class ApiControllerBase:ControllerBase public abstract class ApiControllerBase:ControllerBase
{ {
/// <summary>
/// 系统缓存
/// </summary>
public IDistributedCache? Cache { get; set; }
/// <summary> /// <summary>
/// 日志记录服务 /// 日志记录服务
/// </summary> /// </summary>
@ -55,6 +60,7 @@ namespace Falcon.SugarApi.ApiDefinistions
this.Services = service; this.Services = service;
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger ?? throw new NullReferenceException("ILogger"); this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger ?? throw new NullReferenceException("ILogger");
this.SugarDb = service.GetService<SugarDbContext>() ?? throw new NullReferenceException("SugarDbContext"); this.SugarDb = service.GetService<SugarDbContext>() ?? throw new NullReferenceException("SugarDbContext");
this.Cache = service.GetService<IDistributedCache>();
} }
#region #region
@ -237,5 +243,75 @@ namespace Falcon.SugarApi.ApiDefinistions
=> await Task.FromResult(FailResult<T>(failMessage)); => await Task.FromResult(FailResult<T>(failMessage));
#endregion #endregion
#region
/// <summary>
/// 获取缓存字符串
/// </summary>
/// <param name="key">存储的键</param>
/// <returns>缓存的字符串</returns>
protected string GetCache(string key) {
if(this.Cache == null) {
return string.Empty;
}
if(key.IsNullOrEmpty()) {
return string.Empty; ;
}
return this.Cache.GetString(key);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key">存储的键</param>
/// <param name="val">要存储的值</param>
/// <param name="span">存储过期时间</param>
protected void SetCache(string key,string val,TimeSpan? span = null) {
if(this.Cache == null) {
return;
}
if(key.IsNullOrEmpty() || val.IsNotNullOrEmpty()) {
return;
}
if(span == null) {
span = TimeSpan.FromMinutes(1);
}
this.Cache.SetString(key,val,new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = span });
}
/// <summary>
/// 获取缓存的对象
/// </summary>
/// <typeparam name="T">缓存对象的类型</typeparam>
/// <param name="key">缓存的键</param>
/// <returns>缓存的对象</returns>
protected T? GetCache<T>(string key) {
if(this.Cache == null) {
return default(T);
}
var value = this.Cache.GetString(key);
if(value == null)
return default(T);
else
return JsonSerializer.Deserialize<T>(value);
}
/// <summary>
/// 设置对象缓存
/// </summary>
/// <typeparam name="T">缓存对象的类型</typeparam>
/// <param name="key">缓存的键</param>
/// <param name="val">缓存的对象</param>
/// <param name="span">存储过期时间</param>
protected void SetCache<T>(string key,T val,TimeSpan? span = null) {
if(this.Cache == null) {
return;
}
if(key.IsNullOrEmpty() || val == null) {
return;
}
var str = JsonSerializer.Serialize(val);
this.Cache.SetString(key,str,new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = span });
}
#endregion
} }
} }