using OASystem.RedisRepository.CommonHelper;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.RedisRepository.RedisAsyncHelper
{
///
/// Redis异步操作类 Hash部分类
///
internal partial class RedisHelper //: IRedisHelper
{
#region Hash 写操作
public async Task HashSetAsync(string key, string field, T value)
{
return await _client.HashSetAsync(key, field, SerializeHelper.Serialize(value));
}
public async Task HashMultiSetAsync(string key, Dictionary hashFields)
{
List entries = new List();
hashFields.ToList().ForEach(d => entries.Add(new HashEntry(d.Key, SerializeHelper.Serialize(d.Value))));
await _client.HashSetAsync(key, entries.ToArray());
}
public async Task HashIncrementAsync(string key, string field, long incrCount = 1)
{
return await _client.HashIncrementAsync(key, field, incrCount);
}
public async Task HashDecrementAsync(string key, string field, long decrCount = 1)
{
return await _client.HashDecrementAsync(key, field, decrCount);
}
public async Task HashDeleteFieldAsync(string key, string field)
{
return await _client.HashDeleteAsync(key, field);
}
public async Task HashMultiDeleteFieldAsync(string key, List fields)
{
List values = new List();
fields.ForEach(f => values.Add(f));
return await _client.HashDeleteAsync(key, values.ToArray());
}
#endregion
#region Hash 读操作
///
/// Redis 指定hash类型key中field是否存在
///
///
///
///
public async Task HashFieldExistAsync(string key, string field)
{
return await _client.HashExistsAsync(key, field, CommandFlags.PreferSlave);
}
public async Task> HashFieldsAsync(string key)
{
RedisValue[] values = await _client.HashKeysAsync(key, CommandFlags.PreferSlave);
return RedisInnerTypeHelper.RedisValuesToGenericList(values);
}
public async Task> HashValuesAsync(string key)
{
var values = await _client.HashValuesAsync(key, CommandFlags.PreferSlave);
return RedisInnerTypeHelper.RedisValuesToGenericList(values);
}
public async Task HashGetAsync(string key, string field)
{
return SerializeHelper.Deserialize(await _client.HashGetAsync(key, field, CommandFlags.PreferSlave));
}
public async Task> HashGetAllAsync(string key)
{
HashEntry[] entries = await _client.HashGetAllAsync(key, CommandFlags.PreferSlave);
Dictionary dic = new Dictionary();
entries.ToList().ForEach(e => dic.Add(e.Name, SerializeHelper.Deserialize(e.Value)));
return dic;
}
#endregion
}
}