点击或拖拽改变大小

RedisClientPoolWriteAsyncT 方法

从设备里写入支持Hsl特性的数据内容, 该特性为HslRedisKeyAttributeHslRedisHashFieldAttribute 需要注意的是写入并不支持HslRedisListAttributeHslRedisListItemAttribute特性,详细参考代码示例的操作说明。

命名空间:  HslCommunication.Enthernet.Redis
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.1.2.0 (12.1.2.0)
语法
public Task<OperateResult> WriteAsync<T>(
	T data
)
where T : class, new()

参数

data
类型:T
等待写入的数据参数

类型参数

T
自定义的数据类型对象

返回值

类型:TaskOperateResult
包含是否成功的结果对象
示例
我们来说明下这个方法到底是怎么用的,当我们需要读取redis好几个数据的时候,我们很可能写如下的代码:
基础的使用
RedisClient redis = new RedisClient( "127.0.0.1", 6379, string.Empty );
redis.ConnectServer( );

// 上面是连接,此处不关心,只关心读数情况
// The above is the connection, here does not care, only care about the reading
OperateResult<string> readA = redis.ReadKey( "A" );
OperateResult<string> readB = redis.ReadKey( "B" );
OperateResult<string> readC = redis.ReadKey( "C" );
OperateResult<string> readD = redis.ReadKey( "D" );

if (readA.IsSuccess && readB.IsSuccess && readC.IsSuccess && readD.IsSuccess)
{
    // do somethong
}


// 当然你也可以这么干,加快读取的速度,并且代码更加简洁
// Of course, you can also do this, speed up the reading speed, and the code is more concise
OperateResult<string[]> readAD = redis.ReadKey( new string[] { "A", "B", "C", "D" } );
if (readAD.IsSuccess)
{
    // do something
    // A = readAD.Content[0]
    // B = readAD.Content[0]
    // C = readAD.Content[0]
    // D = readAD.Content[0]
}

// 当你需要读取加转换代码的时候,就没有那么方便了
// When you need to read plus conversion code, it is not so convenient
OperateResult<string[]> readAD2 = redis.ReadKey( new string[] { "A", "B", "C", "D" } );
if (readAD.IsSuccess)
{
    // 我们假设读取的所有数据需要转换int类型数据
    // We assume that all data read needs to be converted to int data
    int[] buffer = readAD2.Content.Select( m => int.Parse( m ) ).ToArray( );

    // do something
}

// 当你还要读取其他的信息的时候,就不得不多写点代码了
// When you need to read other information, you have to write more code
OperateResult<string> readListA = redis.ReadListByIndex( "List", 0 );
OperateResult<string> readHashA1 = redis.ReadHashKey( "HashA", "A1" );
OperateResult<string> readHashA2 = redis.ReadHashKey( "HashA", "A2" );
// ...
// and so on

redis.ConnectClose( );
总的来说,当读取的数据种类比较多的时候,读取的关键字比较多的时候,处理起来就比较的麻烦,此处推荐一个全新的写法,为了更好的对比,我们假设实现一种需求
同等代码
RedisClient redis = new RedisClient( "127.0.0.1", 6379, string.Empty );
redis.ConnectServer( );

// 上面是连接,此处不关心,只关心读数情况
// The above is the connection, here does not care, only care about the reading

// 我们来看看下面的两种等效写法,就可以发现此处的新写法是多么的便捷
// Let's take a look at the two equivalents below to see how convenient the new writing is here

// 假设下面是我们需要读取的,并且转化数据的,读取的数据在redis是真实存在的
// Suppose the following is what we need to read and transform the data. The read data is real in redis
OperateResult<string> readA = redis.ReadKey( "A" );
OperateResult<string> readB = redis.ReadKey( "B" );
OperateResult<string> readC = redis.ReadKey( "C" );
OperateResult<string> readD = redis.ReadKey( "D" );
OperateResult<string> readE = redis.ReadListByIndex( "E", 0 );
OperateResult<string[]> readF = redis.ListRange( "F", 0, -1 );
OperateResult<string[]> readG = redis.ReadHashKey( "G", new string[] { "G1", "G2", "G4" } );
OperateResult<string> readH = redis.ReadHashKey( "H", "H1" );

