mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-17 23:16:43 +08:00
更改使用
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using Castle.DynamicProxy;
|
||||
using Newtonsoft.Json;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Base;
|
||||
using Yi.Framework.Common.Helper;
|
||||
|
||||
namespace Yi.Framework.Core.Cache.Aop
|
||||
{
|
||||
public abstract class CacheAOPbase : IInterceptor
|
||||
{
|
||||
/// <summary>
|
||||
/// AOP的拦截方法
|
||||
/// </summary>
|
||||
/// <param name="invocation"></param>
|
||||
public abstract void Intercept(IInvocation invocation);
|
||||
|
||||
/// <summary>
|
||||
/// 自定义缓存的key
|
||||
/// </summary>
|
||||
/// <param name="invocation"></param>
|
||||
/// <returns></returns>
|
||||
protected string CustomCacheKey(IInvocation invocation)
|
||||
{
|
||||
var typeName = invocation.TargetType.Name;
|
||||
var methodName = invocation.Method.Name;
|
||||
var methodArguments = invocation.Arguments.Select(GetArgumentValue).Take(3).ToList();//获取参数列表,最多三个
|
||||
|
||||
string key = $"{typeName}:{methodName}:";
|
||||
foreach (var param in methodArguments)
|
||||
{
|
||||
key = $"{key}{param}:";
|
||||
}
|
||||
|
||||
return key.TrimEnd(':');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// object 转 string
|
||||
/// </summary>
|
||||
/// <param name="arg"></param>
|
||||
/// <returns></returns>
|
||||
protected static string GetArgumentValue(object arg)
|
||||
{
|
||||
if (arg is DateTime)
|
||||
return ((DateTime)arg).ToString("yyyyMMddHHmmss");
|
||||
|
||||
if (!arg.IsNotNull())
|
||||
return arg.TryStringNull();
|
||||
|
||||
if (arg != null)
|
||||
{
|
||||
if (arg is Expression)
|
||||
{
|
||||
var obj = arg as Expression;
|
||||
var result = Resolve(obj);
|
||||
return MD5Helper.MD5Encrypt16(result);
|
||||
}
|
||||
else if (arg.GetType().IsClass)
|
||||
{
|
||||
return MD5Helper.MD5Encrypt16(JsonConvert.SerializeObject(arg));
|
||||
}
|
||||
|
||||
return $"value:{arg.TryStringNull()}";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static string Resolve(Expression expression)
|
||||
{
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(expression, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
|
||||
pars.ForEach(s =>
|
||||
{
|
||||
value = value.Replace(s.ParameterName, s.Value.TryStringNull());
|
||||
});
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Castle.DynamicProxy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Core.Cache;
|
||||
|
||||
namespace Yi.Framework.Core.Cache.Aop
|
||||
{
|
||||
public class MemoryCacheAOP : CacheAOPbase
|
||||
{
|
||||
private CacheInvoker _cache;
|
||||
public MemoryCacheAOP(CacheInvoker cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public override void Intercept(IInvocation invocation)
|
||||
{
|
||||
var method = invocation.MethodInvocationTarget ?? invocation.Method;
|
||||
|
||||
var cachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute));
|
||||
if (cachingAttribute is CachingAttribute qCachingAttribute)
|
||||
{
|
||||
//获取自定义缓存键
|
||||
var cacheKey = CustomCacheKey(invocation);
|
||||
//根据key获取相应的缓存值
|
||||
var cacheValue = _cache.Get<string>(cacheKey);
|
||||
if (cacheValue != null)
|
||||
{
|
||||
//将当前获取到的缓存值,赋值给当前执行方法
|
||||
invocation.ReturnValue = cacheValue;
|
||||
return;
|
||||
}
|
||||
//去执行当前的方法
|
||||
invocation.Proceed();
|
||||
//存入缓存
|
||||
if (!string.IsNullOrWhiteSpace(cacheKey))
|
||||
{
|
||||
_cache.Set(cacheKey, invocation.ReturnValue, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
invocation.Proceed();//直接执行被拦截方法
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using Castle.DynamicProxy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Cache;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
|
||||
namespace Yi.Framework.Core.Cache.Aop
|
||||
{
|
||||
public class RedisCacheAOP : CacheAOPbase
|
||||
{
|
||||
private CacheInvoker _cacheDb;
|
||||
public RedisCacheAOP(CacheInvoker cacheInvoker)
|
||||
{
|
||||
_cacheDb = cacheInvoker;
|
||||
}
|
||||
|
||||
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 = _cacheDb.Get<string>(cacheKey);
|
||||
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;
|
||||
|
||||
_cacheDb.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
invocation.Proceed();//直接执行被拦截方法
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.8" />
|
||||
<PackageReference Include="Castle.Core" Version="5.1.1" />
|
||||
<PackageReference Include="Consul" Version="1.6.10.3" />
|
||||
<PackageReference Include="CSRedisCore" Version="3.6.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
|
||||
|
||||
Reference in New Issue
Block a user