mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-19 15:20:16 +08:00
a91befb5f4
2. 重构 ImageBorderRemover.cs,增强参数校验与异常处理,提升多层边框检测准确性,支持浅色背景自动识别,优化性能与颜色比较,移除灰度化处理,结构更清晰。 3. 增强HtmlDiff,新增 maxUnchangedLength 参数与辅助方法,优化 diff 合并逻辑,保持兼容性。
75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
using Masuit.Tools.Systems;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Newtonsoft.Json;
|
|
using System.Text;
|
|
using Masuit.Tools.Extension.Object;
|
|
|
|
namespace Masuit.Tools.AspNetCore.Extensions;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class DistributedCacheExt
|
|
{
|
|
public static T GetOrAdd<T>(this IDistributedCache cache, string key, Func<T> valueFactory)
|
|
{
|
|
var bytes = cache.Get(key);
|
|
if (bytes is null)
|
|
{
|
|
var value = new NullObject<T>(valueFactory());
|
|
bytes = Encoding.UTF8.GetBytes(value.ToJsonString());
|
|
cache.Set(key, bytes);
|
|
return value;
|
|
}
|
|
|
|
return JsonConvert.DeserializeObject<NullObject<T>>(Encoding.UTF8.GetString(bytes));
|
|
}
|
|
|
|
public static T Get<T>(this IDistributedCache cache, string key)
|
|
{
|
|
var bytes = cache.Get(key);
|
|
if (bytes is null)
|
|
{
|
|
return default;
|
|
}
|
|
|
|
return JsonConvert.DeserializeObject<NullObject<T>>(Encoding.UTF8.GetString(bytes));
|
|
}
|
|
|
|
public static void Set<T>(this IDistributedCache cache, string key, T value)
|
|
{
|
|
cache.Set(key, Encoding.UTF8.GetBytes(new NullObject<T>(value).ToJsonString()), new DistributedCacheEntryOptions());
|
|
}
|
|
|
|
public static void Set<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options)
|
|
{
|
|
cache.Set(key, Encoding.UTF8.GetBytes(new NullObject<T>(value).ToJsonString()), options);
|
|
}
|
|
|
|
public static T GetOrAdd<T>(this IDistributedCache cache, string key, T value)
|
|
{
|
|
var bytes = cache.Get(key);
|
|
if (bytes is null)
|
|
{
|
|
bytes = Encoding.UTF8.GetBytes(new NullObject<T>(value).ToJsonString());
|
|
cache.Set(key, bytes);
|
|
return value;
|
|
}
|
|
|
|
return JsonConvert.DeserializeObject<NullObject<T>>(Encoding.UTF8.GetString(bytes));
|
|
}
|
|
|
|
public static void AddOrUpdate<T>(this IDistributedCache cache, string key, T addValue, Func<T, T> updateFunc)
|
|
{
|
|
var bytes = cache.Get(key);
|
|
if (bytes is null)
|
|
{
|
|
bytes = Encoding.UTF8.GetBytes(new NullObject<T>(addValue).ToJsonString());
|
|
cache.Set(key, bytes);
|
|
return;
|
|
}
|
|
|
|
var value = new NullObject<T>(updateFunc(JsonConvert.DeserializeObject<NullObject<T>>(Encoding.UTF8.GetString(bytes))));
|
|
cache.Set(key, Encoding.UTF8.GetBytes(value.ToJsonString()));
|
|
}
|
|
} |