if (readA.IsSuccess && readB.IsSuccess && readC.IsSuccess && readD.IsSuccess &&
    readE.IsSuccess && readF.IsSuccess && readG.IsSuccess && readH.IsSuccess)
{
    // 进行相关的数据转换
    // Perform related data conversion
    string A = readA.Content;
    int B = int.Parse( readB.Content );
    ushort C = ushort.Parse( readC.Content );
    double D = double.Parse( readD.Content );
    float E = float.Parse( readE.Content );
    float[] F = readF.Content.Select( m => float.Parse( m ) ).ToArray( );
    int G1 = int.Parse( readG.Content[0] );
    int G2 = int.Parse( readG.Content[1] );
    int G3 = int.Parse( readG.Content[2] );
    string H = readH.Content;

    // do something
}

// 现在我们紧紧只需要一行代码即可完成任务,前提定义一个数据对象,还可以形成数据复用
// Now we need only one line of code to complete the task, provided that a data object is defined and data reuse can be formed
OperateResult<MyClass> read = redis.Read<MyClass>( );
if (read.IsSuccess)
{
    // do something
}

redis.ConnectClose( );
为此我们只需要实现一个特性类即可。代码如下:(注意,实际是很灵活的,类型都是自动转换的)
数据类
public class MyClass
{
    [HslCommunication.Reflection.HslRedisKey( "A" )]
    public string A { get; set; }

    [HslCommunication.Reflection.HslRedisKey( "B" )]
    public int B { get; set; }

    [HslCommunication.Reflection.HslRedisKey( "C" )]
    public ushort C { get; set; }

    [HslCommunication.Reflection.HslRedisKey( "D" )]
    public double D { get; set; }

    [HslCommunication.Reflection.HslRedisListItem( "E", 0 )]
    public float E { get; set; }

    [HslCommunication.Reflection.HslRedisList( "F", 0, -1 )]
    public float[] F { get; set; }

    [HslCommunication.Reflection.HslRedisHashField( "G", "G1" )]
    public int G1 { get; set; }

    [HslCommunication.Reflection.HslRedisHashField( "G", "G2" )]
    public int G2 { get; set; }

    [HslCommunication.Reflection.HslRedisHashField( "G", "G3" )]
    public int G3 { get; set; }

    [HslCommunication.Reflection.HslRedisHashField( "H", "H1" )]
    public string H { get; set; }
}
当然了,异步也是一样的,异步的代码就不重复介绍了。
异步示例
RedisClient redis = new RedisClient( "127.0.0.1", 6379, string.Empty );
await redis.ConnectServerAsync( );

// 上面是连接,此处不关心,只关心读数情况
// The above is the connection, here does not care, only care about the reading

// 我们来看看下面的两种等效写法,就可以发现此处的新写法是多么的便捷
// Let's take a look at the two equivalents below to see how convenient the new writing is here

// 假设下面是我们需要读取的,并且转化数据的,读取的数据在redis是真实存在的
// Suppose the following is what we need to read and transform the data. The read data is real in redis
OperateResult<string> readA = await redis.ReadKeyAsync( "A" );
OperateResult<string> readB = await redis.ReadKeyAsync( "B" );
OperateResult<string> readC = await redis.ReadKeyAsync( "C" );
OperateResult<string> readD = await redis.ReadKeyAsync( "D" );
OperateResult<string> readE = await redis.ReadListByIndexAsync( "E", 0 );
OperateResult<string[]> readF = await redis.ListRangeAsync( "F", 0, -1 );
OperateResult<string[]> readG = await redis.ReadHashKeyAsync( "G", new string[] { "G1", "G2", "G4" } );
OperateResult<string> readH = await redis.ReadHashKeyAsync( "H", "H1" );

if (readA.IsSuccess && readB.IsSuccess && readC.IsSuccess && readD.IsSuccess &&
    readE.IsSuccess && readF.IsSuccess && readG.IsSuccess && readH.IsSuccess)
{
    // 进行相关的数据转换
    // Perform related data conversion
    string A = readA.Content;
    int B = int.Parse( readB.Content );
    ushort C = ushort.Parse( readC.Content );
    double D = double.Parse( readD.Content );
    float E = float.Parse( readE.Content );
    float[] F = readF.Content.Select( m => float.Parse( m ) ).ToArray( );
    int G1 = int.Parse( readG.Content[0] );
    int G2 = int.Parse( readG.Content[1] );
    int G3 = int.Parse( readG.Content[2] );
    string H = readH.Content;

    // do something
}

// 现在我们紧紧只需要一行代码即可完成任务,前提定义一个数据对象,还可以形成数据复用
// Now we need only one line of code to complete the task, provided that a data object is defined and data reuse can be formed
OperateResult<MyClass> read = await redis.ReadAsync<MyClass>( );
if (read.IsSuccess)
{
    // do something
}

await redis.ConnectCloseAsync( );
参见