mirror of
https://github.com/KevinWG/OSS.Common.git
synced 2026-10-18 22:44:43 +08:00
优化插件模块
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using OSS.Common.Plugs.CachePlug;
|
||||
using OSS.Common.Plugs.LogPlug;
|
||||
|
||||
namespace OSS.Common.Tests
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
|
||||
<Version>1.2.6.0</Version>
|
||||
<Version>1.2.7</Version>
|
||||
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
|
||||
<Authors>Kevin</Authors>
|
||||
<Company>OSSCoder</Company>
|
||||
@@ -13,12 +13,10 @@
|
||||
<PackageIconUrl>http://img1.static.osscoder.com/logo.jpg</PackageIconUrl>
|
||||
<RepositoryUrl>https://github.com/KevinWG/OSS.Common</RepositoryUrl>
|
||||
|
||||
<PackageReleaseNotes>1. 优化HMAC未覆盖的算法
|
||||
2. 随机数算法添加启动seed
|
||||
3. 默认日志功能添加错误Code
|
||||
<PackageReleaseNotes>优化插件模块
|
||||
</PackageReleaseNotes>
|
||||
<AssemblyVersion>1.2.6.0</AssemblyVersion>
|
||||
<FileVersion>1.2.6.0</FileVersion>
|
||||
<AssemblyVersion>1.2.7.0</AssemblyVersion>
|
||||
<FileVersion>1.2.7.0</FileVersion>
|
||||
|
||||
<PackageLicenseUrl>https://github.com/KevinWG/OSS.Common/blob/master/LICENSE</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace OSS.Common.Plugs.CachePlug
|
||||
{
|
||||
@@ -21,7 +20,7 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
/// </summary>
|
||||
public static class CacheUtil
|
||||
{
|
||||
private static readonly DefaultCachePlug defaultCache= new DefaultCachePlug();
|
||||
private static readonly DefaultCachePlug defaultCache = new DefaultCachePlug();
|
||||
|
||||
/// <summary>
|
||||
/// 通过模块名称获取
|
||||
@@ -47,13 +46,46 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间 </param>
|
||||
/// <param name="moduleName">模块名称</param>
|
||||
/// <returns> 是否添加成功 </returns>
|
||||
public static bool AddOrUpdate<T>(string key, T obj, TimeSpan slidingExpiration, DateTime? absoluteExpiration = null,
|
||||
string moduleName = ModuleNames.Default)
|
||||
[Obsolete]
|
||||
public static bool AddOrUpdate<T>(string key, T obj, TimeSpan slidingExpiration,
|
||||
DateTime? absoluteExpiration = null,
|
||||
string moduleName = ModuleNames.Default)
|
||||
{
|
||||
return GetCache(moduleName).AddOrUpdate(key, obj, slidingExpiration, absoluteExpiration);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间段过期缓存
|
||||
/// 如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="slidingExpiration">相对过期的TimeSpan</param>
|
||||
/// <param name="moduleName">模块名称</param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
public static bool Set<T>(string key, T obj, TimeSpan slidingExpiration,
|
||||
string moduleName = ModuleNames.Default)
|
||||
{
|
||||
return GetCache(moduleName).Set(key, obj, slidingExpiration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加固定过期时间缓存,如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间,不为空则按照绝对过期时间计算 </param>
|
||||
/// <param name="moduleName">模块名称</param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
public static bool Set<T>(string key, T obj, DateTime absoluteExpiration,
|
||||
string moduleName = ModuleNames.Default)
|
||||
{
|
||||
return GetCache(moduleName).Set(key, obj, absoluteExpiration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存对象
|
||||
/// </summary>
|
||||
|
||||
@@ -29,7 +29,8 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
private static readonly MemoryCache m_Cache=new MemoryCache(new MemoryCacheOptions());
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 添加缓存,如果存在则更新
|
||||
/// 添加缓存
|
||||
/// 如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
@@ -37,11 +38,39 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
/// <param name="slidingExpiration">相对过期的TimeSpan 如果使用固定时间 =TimeSpan.Zero</param>
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间,不为空则按照绝对过期时间计算 </param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
[Obsolete]
|
||||
public bool AddOrUpdate<T>(string key, T obj, TimeSpan slidingExpiration, DateTime? absoluteExpiration = null)
|
||||
{
|
||||
return Add(key, obj, slidingExpiration, absoluteExpiration, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间段过期缓存
|
||||
/// 如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="slidingExpiration">相对过期的TimeSpan</param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
public bool Set<T>(string key, T obj, TimeSpan slidingExpiration)
|
||||
{
|
||||
return Add(key, obj, slidingExpiration, null, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加固定过期时间缓存,如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间,不为空则按照绝对过期时间计算 </param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
public bool Set<T>(string key, T obj, DateTime absoluteExpiration)
|
||||
{
|
||||
return Add(key, obj, TimeSpan.Zero, absoluteExpiration, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
@@ -52,7 +81,8 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
/// <param name="absoluteExpiration"></param>
|
||||
/// <param name="isUpdate"></param>
|
||||
/// <returns></returns>
|
||||
private static bool Add<T>(string key, T obj, TimeSpan slidingExpiration, DateTime? absoluteExpiration, bool isUpdate)
|
||||
private static bool Add<T>(string key, T obj, TimeSpan slidingExpiration, DateTime? absoluteExpiration,
|
||||
bool isUpdate)
|
||||
{
|
||||
if (slidingExpiration == TimeSpan.Zero && absoluteExpiration == null)
|
||||
throw new ArgumentNullException("slidingExpiration", "缓存过期时间不正确,需要设置固定过期时间或者相对过期时间");
|
||||
@@ -71,7 +101,7 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
return true;
|
||||
#else
|
||||
var opt = new MemoryCacheEntryOptions();
|
||||
if (absoluteExpiration != null)
|
||||
if (absoluteExpiration != null)
|
||||
opt.AbsoluteExpiration = new DateTimeOffset(absoluteExpiration.Value);
|
||||
else
|
||||
opt.SlidingExpiration = slidingExpiration;
|
||||
|
||||
@@ -29,8 +29,30 @@ namespace OSS.Common.Plugs.CachePlug
|
||||
/// <param name="slidingExpiration">相对过期的TimeSpan 如果使用固定时间 =TimeSpan.Zero</param>
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间,不为空则按照绝对过期时间计算 </param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
[Obsolete]
|
||||
bool AddOrUpdate<T>(string key, T obj, TimeSpan slidingExpiration, DateTime? absoluteExpiration = null);
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间段过期缓存
|
||||
/// 如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="slidingExpiration">相对过期的TimeSpan</param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
bool Set<T>(string key, T obj, TimeSpan slidingExpiration);
|
||||
|
||||
/// <summary>
|
||||
/// 添加固定过期时间缓存,如果存在则更新
|
||||
/// </summary>
|
||||
/// <typeparam name="T">添加缓存对象类型</typeparam>
|
||||
/// <param name="key">添加对象的key</param>
|
||||
/// <param name="obj">值</param>
|
||||
/// <param name="absoluteExpiration"> 绝对过期时间,不为空则按照绝对过期时间计算 </param>
|
||||
/// <returns>是否添加成功</returns>
|
||||
bool Set<T>(string key, T obj, DateTime absoluteExpiration);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存对象
|
||||
/// </summary>
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace OSS.Common.Plugs.DirConfigPlug
|
||||
|
||||
var type = typeof(TConfig);
|
||||
|
||||
XmlSerializer xmlSer = new XmlSerializer(type);
|
||||
var xmlSer = new XmlSerializer(type);
|
||||
xmlSer.Serialize(fs, dirConfig);
|
||||
|
||||
result = new ResultMo();
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace OSS.Common.Plugs.LogPlug
|
||||
return string.Concat(dirPath, DateTime.Now.ToString("yyyyMMddHH"), ".txt");
|
||||
}
|
||||
|
||||
private object obj = new object();
|
||||
private readonly object obj = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 写日志
|
||||
@@ -61,7 +61,7 @@ namespace OSS.Common.Plugs.LogPlug
|
||||
{
|
||||
lock (obj)
|
||||
{
|
||||
string filePath = getLogFilePath(info.ModuleName, info.Level);
|
||||
var filePath = getLogFilePath(info.ModuleName, info.Level);
|
||||
#if NETFW
|
||||
using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
|
||||
#else
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#endregion
|
||||
|
||||
namespace OSS.Common.Plugs
|
||||
{
|
||||
/// <summary>
|
||||
@@ -25,24 +26,29 @@ namespace OSS.Common.Plugs
|
||||
public const string Default = "default";
|
||||
|
||||
/// <summary>
|
||||
/// 支付模块
|
||||
/// 支付中心模块
|
||||
/// </summary>
|
||||
public const string PayCenter = "pay_center";
|
||||
|
||||
/// <summary>
|
||||
/// 推送中心模块
|
||||
/// </summary>
|
||||
public const string PushCenter = "push_center";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 社会化中心
|
||||
/// </summary>
|
||||
public const string SocialCenter = "social_center";
|
||||
|
||||
/// <summary>
|
||||
/// 多媒体中心模块
|
||||
/// 用户中心模块
|
||||
/// </summary>
|
||||
public const string MediaStorage = "media_storage";
|
||||
public const string User = "user";
|
||||
|
||||
/// <summary>
|
||||
/// 订单模块
|
||||
/// </summary>
|
||||
public const string Order = "order";
|
||||
|
||||
/// <summary>
|
||||
/// 产品模块
|
||||
/// </summary>
|
||||
public const string Product = "order";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,57 +1,73 @@
|
||||
#OS.Common
|
||||
通用基础实现,提供主要有以下的内容:
|
||||
1. 基础用户系统设备信息实体定义。
|
||||
2. 主流加解密方案实现()
|
||||
3. 常规实体DTO转化,静态扩展方法(时间,字符串等等处理)
|
||||
4. 基础日志,缓存,配置辅助静态类及默认方案实现
|
||||
5. 全局结果,分页实体定义
|
||||
# OS.Common
|
||||
通用基础实现,当前是以标准库的形式提供,在net46分支下有标准库和Framework 4.6的两个版本实现
|
||||
当前类库主要提供以下的内容:
|
||||
|
||||
###Authrization
|
||||
用户授权信息模块,可以在上下文中使用MemberShiper,其中主要包含两个属性:
|
||||
1. AppAuthorize
|
||||
对应的是应用授权信息主要是应用来源,客户端的类型等
|
||||
2. MemberInfo
|
||||
对应的是当前的用户信息,用户名称 等
|
||||
MemberShiper 中提供了GetToken方法,方便加密用户Id,同时有一个对应的GetTokenDetail来从token中解密用户id信息
|
||||
使用的是加密方式为Aes加密
|
||||
1. 基础用户系统设备信息实体定义。
|
||||
2. 主流加解密方案实现()
|
||||
3. 常规实体DTO转化,静态扩展方法(时间,字符串等等处理)
|
||||
4. 基础日志,缓存,配置辅助静态类及默认方案实现
|
||||
5. 全局结果,分页实体定义
|
||||
|
||||
使用方法:可以在请求中添加中间件或者filter,来对其进行赋值,然后可以在具体的业务中进行调用。
|
||||
需要注意的是以上两个变量皆为线程变量,如果你使用的异步线程,请注意传值给对应的线程。
|
||||
## Authrization
|
||||
用户授权信息模块,可以在上下文中使用MemberShiper,其中主要包含两个属性:
|
||||
1. AppAuthorize
|
||||
对应的是应用授权信息主要是应用来源,客户端的类型等
|
||||
2. Identity
|
||||
对应的是当前的用户信息,用户名称 等
|
||||
MemberShiper 中提供了GetToken方法,方便加密用户Id,同时有一个对应的GetTokenDetail来从token中解密用户id信息
|
||||
使用的是加密方式为Aes加密
|
||||
|
||||
###ComModels
|
||||
系统默认实体信息,分页实体 , 结果实体,方便全局使用,
|
||||
其中结果实体ResultMo包含三个属性:
|
||||
Ret-状态码,在ResultTypes的枚举中已经把常见的状态信息做了简单定义,这些状态码和Http状态码基本相对应,
|
||||
|
||||
## ComModels
|
||||
系统默认实体信息,分页实体 , 结果实体,方便全局使用,
|
||||
其中结果实体ResultMo包含三个属性:
|
||||
ret-状态码,在ResultTypes的枚举中已经把常见的状态信息做了简单定义,这些状态码和Http状态码基本相对应,
|
||||
其中0表示为Success,同时定义了个IsSuccess()的方法扩展,此值为快速判断返回是否正确。
|
||||
msg-对应状态码的消息
|
||||
|
||||
Message-对应状态码的消息
|
||||
IsSuccess-快速判断状态码是否正确,默认0时正确
|
||||
## Encrypt
|
||||
系统加密基础库
|
||||
主要包含:md5(Md5),aes(AesRijndael),sha1(Sha1),hmacsha1(HmacSha1-加盐sha1加密方式这几种加密算法
|
||||
|
||||
###Encrypt
|
||||
系统加密基础库
|
||||
主要包含:md5(Md5),aes(AesRijndael),sha1(Sha1),hmacsha1(HmacSha1-加盐sha1加密方式这几种加密算法
|
||||
## Extention
|
||||
系统扩展方法,主要包含:
|
||||
字符串转化扩展,如: "0".ToInt32(),"xxx".Base64UrlEncode()等
|
||||
时间转化扩展,如:DateTime.Now.ToUtcSeconds() 等
|
||||
Task扩展方法,如: Task.WaitResult() 等
|
||||
UrlCode扩展方法,如 "name=n&code=1".UrlEncode();
|
||||
枚举扩展方法,如: typeof(Enum).ToEnumDirs();
|
||||
xml序列化扩展方法, 如: "<xml><name>test</name></xml>".DeserializeXml<Type>();
|
||||
|
||||
###Extention
|
||||
系统扩展方法,主要包含:
|
||||
字符串转化扩展,如: "0".ToInt32(),"xxx".Base64UrlEncode()等
|
||||
时间转化扩展,如:DateTime.Now.ToUtcSeconds() 等
|
||||
Task扩展方法,如: Task.WaitResult() 等
|
||||
UrlCode扩展方法,如 "name=n&code=1".UrlEncode();
|
||||
枚举扩展方法,如: typeof(Enum).ToEnumDirs();
|
||||
xml序列化扩展方法, 如: "<xml><name>test</name></xml>".DeserializeXml<Type>();
|
||||
## Plugs
|
||||
系统扩展模块和对应的默认实现,同时在Plugs目录下定义了ModuleNames类,含有系统常见模块。
|
||||
如果你有高级定制需求,比如在系统不同模块使用插件的不同实现,可以在程序入口处定义指定模块名称对应的插件实现,而无需修改业务代码
|
||||
|
||||
这里主要包含以下三种插件
|
||||
|
||||
### 一.CachePlug
|
||||
缓存模块,内部定义了ICachePlug接口,和一个 DefaultCachePlug默认缓存实现,
|
||||
使用时,可以直接使用 CacheUtil 静态类下的方法,比如Set方法
|
||||
|
||||
还有自定义验证属性像 OSRequiredAttribute等,可以使用IsOsValidate来验证是否验证通过。
|
||||
CacheUtil.Set("key","va",TimeSpan.FromHours(1),"moduleName")
|
||||
|
||||
###Modules
|
||||
系统扩展模块和对应的默认实现,其中主要有下边三个部分:
|
||||
其中 modulename 是可选参数
|
||||
如果传入,则在程序入口处可以给 OsConfig.CacheProvider(Func<string, ICachePlug>) 委托赋值
|
||||
我们就可以根据使用频率,数据重要性,以及和其它系统的依赖情况来定义特定模块下的不同缓存实现
|
||||
比如设置不同的模块分别Redis,Memcache等,又或者我们可以设置不同的模块使用Redis的不同实例。
|
||||
|
||||
当然,如果这个委托没有定义,或者委托返回空,就会使用默认缓存实现,如果我们希望全站换到相同的缓存实现,则直接返回对应实现即可。
|
||||
|
||||
####一.CacheModule
|
||||
缓存模块
|
||||
|
||||
####二.DirConfig
|
||||
字典配置模块
|
||||
|
||||
####三.LogModule
|
||||
日志模块
|
||||
### 二.DirConfigPlug
|
||||
配置模块, 和缓存的实现形式相同,定义了一个 IDirConfigPlug 接口,和一个 DefaultDirConfigPlug 实现
|
||||
使用时,直接使用 DirConfigUtil 静态类。
|
||||
默认实现是利用Xml序列化的方式,保存到本地文件中,依然可以定义指定模块名称的特定数据源的配置实例。
|
||||
比如项目本身的配置可以设置数据库实现,依赖其他项目的,可以通过接口实现。
|
||||
|
||||
|
||||
### 三.LogPlug
|
||||
日志模块,一样,内部定义了一个 ILogPlug 接口,和一个 DefaultLogPlug 默认实现
|
||||
使用时,直接使用 LogUtil 静态类。
|
||||
默认实现是通过保存到本地文件的形式实现,每个小时生成一个文件。
|
||||
依然可以定义指定模块名称的特定数据源的配置实例,比如有些发送邮件,有些发送短信通知等。
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user