RedisRepository 分享和纠错(5)

using Fantasy.RedisRepository.CommonHelper; using StackExchange.Redis; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { /// <summary> /// Redis异步操作类 Hash部分类 /// </summary> internal partial class RedisHelper { #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></param> /// <param></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 } }

RedisLuaHelper.cs 这里打算装一些功能行lua脚本, 外部依然是传key一类的参数,这个不完整,只是个实例。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdppwx.html