bug处理,二楼空载具料架入库
This commit is contained in:
88
common/Tnb.Common/Redis/StackExRedisHelper.cs
Normal file
88
common/Tnb.Common/Redis/StackExRedisHelper.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
using JNPF;
|
||||
using JNPF.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Spire.Xls.Core;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tnb.Common.Redis
|
||||
{
|
||||
public class StackExRedisHelper : ISingleton
|
||||
{
|
||||
private ConnectionMultiplexer _redis;
|
||||
private IDatabase _db;
|
||||
|
||||
// 初始化 Redis 连接
|
||||
public StackExRedisHelper()
|
||||
{
|
||||
RedisOptions _RedisOptions = App.GetConfig<RedisOptions>("Redis", true);
|
||||
_redis = ConnectionMultiplexer.Connect($"{_RedisOptions.ip}:{_RedisOptions.port},password={_RedisOptions.password}");
|
||||
_db = _redis.GetDatabase();
|
||||
}
|
||||
|
||||
// 存储字符串值
|
||||
public void SetString(string key, string value)
|
||||
{
|
||||
_db.StringSet(key, value);
|
||||
}
|
||||
|
||||
// 获取字符串值
|
||||
public string GetString(string key)
|
||||
{
|
||||
return _db.StringGet(key);
|
||||
}
|
||||
|
||||
// 存储哈希值
|
||||
public void SetHash(string key, string field, string value)
|
||||
{
|
||||
_db.HashSet(key, field, value);
|
||||
}
|
||||
|
||||
// 获取哈希值
|
||||
public string GetHash(string key, string field)
|
||||
{
|
||||
return _db.HashGet(key, field);
|
||||
}
|
||||
|
||||
// 存储列表值
|
||||
public void ListRightPush(string key, string value)
|
||||
{
|
||||
_db.ListRightPush(key, value);
|
||||
}
|
||||
|
||||
// 关闭 Redis 连接
|
||||
public void Close()
|
||||
{
|
||||
if (_redis != null && _redis.IsConnected)
|
||||
{
|
||||
_redis.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> TryGetValueByKeyField<T>(string key, string field)
|
||||
{
|
||||
if (_db.HashExists(key, field))
|
||||
{
|
||||
string data = GetHash(key, field);
|
||||
Dictionary<String, String> json = JsonConvert.DeserializeObject<Dictionary<String, String>>(data);
|
||||
Type type = typeof(T);
|
||||
if ((!type.IsValueType && type.GetGenericArguments().Length > 0) || (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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user