12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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
- {
- /// <summary>
- /// Redis异步操作类 Hash部分类
- /// </summary>
- internal partial class RedisHelper //: IRedisHelper
- {
- #region Hash 写操作
- public async Task<bool> HashSetAsync<T>(string key, string field, T value)
- {
- return await _client.HashSetAsync(key, field, SerializeHelper.Serialize(value));
- }
- public async Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields)
- {
- List<HashEntry> entries = new List<HashEntry>();
- hashFields.ToList().ForEach(d => entries.Add(new HashEntry(d.Key, SerializeHelper.Serialize(d.Value))));
- await _client.HashSetAsync(key, entries.ToArray());
- }
- public async Task<long> HashIncrementAsync(string key, string field, long incrCount = 1)
- {
- return await _client.HashIncrementAsync(key, field, incrCount);
- }
- public async Task<long> HashDecrementAsync(string key, string field, long decrCount = 1)
- {
- return await _client.HashDecrementAsync(key, field, decrCount);
- }
- public async Task<bool> HashDeleteFieldAsync(string key, string field)
- {
- return await _client.HashDeleteAsync(key, field);
- }
- public async Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields)
- {
- List<RedisValue> values = new List<RedisValue>();
- fields.ForEach(f => values.Add(f));
- return await _client.HashDeleteAsync(key, values.ToArray());
- }
- #endregion
- #region Hash 读操作
- /// <summary>
- /// Redis 指定hash类型key中field是否存在
- /// </summary>
- /// <param name="key"></param>
- /// <param name="field"></param>
- /// <returns></returns>
- public async Task<bool> HashFieldExistAsync(string key, string field)
- {
- return await _client.HashExistsAsync(key, field, CommandFlags.PreferSlave);
- }
- public async Task<List<string>> HashFieldsAsync(string key)
- {
- RedisValue[] values = await _client.HashKeysAsync(key, CommandFlags.PreferSlave);
- return RedisInnerTypeHelper.RedisValuesToGenericList<string>(values);
- }
- public async Task<List<T>> HashValuesAsync<T>(string key)
- {
- var values = await _client.HashValuesAsync(key, CommandFlags.PreferSlave);
- return RedisInnerTypeHelper.RedisValuesToGenericList<T>(values);
- }
- public async Task<T> HashGetAsync<T>(string key, string field)
- {
- return SerializeHelper.Deserialize<T>(await _client.HashGetAsync(key, field, CommandFlags.PreferSlave));
- }
- public async Task<Dictionary<string, T>> HashGetAllAsync<T>(string key)
- {
- HashEntry[] entries = await _client.HashGetAllAsync(key, CommandFlags.PreferSlave);
- Dictionary<string, T> dic = new Dictionary<string, T>();
- entries.ToList().ForEach(e => dic.Add(e.Name, SerializeHelper.Deserialize<T>(e.Value)));
- return dic;
- }
- #endregion
- }
- }
|