82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace DataTransferService.RedisHelp
|
|
{
|
|
public class DataHelper
|
|
{
|
|
#region 方法 -> 从Redis缓存中判断是否已存在数据
|
|
public static bool ContainData<T>(string tableName, string KeyValue)
|
|
{
|
|
//从Redis的db0数据库中获取信息
|
|
T InfoModel = RedisHelp.Redis.HashGet<T>(0, tableName, KeyValue);
|
|
if (InfoModel != null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
#region 方法 -> 将数据记录Redis缓存中
|
|
/// <summary>
|
|
/// 将数据记录Redis缓存中
|
|
/// </summary>
|
|
/// <typeparam name="T">泛型对象</typeparam>
|
|
/// <param name="listData">保存到redis数据中的list数据集</param>
|
|
/// <param name="tableName">数据表名称</param>
|
|
/// <param name="KeyName">数据键值</param>
|
|
/// <returns></returns>
|
|
public static bool RecordToRedis<T>(T ObjectModel, string tableName, string KeyName)
|
|
{
|
|
//获取item对象类型
|
|
var pros = typeof(T).GetProperties();
|
|
string KeyValue = string.Empty; //字段值
|
|
foreach (var item in pros)
|
|
{
|
|
if (item.Name == KeyName)
|
|
{
|
|
KeyValue = item.GetValue(ObjectModel, null).ToString();
|
|
break;
|
|
}
|
|
}
|
|
|
|
//写入Redis的db0数据库
|
|
return RedisHelp.Redis.HashSet(0, tableName, KeyValue, ObjectModel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将数据记录Redis缓存中
|
|
/// </summary>
|
|
/// <typeparam name="T">泛型对象</typeparam>
|
|
/// <param name="listData">保存到redis数据中的list数据集</param>
|
|
/// <param name="tableName">数据表名称</param>
|
|
/// <param name="KeyName">数据键值</param>
|
|
/// <returns></returns>
|
|
public static bool RecordToRedis<T>(List<T> listData, string tableName, string KeyName)
|
|
{
|
|
bool flag = false;
|
|
|
|
foreach (T model in listData)
|
|
{
|
|
//获取item对象类型
|
|
var pros = typeof(T).GetProperties();
|
|
string KeyValue = string.Empty; //字段值
|
|
foreach (var item in pros)
|
|
{
|
|
if (item.Name == KeyName)
|
|
{
|
|
KeyValue = item.GetValue(model, null).ToString();
|
|
break;
|
|
}
|
|
}
|
|
|
|
//写入Redis的db0数据库
|
|
flag = RedisHelp.Redis.HashSet(0, tableName, KeyValue, model);
|
|
}
|
|
|
|
return flag;
|
|
}
|
|
#endregion
|
|
}
|
|
} |