优化默认处理

This commit is contained in:
Kevin
2022-07-16 21:33:43 +08:00
parent bbf53d6b9f
commit e4698af7d0
11 changed files with 42 additions and 51 deletions
+16 -21
View File
@@ -38,11 +38,8 @@ namespace OSS.Tools.Cache
/// </summary>
/// <param name="sourceName"></param>
/// <returns></returns>
public static IToolCache GetCache(string sourceName= "default")
public static IToolCache GetCache(string sourceName= "")
{
if (string.IsNullOrEmpty(sourceName))
sourceName = "default";
if (SourceFormat != null)
sourceName = SourceFormat.Invoke(sourceName);
@@ -62,7 +59,7 @@ namespace OSS.Tools.Cache
/// <param name="slidingExpiration">滚动过期时长,访问后自动延长</param>
/// <param name="sourceName">来源名称</param>
/// <returns>是否添加成功</returns>
public static Task<bool> SetAsync<T>(string key, T obj, TimeSpan slidingExpiration, string sourceName = "default")
public static Task<bool> SetAsync<T>(string key, T obj, TimeSpan slidingExpiration, string sourceName = "")
{
return SetAsync(key, obj,new CacheTimeOptions(){sliding_expiration = slidingExpiration }, sourceName);
}
@@ -76,7 +73,7 @@ namespace OSS.Tools.Cache
/// <param name="absoluteExpiration"> 固定过期时长,设置后到时过期 </param>
/// <param name="sourceName">来源名称</param>
/// <returns>是否添加成功</returns>
public static Task<bool> SetAbsoluteAsync<T>(string key, T obj, TimeSpan absoluteExpiration, string sourceName = "default")
public static Task<bool> SetAbsoluteAsync<T>(string key, T obj, TimeSpan absoluteExpiration, string sourceName = "")
{
return SetAsync(key, obj, new CacheTimeOptions(){absolute_expiration_relative_to_now = absoluteExpiration}, sourceName);
}
@@ -90,7 +87,7 @@ namespace OSS.Tools.Cache
/// <param name="opt">缓存过期时间选项</param>
/// <param name="sourceName"></param>
/// <returns></returns>
public static Task<bool> SetAsync<T>(string key, T obj, CacheTimeOptions opt, string sourceName = "default")
public static Task<bool> SetAsync<T>(string key, T obj, CacheTimeOptions opt, string sourceName = "")
{
return GetCache(sourceName).SetAsync(key, obj, opt);
}
@@ -106,7 +103,7 @@ namespace OSS.Tools.Cache
/// <param name="key">key</param>
/// <param name="sourceName">来源名称</param>
/// <returns>获取指定key对应的值 </returns>
public static Task<T> GetAsync<T>(string key, string sourceName = "default")
public static Task<T> GetAsync<T>(string key, string sourceName = "")
{
return GetCache(sourceName).GetAsync<T>(key);
}
@@ -127,7 +124,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> GetOrSetAsync<RType>(string cacheKey, Func<Task<RType>> getFunc,
TimeSpan slidingExpiration, int hitProtectedSeconds = 10, string sourceName = "default")
TimeSpan slidingExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { sliding_expiration = slidingExpiration }, null, hitProtectedSeconds, sourceName);
}
@@ -145,7 +142,7 @@ namespace OSS.Tools.Cache
/// <returns></returns>
public static Task<RType> GetOrSetAsync<RType>(string cacheKey, Func<Task<RType>> getFunc,TimeSpan slidingExpiration,
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10,
string sourceName = "default")
string sourceName = "")
{
return GetOrSetAsync(cacheKey, getFunc,new CacheTimeOptions(){sliding_expiration = slidingExpiration}, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
@@ -161,7 +158,7 @@ namespace OSS.Tools.Cache
/// <param name="hitProtectedSeconds">缓存击穿保护秒数,默认值10。</param>
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> GetOrSetAbsoluteAsync<RType>(string cacheKey, Func<Task<RType>> getFunc, TimeSpan absoluteExpiration, int hitProtectedSeconds = 10, string sourceName = "default")
public static Task<RType> GetOrSetAbsoluteAsync<RType>(string cacheKey, Func<Task<RType>> getFunc, TimeSpan absoluteExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { absolute_expiration_relative_to_now = absoluteExpiration }, null
, hitProtectedSeconds, sourceName);
@@ -180,7 +177,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> GetOrSetAbsoluteAsync<RType>(string cacheKey, Func<Task<RType>> getFunc,TimeSpan absoluteExpiration,
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10,string sourceName = "default")
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10,string sourceName = "")
{
return GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions(){absolute_expiration_relative_to_now = absoluteExpiration}, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
@@ -199,7 +196,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static async Task<RType> GetOrSetAsync<RType>(string cacheKey, Func<Task<RType>> getFunc, CacheTimeOptions cacheTimeOpt,
Func<RType, bool> beforeSettingChecker = null, int hitProtectedSeconds = 10, string sourceName = "default")
Func<RType, bool> beforeSettingChecker = null, int hitProtectedSeconds = 10, string sourceName = "")
{
if (getFunc == null)
throw new ArgumentNullException("获取原始数据方法(getFunc)不能为空!");
@@ -251,7 +248,7 @@ namespace OSS.Tools.Cache
/// <param name="key"></param>
/// <param name="sourceName">来源名称</param>
/// <returns>是否成功</returns>
public static Task<bool> RemoveAsync(string key, string sourceName = "default")
public static Task<bool> RemoveAsync(string key, string sourceName = "")
{
return GetCache(sourceName).RemoveAsync(key);
}
@@ -262,13 +259,11 @@ namespace OSS.Tools.Cache
/// <param name="keys"></param>
/// <param name="sourceName">来源名称</param>
/// <returns>是否成功</returns>
public static Task<bool> RemoveAsync(string[] keys, string sourceName = "default")
public static Task<bool> RemoveAsync(string[] keys, string sourceName = "")
{
return GetCache(sourceName).RemoveAsync(keys);
}
#region 过时方法
/// <summary>
@@ -279,14 +274,14 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns>获取指定key对应的值 </returns>
[Obsolete("请使用 GetAsync")]
public static T Get<T>(string key, string sourceName = "default")
public static T Get<T>(string key, string sourceName = "")
{
return GetAsync<T>(key, sourceName).Result;
}
[Obsolete("请使用 RemoveAsync")]
public static bool Remove(string key, string sourceName = "default")
public static bool Remove(string key, string sourceName = "")
{
return RemoveAsync(key,sourceName).Result;
}
@@ -301,7 +296,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns>是否添加成功</returns>
[Obsolete("请使用 SetAsync")]
public static bool Set<T>(string key, T obj, TimeSpan slidingExpiration, string sourceName = "default")
public static bool Set<T>(string key, T obj, TimeSpan slidingExpiration, string sourceName = "")
{
return SetAsync(key, obj, slidingExpiration, sourceName).Result;
}
@@ -316,7 +311,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns>是否添加成功</returns>
[Obsolete("请使用 SetAbsoluteAsync")]
public static bool Set<T>(string key, T obj, DateTime absoluteExpiration, string sourceName = "default")
public static bool Set<T>(string key, T obj, DateTime absoluteExpiration, string sourceName = "")
{
return SetAbsoluteAsync(key, obj, TimeSpan.FromTicks((absoluteExpiration - DateTime.Now).Ticks), sourceName).Result;
}
@@ -21,7 +21,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> WithCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey,
TimeSpan slidingExpiration, int hitProtectedSeconds = 10, string sourceName = "default")
TimeSpan slidingExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { sliding_expiration = slidingExpiration }, null, hitProtectedSeconds, sourceName);
}
@@ -39,7 +39,7 @@ namespace OSS.Tools.Cache
/// <returns></returns>
public static Task<RType> WithCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey, TimeSpan slidingExpiration,
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10,
string sourceName = "default")
string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { sliding_expiration = slidingExpiration }, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
@@ -55,7 +55,7 @@ namespace OSS.Tools.Cache
/// <param name="hitProtectedSeconds">缓存击穿保护秒数,默认值10。</param>
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> WithAbsoluteCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey, TimeSpan absoluteExpiration, int hitProtectedSeconds = 10, string sourceName = "default")
public static Task<RType> WithAbsoluteCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey, TimeSpan absoluteExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { absolute_expiration_relative_to_now = absoluteExpiration }, null
, hitProtectedSeconds, sourceName);
@@ -74,7 +74,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> WithAbsoluteCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey, TimeSpan absoluteExpiration,
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10, string sourceName = "default")
Func<RType, bool> beforeSettingChecker, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc,
new CacheTimeOptions()
@@ -98,7 +98,7 @@ namespace OSS.Tools.Cache
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<RType> WithCacheAsync<RType>(this Func<Task<RType>> getFunc, string cacheKey, CacheTimeOptions cacheTimeOpt,
Func<RType, bool> beforeSettingChecker = null, int hitProtectedSeconds = 10, string sourceName = "default")
Func<RType, bool> beforeSettingChecker = null, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, cacheTimeOpt, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.2</Version>
<Version>2.2.1</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIconUrl>http://img1.osscore.cn/static/oss_net.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/KevinWG/OSS.Tools</PackageProjectUrl>
@@ -51,7 +51,7 @@ namespace OSS.Tools.DirConfig
private const string _folderName = "Configs";
/// <inheritdoc />
public Task<bool> SetDirConfig<TConfig>(string key, TConfig dirConfig, string sourceName) where TConfig : class, new()
public Task<bool> SetDirConfig<TConfig>(string key, TConfig dirConfig, string sourceName)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key), "配置键值不能为空!");
@@ -81,7 +81,7 @@ namespace OSS.Tools.DirConfig
/// <inheritdoc />
public Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName) where TConfig : class, new()
public Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName)
{
if (string.IsNullOrEmpty(key))
@@ -44,9 +44,6 @@ namespace OSS.Tools.DirConfig
/// <returns></returns>
private static IToolDirConfig GetDirConfig(string sourceName)
{
if (string.IsNullOrEmpty(sourceName))
sourceName = "default";
if (SourceFormat != null)
sourceName = SourceFormat.Invoke(sourceName);
@@ -63,7 +60,7 @@ namespace OSS.Tools.DirConfig
/// <typeparam name="TConfig"></typeparam>
/// <returns></returns>
public static Task<bool> SetDirConfig<TConfig>(string key, TConfig dirConfig,
string sourceName = "default") where TConfig : class, new()
string sourceName = "") where TConfig : class, new()
{
return GetDirConfig(sourceName).SetDirConfig(key, dirConfig,sourceName);
}
@@ -76,7 +73,7 @@ namespace OSS.Tools.DirConfig
/// <param name="key"></param>
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName = "default")
public static Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName = "")
where TConfig : class, new()
{
return GetDirConfig(sourceName).GetDirConfig<TConfig>(key, sourceName);
@@ -88,7 +85,7 @@ namespace OSS.Tools.DirConfig
/// <param name="key"></param>
/// <param name="sourceName">来源名称</param>
/// <returns></returns>
public static Task RemoveDirConfig(string key, string sourceName = "default")
public static Task RemoveDirConfig(string key, string sourceName = "")
{
return GetDirConfig(sourceName).RemoveDirConfig(key, sourceName);
}
+2 -2
View File
@@ -29,7 +29,7 @@ namespace OSS.Tools.DirConfig
/// <param name="sourceName">来源名称</param>
/// <typeparam name="TConfig">配置信息类型</typeparam>
/// <returns></returns>
Task<bool> SetDirConfig<TConfig>(string key, TConfig dirConfig, string sourceName) where TConfig : class ,new();
Task<bool> SetDirConfig<TConfig>(string key, TConfig dirConfig, string sourceName);
/// <summary>
@@ -39,7 +39,7 @@ namespace OSS.Tools.DirConfig
/// <param name="sourceName">来源名称</param>
/// <typeparam name="TConfig">配置信息类型</typeparam>
/// <returns></returns>
Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName) where TConfig : class ,new();
Task<TConfig> GetDirConfig<TConfig>(string key, string sourceName);
/// <summary>
/// 移除配置信息
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1</Version>
<Version>2.1.1</Version>
<PackageReleaseNotes>优化扩展</PackageReleaseNotes>
<PackageIconUrl>http://img1.osscore.cn/static/oss_net.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/KevinWG/OSS.Tools</PackageProjectUrl>
+4 -2
View File
@@ -38,8 +38,10 @@ namespace OSS.Tools.Log
private string getLogFilePath(string module, LogLevelEnum level)
{
var date = DateTime.Now;
var dirPath = Path.Combine(_logBaseDirPath, string.Concat(module, "_", level),
var date = DateTime.Now;
var moduleName = string.IsNullOrEmpty(module) ? "default" : module;
var dirPath = Path.Combine(_logBaseDirPath, string.Concat(moduleName, "_", level),
DateTime.Now.ToString("yyyyMM")); //string.Format(@"{0}\{1}\{2}\",_logBaseDirPath, module, level);
if (!Directory.Exists(dirPath))
+6 -9
View File
@@ -27,7 +27,7 @@ namespace OSS.Tools.Log
/// <summary>
/// 日志来源提供者
/// 日志工具提供者
/// </summary>
public static Func<string, IToolLog> LogToolProvider { get; set; }
@@ -44,9 +44,6 @@ namespace OSS.Tools.Log
/// <returns></returns>
public static IToolLog GetLogWrite(string logModule)
{
if (string.IsNullOrEmpty(logModule))
logModule = "default";
return LogToolProvider?.Invoke(logModule) ?? DefaultLogTool;
}
@@ -56,7 +53,7 @@ namespace OSS.Tools.Log
/// <param name="msg"> 日志信息 </param>
/// <param name="msgKey"> 关键值 </param>
/// <param name="sourceName"> 来源名称 </param>
public static string Info(object msg, string msgKey = null, string sourceName = "default")
public static string Info(object msg, string msgKey = null, string sourceName = "")
{
return Log(new LogInfo(LogLevelEnum.Info, msg, msgKey, sourceName));
}
@@ -67,7 +64,7 @@ namespace OSS.Tools.Log
/// <param name="msg"> 日志信息 </param>
/// <param name="msgKey"> 关键值 </param>
/// <param name="sourceName">来源名称</param>
public static string Warning(object msg, string msgKey = null, string sourceName = "default")
public static string Warning(object msg, string msgKey = null, string sourceName = "")
{
return Log(new LogInfo(LogLevelEnum.Warning, msg, msgKey, sourceName));
}
@@ -78,7 +75,7 @@ namespace OSS.Tools.Log
/// <param name="msg"> 日志信息 </param>
/// <param name="msgKey"> 关键值 </param>
/// <param name="sourceName">来源名称</param>
public static string Error(object msg, string msgKey = null, string sourceName = "default")
public static string Error(object msg, string msgKey = null, string sourceName = "")
{
return Log(new LogInfo(LogLevelEnum.Error, msg, msgKey, sourceName));
}
@@ -89,7 +86,7 @@ namespace OSS.Tools.Log
/// <param name="msg"> 日志信息 </param>
/// <param name="msgKey"> 关键值 </param>
/// <param name="sourceName">来源名称</param>
public static string Trace(object msg, string msgKey = null, string sourceName = "default")
public static string Trace(object msg, string msgKey = null, string sourceName = "")
{
return Log(new LogInfo(LogLevelEnum.Trace, msg, msgKey, sourceName));
}
@@ -103,7 +100,7 @@ namespace OSS.Tools.Log
try
{
if (string.IsNullOrEmpty(info.source_name))
info.source_name = "default";
info.source_name = "";
LogFormat?.Invoke(info);
GetLogWrite(info.source_name)?.WriteLogAsync(info);
+1 -1
View File
@@ -59,7 +59,7 @@ namespace OSS.Tools.Log
/// <param name="logMsg"></param>
/// <param name="msgKey"></param>
/// <param name="sourceName"></param>
internal LogInfo(LogLevelEnum loglevel, object logMsg, string msgKey = null, string sourceName = "default")
internal LogInfo(LogLevelEnum loglevel, object logMsg, string msgKey = null, string sourceName = "")
{
level = loglevel;
source_name = sourceName;
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.2.2</Version>
<Version>1.2.3</Version>
<PackageReleaseNotes>修改属性名称</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/KevinWG/OSS.Tools</PackageProjectUrl>
<RepositoryUrl>https://github.com/KevinWG/OSS.Tools</RepositoryUrl>