Merge branch 'pr_3' into sqlsugar-dev

This commit is contained in:
橙子
2023-01-07 19:42:13 +08:00
12 changed files with 348 additions and 6 deletions

View File

@@ -35,7 +35,7 @@
"DbConn": {
"WriteUrl": "DataSource=yi-sqlsugar-dev.db",
"ReadUrl": [
"DataSource=[xxxx]",//sqlite
"DataSource=[xxxx]", //sqlite
"server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //mysql
"Data Source=[xxxx];Initial Catalog=[xxxx];User ID=[xxxx];password=[xxxx]" //sqlserver
]
@@ -47,6 +47,11 @@
"CacheList": [ "Redis", "MemoryCache" ],
//选择缓存
"CacheSelect": "MemoryCache",
//缓存Aop
"CacheAOP_Enabled": false,
//缓存种子数据是否开启
"CacheSeed_Enabled": false,

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Common.Attribute
{
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class CachingAttribute : System.Attribute
{
/// <summary>
/// 缓存绝对过期时间(分钟)
/// </summary>
public int AbsoluteExpiration { get; set; } = 30;
}
}

View File

@@ -110,6 +110,11 @@ namespace Yi.Framework.Common.Base
}
public static string TryStringNull(this object value)
{
return value == null ? "" : value.ToString()!.Trim();
}
/// <summary>
/// Object类型无值判断
/// </summary>

View File

@@ -47,6 +47,63 @@ namespace Yi.Framework.Common.Helper
return ConvertEx.ToUrlBase64String(bRet);
}
/// <summary>
/// 16位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt16(string password)
{
var md5 = MD5.Create();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
t2 = t2.Replace("-", string.Empty);
return t2;
}
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt32(string password = "")
{
string pwd = string.Empty;
try
{
if (!string.IsNullOrEmpty(password) && !string.IsNullOrWhiteSpace(password))
{
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
foreach (var item in s)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母如果使用大写X则格式后的字符是大写字符
pwd = string.Concat(pwd, item.ToString("X2"));
}
}
}
catch
{
throw new Exception($"错误的 password 字符串:【{password}】");
}
return pwd;
}
/// <summary>
/// 64位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt64(string password)
{
// 实例化一个md5对像
// 加密后是一个字节类型的数组这里要注意编码UTF8/Unicode等的选择 
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
return Convert.ToBase64String(s);
}
}
public class ConvertEx
{

View File

@@ -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;
}
}
}

View File

@@ -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();//直接执行被拦截方法
}
}
}
}

View File

@@ -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();//直接执行被拦截方法
}
}
}
}

View File

@@ -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" />

View File

@@ -15,7 +15,7 @@ namespace Yi.Framework.Model.RABC.Entitys
{
public ConfigEntity()
{
CreateTime = DateTime.Now;
//CreateTime = DateTime.Now;
}
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]

View File

@@ -1,6 +1,7 @@
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using Yi.Framework.Common.Attribute;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.Interface.RABC;
@@ -15,6 +16,8 @@ namespace Yi.Framework.Service.RABC
public ConfigService(IRepository<ConfigEntity> repository) : base(repository)
{
}
[Caching(AbsoluteExpiration = 10)]
public async Task<PageModel<List<ConfigEntity>>> SelctPageList(ConfigEntity config, PageParModel page)
{
RefAsync<int> total = 0;

View File

@@ -92,6 +92,10 @@ namespace Yi.Framework.WebCore.AspNetCoreExtensions
{
//entityInfo.SetValue(new Guid(httpcontext.Request.Headers["TenantId"].ToString()));
}
if (entityInfo.PropertyName == "CreateTime")
{
entityInfo.SetValue(DateTime.Now);
}
break;
case DataFilterType.UpdateByObject:
if (entityInfo.PropertyName == "ModifyTime")

View File

@@ -12,11 +12,13 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Yi.Framework.Common.Abstract;
using Yi.Framework.Core.Cache.Aop;
using Yi.Framework.Interface;
using Yi.Framework.Job;
using Yi.Framework.Repository;
using Yi.Framework.Service;
using Yi.Framework.WebCore.AutoFacExtend;
using Yi.Framework.WebCore.CommonExtend;
using Yi.Framework.WebCore.Impl;
using Module = Autofac.Module;
@@ -43,14 +45,36 @@ namespace Yi.Framework.WebCore.AutoFacExtend
containerBuilder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
var cacheType = new List<Type>();
//containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope();
///反射注入服务层及接口层
var assemblysServices = GetDll("Yi.Framework.Service.dll");
containerBuilder.RegisterAssemblyTypes(assemblysServices).PropertiesAutowired(new AutowiredPropertySelector())
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors();
var regContainerBuilder = containerBuilder.RegisterAssemblyTypes(assemblysServices).PropertiesAutowired(new AutowiredPropertySelector())
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors();
if (Appsettings.appBool("CacheAOP_Enabled"))
{
var cacheSelect = Appsettings.app("CacheSelect");
switch (cacheSelect)
{
case "Redis":
containerBuilder.RegisterType<RedisCacheAOP>();
cacheType.Add(typeof(RedisCacheAOP));
break;
case "MemoryCache":
containerBuilder.RegisterType<MemoryCacheAOP>();
cacheType.Add(typeof(MemoryCacheAOP));
break;
default: throw new ArgumentException("CacheSelect配置填的是什么东西俺不认得");
}
regContainerBuilder.InterceptedBy(cacheType.ToArray());
}
//开启工作单元拦截
//.InterceptedBy(typeof(UnitOfWorkInterceptor));