🎨 移除原有的RedisCacheAop 与CacheAop有些重复

This commit is contained in:
LemonNoCry 2023-06-12 11:34:39 +08:00
parent 60cade83d4
commit c1ce9e5972
No known key found for this signature in database
9 changed files with 14 additions and 126 deletions

View File

@ -164,7 +164,7 @@ namespace Blog.Core.Controllers
// 测试模拟异常,全局异常过滤器拦截 // 测试模拟异常,全局异常过滤器拦截
var i = 0; var i = 0;
var d = 3 / i; // var d = 3 / i;
// 测试 AOP 缓存 // 测试 AOP 缓存

View File

@ -41,10 +41,7 @@
"SubscriptionClientName": "Blog.Core" "SubscriptionClientName": "Blog.Core"
}, },
"AppSettings": { "AppSettings": {
"RedisCachingAOP": { "CachingAOP": {
"Enabled": false
},
"MemoryCachingAOP": {
"Enabled": true "Enabled": true
}, },
"LogToDb": true, "LogToDb": true,

View File

@ -61,8 +61,8 @@ namespace Blog.Core.AOP
var type = invocation.Method.ReturnType; var type = invocation.Method.ReturnType;
if (typeof(Task).IsAssignableFrom(type)) if (typeof(Task).IsAssignableFrom(type))
{ {
var resultProperty = type.GetProperty("Result"); dynamic result = invocation.ReturnValue;
response = resultProperty?.GetValue(invocation.ReturnValue); response = result.Result;
} }
else else
{ {

View File

@ -1,89 +0,0 @@
using Blog.Core.Common;
using Blog.Core.Extensions;
using Castle.DynamicProxy;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Blog.Core.AOP
{
/// <summary>
/// 面向切面的缓存使用
/// </summary>
public class BlogRedisCacheAOP : CacheAOPbase
{
//通过注入的方式,把缓存操作接口通过构造函数注入
private readonly IRedisBasketRepository _cache;
public BlogRedisCacheAOP(IRedisBasketRepository cache)
{
_cache = cache;
}
//Intercept方法是拦截的关键所在也是IInterceptor接口中的唯一定义
//代码已经合并 学习pr流程
public override void Intercept(IInvocation invocation)
{
var method = invocation.MethodInvocationTarget ?? invocation.Method;
if (method.ReturnType == typeof(void) || method.ReturnType == typeof(Task))
{
invocation.Proceed();
return;
}
//对当前方法的特性验证
var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute;
if (qCachingAttribute != null)
{
//获取自定义缓存键
var cacheKey = CustomCacheKey(invocation);
//注意是 string 类型方法GetValue
var cacheValue = _cache.GetValue(cacheKey).Result;
if (cacheValue != null)
{
//将当前获取到的缓存值,赋值给当前执行方法
Type returnType;
if (typeof(Task).IsAssignableFrom(method.ReturnType))
{
returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
}
else
{
returnType = method.ReturnType;
}
dynamic _result = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, returnType);
invocation.ReturnValue = (typeof(Task).IsAssignableFrom(method.ReturnType)) ? Task.FromResult(_result) : _result;
return;
}
//去执行当前的方法
invocation.Proceed();
//存入缓存
if (!string.IsNullOrWhiteSpace(cacheKey))
{
object response;
//Type type = invocation.ReturnValue?.GetType();
var type = invocation.Method.ReturnType;
if (typeof(Task).IsAssignableFrom(type))
{
var resultProperty = type.GetProperty("Result");
response = resultProperty.GetValue(invocation.ReturnValue);
}
else
{
response = invocation.ReturnValue;
}
if (response == null) response = string.Empty;
_cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration)).Wait();
}
}
else
{
invocation.Proceed();//直接执行被拦截方法
}
}
}
}

View File

@ -1,6 +1,7 @@
using StackExchange.Redis; using StackExchange.Redis;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Blog.Core.Extensions namespace Blog.Core.Extensions
@ -8,7 +9,7 @@ namespace Blog.Core.Extensions
/// <summary> /// <summary>
/// Redis缓存接口 /// Redis缓存接口
/// </summary> /// </summary>
[Obsolete("普通缓存考虑直接使用ICaching,如果要使用Redis队列等还是使用此类")] [Description("普通缓存考虑直接使用ICaching,如果要使用Redis队列等还是使用此类")]
public interface IRedisBasketRepository public interface IRedisBasketRepository
{ {

View File

@ -40,25 +40,14 @@ namespace Blog.Core.Extensions
{ {
Console.WriteLine($"Current authorization scheme: " + (Permissions.IsUseIds4 ? "Ids4" : "JWT")); Console.WriteLine($"Current authorization scheme: " + (Permissions.IsUseIds4 ? "Ids4" : "JWT"));
} }
// 缓存AOP
// Redis缓存AOP if (!AppSettings.app(new string[] { "AppSettings", "CachingAOP", "Enabled" }).ObjToBool())
if (!AppSettings.app(new string[] { "AppSettings", "RedisCachingAOP", "Enabled" }).ObjToBool())
{ {
Console.WriteLine($"Redis Caching AOP: False"); Console.WriteLine($"Caching AOP: False");
} }
else else
{ {
ConsoleHelper.WriteSuccessLine($"Redis Caching AOP: True"); ConsoleHelper.WriteSuccessLine($"Caching AOP: True");
}
// 内存缓存AOP
if (!AppSettings.app(new string[] { "AppSettings", "MemoryCachingAOP", "Enabled" }).ObjToBool())
{
Console.WriteLine($"Memory Caching AOP: False");
}
else
{
ConsoleHelper.WriteSuccessLine($"Memory Caching AOP: True");
} }
// 服务日志AOP // 服务日志AOP
@ -259,8 +248,7 @@ namespace Blog.Core.Extensions
List<string[]> aopInfos = new() List<string[]> aopInfos = new()
{ {
new string[] { "Redis缓存AOP", AppSettings.app("AppSettings", "RedisCachingAOP", "Enabled") }, new string[] { "缓存AOP", AppSettings.app("AppSettings", "CachingAOP", "Enabled") },
new string[] { "内存缓存AOP", AppSettings.app("AppSettings", "MemoryCachingAOP", "Enabled") },
new string[] { "服务日志AOP", AppSettings.app("AppSettings", "LogAOP", "Enabled") }, new string[] { "服务日志AOP", AppSettings.app("AppSettings", "LogAOP", "Enabled") },
new string[] { "事务AOP", AppSettings.app("AppSettings", "TranAOP", "Enabled") }, new string[] { "事务AOP", AppSettings.app("AppSettings", "TranAOP", "Enabled") },
new string[] { "Sql执行AOP", AppSettings.app("AppSettings", "SqlAOP", "Enabled") }, new string[] { "Sql执行AOP", AppSettings.app("AppSettings", "SqlAOP", "Enabled") },

View File

@ -39,13 +39,7 @@ namespace Blog.Core.Extensions
// AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。 // AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。
var cacheType = new List<Type>(); var cacheType = new List<Type>();
if (AppSettings.app(new string[] { "AppSettings", "RedisCachingAOP", "Enabled" }).ObjToBool()) if (AppSettings.app(new string[] { "AppSettings", "CachingAOP", "Enabled" }).ObjToBool())
{
builder.RegisterType<BlogRedisCacheAOP>();
cacheType.Add(typeof(BlogRedisCacheAOP));
}
if (AppSettings.app(new string[] { "AppSettings", "MemoryCachingAOP", "Enabled" }).ObjToBool())
{ {
builder.RegisterType<BlogCacheAOP>(); builder.RegisterType<BlogCacheAOP>();
cacheType.Add(typeof(BlogCacheAOP)); cacheType.Add(typeof(BlogCacheAOP));

View File

@ -13,7 +13,7 @@ namespace Blog.Core.Services
int a = 1; int a = 1;
int b = 0; int b = 0;
int c = a / b; // int c = a / b;
} }
//public IAdvertisementRepository dal = new AdvertisementRepository(); //public IAdvertisementRepository dal = new AdvertisementRepository();

View File

@ -45,10 +45,7 @@
"SubscriptionClientName": "Blog.Core" "SubscriptionClientName": "Blog.Core"
}, },
"AppSettings": { "AppSettings": {
"RedisCachingAOP": { "CachingAOP": {
"Enabled": false
},
"MemoryCachingAOP": {
"Enabled": true "Enabled": true
}, },
"LogAOP": { "LogAOP": {