This commit is contained in:
2024-05-21 09:26:29 +08:00
24 changed files with 471 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ using JNPF;
using JNPF.Common.Cache;
using JNPF.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace Tnb.Common.Redis
{
@@ -243,5 +244,25 @@ namespace Tnb.Common.Redis
{
return _instance.HSetAsync(key, field, value);
}
public async Task<T> TryGetValueByKeyField<T>(string key, string field)
{
if (await _instance.HExistsAsync(key, field))
{
string data = await GetHash(key, field);
Dictionary<String, String> json = JsonConvert.DeserializeObject<Dictionary<String, String>>(data);
Type type = typeof(T);
if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
return (T)Convert.ChangeType(json["Value"],type.GetGenericArguments()[0]);
}
else
{
return (T)Convert.ChangeType(json["Value"],type);
}
// return ;
}
return default(T);
}
}
}