1. 이전에 쓴 글
졸업하고 일을 마치고 드디어 오늘 집에 가보니 지난 6개월간 했던 컨텐츠들을 되돌아보는데 늘 그렇구나 하는 생각이 듭니다. 아직은 기초적인 단계이고, 다양한 문제를 해결하는 과정에서 혁신적인 아이디어가 줄어들고 검색에 더 의존하게 됩니다. 2016년을 요약하고 싶지는 않습니다. 2017년에는 제가 좋아하는 기술을 더 많이 배우고, 더 많은 오픈 소스 코드를 읽고, 더 많은 디자인 아이디어와 코딩 아이디어를 배우고, 지난 2년 동안의 코드에 대한 이해를 업데이트할 수 있기를 바랍니다. .
이 공유는 주로 이전 RedisRepository의 단점을 보완하기 위한 것입니다.
반년 전 StackExchange.Redis 문서를 제대로 읽지 못한 탓에 제가 공유한 RedisRepository가 잘못되었습니다. 다음은 제가 저지른 주요 실수입니다.
오류 1, 싱글톤 ConnectionMultiplexer Redis 연결 개체가 없으며, 싱글톤 개체를 잠그면 동시 상황에서 Redis의 성능이 제한될 것이라고 순진하게 생각했습니다.
오류 2. 마스터-슬레이브 상황에서 수동 전환이 발생하면 전환 이벤트를 구독하고 이벤트 발생 후 연결 개체가 가리키는 Endpoint를 동적으로 변경해야 한다고 생각했습니다.
문서를 다시 자세히 읽어보니 실수를 깨달았습니다. 늦은 수정이지만 아직 부족한 부분이 많다고 느껴서 숙련된 드라이버의 지도와 제안이 꼭 필요합니다.
수정 사항 1. Redis 연결 개체를 생성하는 비용은 매우 높으며, 네트워크 요청 중에 연결 개체가 대기하지 않기 때문에 싱글톤 잠금은 Redis 성능에 영향을 주지 않습니다.
수정 2, Redis가 마스터-슬레이브인 경우 센티널이 마스터-슬레이브 관계를 전환한 후 StackExchange.Redis는 우리 측의 어떠한 작업 없이도 새로운 마스터-슬레이브를 식별합니다.
아직까지 두 가지 질문이 있습니다.
질문 1. 문서를 읽어봐도 뚜렷한 결과가 없습니다. 마스터-슬레이브 읽기-쓰기 분리가 수행될 때 엔드포인트 컬렉션에 여러 노드를 추가하면 읽기-쓰기 분리가 자동으로 수행됩니까? 아니면 명령을 읽는 방법에서 CommandFlags.PreferSlave를 지정해야 합니까? 내 생각엔 후자인 것 같은데? 그래서 모든 읽기 방법에 PreferSlave를 지정합니다. 베테랑 운전자들은 뭐라고 말합니까?
질문 2. LuaScript.Prepare(lua)를 사용하고 로드합니다. lua를 실행하면 항상 효과가 없으며 LuaScript.GetCachedScriptCount()는 0입니다. 그러나 ScriptEvaluateAsync를 직접 사용하면 쉽습니다. Old 운전자가 좋은 예를 가지고 있다면 경험이 풍부한 운전자가 조언을 해주거나 공유할 수 있기를 바랍니다.
2. 참고용 코드 구조
구조는 대략 다음과 같습니다. RedisAsyncHelper 아래의 모든 클래스는 부분 클래스이며 클래스 이름은 RedisHelper입니다. . 그들은 IRedisHelper 인터페이스를 공동으로 구현하고 자세한 의견을 남겼습니다.
동기 버전과 비동기 버전의 디렉터리 구조는 동일합니다.
3. 준비 단계
CommonHelper의 두 도우미 클래스:
RedisInnerTypeHelper.cs
using StackExchange.Redis; using System.Collections.Generic; using System.Linq; namespace Fantasy.RedisRepository.CommonHelper { internal class RedisInnerTypeHelper { public static List<T> RedisValuesToGenericList<T>(RedisValue[] redisValues) { var result = new List<T>(); redisValues.ToList().ForEach(r => result.Add(SerializeHelper.Deserialize<T>(r))); return result; } public static RedisValue[] GenericListToRedisValues<T>(List<T> values) { var redisValues = new List<RedisValue>(); values.ForEach(v => redisValues.Add(SerializeHelper.Serialize(values))); return redisValues.ToArray(); } public static RedisKey[] GenericListToRedisKeys(List<string> keys) { var redisKeys = new List<RedisKey>(); keys.ForEach(k => redisKeys.Add(k)); return redisKeys.ToArray(); } } }
SerializeHelper.cs
using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace Fantasy.RedisRepository.CommonHelper { internal static class SerializeHelper { /// <summary> /// 字节数组序列化 /// </summary> /// <param name="o"></param> /// <returns></returns> internal static byte[] Serialize(object o) { if (o == null) { return null; } BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, o); byte[] objectDataAsStream = memoryStream.ToArray(); return objectDataAsStream; } } /// <summary> /// 字节数组反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <returns></returns> internal static T Deserialize<T>(byte[] stream) { if (stream == null) { return default(T); } BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream(stream)) { T result = (T)binaryFormatter.Deserialize(memoryStream); return result; } } } }
using System; using System.Configuration; namespace Fantasy.RedisRepository.Config { internal class ConfigHelper { internal static T Get<T>(string appSettingsKey, T defaultValue) { string text = ConfigurationManager.AppSettings[appSettingsKey]; if (string.IsNullOrWhiteSpace(text)) return defaultValue; try { var value = Convert.ChangeType(text, typeof(T)); return (T)value; } catch { return defaultValue; } } } }
RedisClientConfig.cs
namespace Fantasy.RedisRepository.Config { internal class RedisClientConfig { private static string _server = ConfigHelper.Get("RedisServer", "115.xx.xx.31"); /// <summary> /// 节点IP /// </summary> public static string Server { get { return _server; } set { _server = value; } } private static int _port = ConfigHelper.Get("RedisPort", 6380); /// <summary> /// 节点端口 /// </summary> public static int Port { get { return _port; } set { _port = value; } } private static string _slaveServer = ConfigHelper.Get("SlaveServer", "115.xx.xx.31"); /// <summary> /// 节点IP /// </summary> public static string SlaveServer { get { return _slaveServer; } set { _slaveServer = value; } } private static int _slavePort = ConfigHelper.Get("SlavePort", 6381); /// <summary> /// 节点端口 /// </summary> public static int SlavePort { get { return _slavePort; } set { _slavePort = value; } } private static string _auth = ConfigHelper.Get("RedisAuth", "fantasy.."); /// <summary> /// 节点密码 /// </summary> public static string RedisAuth { get { return _auth; } set { _auth = value; } } private static int _defaultDatabase = ConfigHelper.Get("RedisDataBase", 0); /// <summary> /// redis默认0号库 /// </summary> public static int DefaultDatabase { get { return _defaultDatabase; } set { _defaultDatabase = value; } } private static int _connectTimeout = 10000; public static int ConnectTimeout { get { return _connectTimeout; } set { _connectTimeout = value; } } private static int _connectRetry = 3; public static int ConnectRetry { get { return _connectRetry; } set { _connectRetry = value; } } private static bool _preserveAsyncOrder = false; public static bool PreserveAsyncOrder { get { return _preserveAsyncOrder; } set { _preserveAsyncOrder = value; } } } }
RedisConnection.cs
using Fantasy.RedisRepository.Config; using StackExchange.Redis; namespace Fantasy.RedisRepository { /// <summary> /// Redis连接类 /// </summary> public static class RedisConnection { private static ConnectionMultiplexer _connection; private static readonly object SyncObject = new object(); /// <summary> /// redis连接对象,单例加锁不影响性能 /// </summary> public static ConnectionMultiplexer GenerateConnection { get { if (_connection == null || !_connection.IsConnected) { lock (SyncObject) { var configurationOptions = new ConfigurationOptions() { Password = RedisClientConfig.RedisAuth, EndPoints = { {RedisClientConfig.Server, RedisClientConfig.Port}, {RedisClientConfig.SlaveServer, RedisClientConfig.SlavePort} } }; _connection = ConnectionMultiplexer.Connect(configurationOptions); } } return _connection; } } } }
4. RedisHelper
는 실제로 레이어 직렬화 래퍼입니다.
IRedisHelper:
using System; using StackExchange.Redis; using System.Collections.Generic; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { /// <summary> /// 异步方法接口 --Author 吴双 www.cnblogs.com/tdws /// 存入数据均为方法内部序列化后的byte,所以取数据的时候需要反序列化时,请指定正确的数据类型 /// </summary> public partial interface IRedisHelper { #region Redis数据类型—String /// <summary> /// 将任何数据以redis string存储 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="timeout"></param> /// <returns></returns> Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null); /// <summary> /// 对数值进行减法操作,默认-1 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns>操作后的结果</returns> Task<long> StringDecrementAsync(string key, long value = 1L); /// <summary> /// 对数值进行加法操作,默认+1 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns>操作后的结果</returns> Task<long> StringIncrementAsync(string key, long value = 1L); /// <summary> /// 从redis string中以指定类型取出 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> StringGetAsync<T>(string key); #endregion #region Redis数据类型—Hash /// <summary> /// 向Hash key中存储任意类型任意值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="field"></param> /// <param name="value"></param> /// <returns>是否成功</returns> Task<bool> HashSetAsync<T>(string key, string field, T value); /// <summary> /// 批量 向Hash key中存储任意类型任意值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="hashFields"></param> /// <returns>无返回值</returns> Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields); /// <summary> /// 对指定hash key中制定field做数量增加操作 默认自增1 /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="incrCount"></param> /// <returns>操作后的结果</returns> Task<long> HashIncrementAsync(string key, string field, long incrCount = 1); /// <summary> /// 对指定hash key中制定field做数量增加操作 默认自减1 /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="decrCount"></param> /// <returns>操作后的结果</returns> Task<long> HashDecrementAsync(string key, string field, long decrCount = 1); /// <summary> /// 从指定Hash中 删除指定field /// 如果key或者field不存在,则false /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns>是否成功</returns> Task<bool> HashDeleteFieldAsync(string key, string field); /// <summary> /// 从指定Hash key中 批量删除指定field /// 如果key或者field不存在,则false /// </summary> /// <param name="key"></param> /// <param name="fields"></param> /// <returns>移除数量</returns> Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields); /// <summary> /// 从指定Hash key中获取指定field值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> Task<T> HashGetAsync<T>(string key, string field); /// <summary> /// 从指定Hash key中判断field是否存在 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> Task<bool> HashFieldExistAsync(string key, string field); /// <summary> /// 获取指定Hash key中的所有field的值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<List<T>> HashValuesAsync<T>(string key); /// <summary> /// 获取指定Hash key中所有 field名称及其Value /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<Dictionary<string, T>> HashGetAllAsync<T>(string key); /// <summary> /// 获取指定Hash key中所有field /// </summary> /// <param name="key"></param> /// <returns></returns> Task<List<string>> HashFieldsAsync(string key); #endregion #region Redis数据类型—List /// <summary> /// 在指定pivot后插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0 /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot后面. /// 即链表从左向右查找,遇到指定pivot,则确定位置 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pivot">list中的一个值</param> /// <param name="value"></param> /// <returns></returns> Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value); /// <summary> /// 在指定pivot前插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0 /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot前面. /// 即链表从左向右查找,遇到指定pivot,则确定位置 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pivot"></param> /// <param name="value"></param> /// <returns></returns> Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value); /// <summary> /// 从链表左侧弹出第一个元素(弹出能获取到该元素并且被删除) /// 如果key不存在 或者链表为空 则为null /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> ListLeftPopAsync<T>(string key); /// <summary> /// 从链表左侧增加一个元素,key不存在则被创建 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns>返回操作后的链表长度</returns> Task<long> ListLeftPushAsync<T>(string key, T value); /// <summary> /// 从链表左侧批量增加元素,如果 a b c 则c会在链表左侧第一位 b第二位 a第三位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns>返回操作后的链表长度</returns> Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values); /// <summary> /// 获取链表长度,不存在key则为0 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<long> ListLengthAsync<T>(string key); /// <summary> /// 获取链表中所有数据,从左侧start开始到stop结束,从0—-1则认为获取全部,默认获取全部 /// start为负数则代表从链表右侧开始,-1为右侧第一位,-2为右侧第二位 /// start要小于stop,否则返回null /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L); /// <summary> /// 从链表中一处count数量的value. count大于0则从左至右,count小于0则从右至左,count=0则移除全部 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="count"></param> /// <returns></returns> Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L); /// <summary> /// 从右侧弹出第一个元素(弹出能获取到该元素并且被删除) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> ListRightPopAsync<T>(string key); /// <summary> /// 从链表右侧加入元素,如果 rpush a b c 则c为右侧第一位 b第二位 c第三位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<long> ListRightPushAsync<T>(string key, T value); /// <summary> /// 从右侧批量插入,和左侧相反 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> Task<long> ListRightMultiPushAsync<T>(string key, List<T> values); /// <summary> /// 在链表指定索引处,插入元素 /// 正数索引从0开始,代表左侧。负数从-1开始 代表从右侧。-1为右侧第一位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="index"></param> /// <param name="value"></param> /// <returns></returns> Task ListSetByIndexAsync<T>(string key, int index, T value); /// <summary> /// 留下start到stop之间的数据。负数代表从右侧寻找 -1为右侧第一位 /// </summary> /// <param name="key"></param> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> Task ListTrimAsync(string key, long start, long stop); /// <summary> /// 获取指定index的值,负数代表从右侧寻找 -1为右侧第一位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="index"></param> /// <returns></returns> Task<T> ListGetByIndexAsync<T>(string key, long index); #endregion #region Redis数据类型—Set /// <summary> /// 向指定集合中增加一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetAddAsync<T>(string key, T value); /// <summary> /// 指定集合计算操作operation枚举,指定计算结果将存的目标destKey,指定需要参与计算的多个key /// </summary> /// <param name="operation"></param> /// <param name="destKey"></param> /// <param name="combineKeys"></param> /// <returns></returns> Task<long> SetCombineAndStoreAsync(SetOperation operation, string destKey, List<string> combineKeys); /// <summary> /// 指定集合计算操作operation枚举,指定需要参与计算的多个key /// </summary> /// <typeparam name="T"></typeparam> /// <param name="operation"></param> /// <param name="combineKeys"></param> /// <returns></returns> Task<List<T>> SetCombineAsync<T>(SetOperation operation, List<string> combineKeys); /// <summary> /// 指定值是否存在于指定集合中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetContainsAsync<T>(string key, T value); /// <summary> /// 获取指定集合中元素个数 /// </summary> /// <param name="key"></param> /// <returns></returns> Task<long> SetLengthAsync(string key); /// <summary> /// 获取指定集合中的所有元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<List<T>> SetMembersAsync<T>(string key, T value); /// <summary> /// 从sourceKey移除指定value到目标distKey集合当中 /// 如果sourceKey存在指定value则返回true,否则不做任何操作返回false /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sourcekey"></param> /// <param name="distKey"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetMoveAsync<T>(string sourcekey, string distKey, T value); /// <summary> /// 从指定集合当中随机取出一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> SetRandomMemberAsync<T>(string key); /// <summary> /// 从指定集合随机弹出(删除并获取)一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> SetPopAsync<T>(string key); /// <summary> /// 从集合中随机弹出(删除并获取)多个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<List<T>> SetRandomMembersAsync<T>(string key); /// <summary> /// 从集合中移除指定元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetRemoveAsync<T>(string key, T value); /// <summary> /// 从集合中批量移除元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> Task<long> SetMultiRemoveAsync<T>(string key, List<T> values); #endregion #region Redis数据类型—SortSet #endregion #region Redis Key操作 /// <summary> /// 删除指定key /// </summary> /// <param name="key"></param> /// <returns></returns> Task<bool> KeyDeleteAsync(string key); /// <summary> /// 设置key过期时间具体DateTime /// </summary> /// <param name="key"></param> /// <param name="expireAt"></param> /// <returns></returns> Task<bool> KeyExpireAtAsync(string key, DateTime expireAt); /// <summary> /// 设置key在将来的timeout后过期(TimeSpan) /// </summary> /// <param name="key"></param> /// <param name="timeout"></param> /// <returns></returns> Task<bool> KeyExpireInAsync(string key, TimeSpan timeout); /// <summary> /// key重命名 /// </summary> /// <param name="key"></param> /// <param name="newKey"></param> /// <returns></returns> Task<bool> KeyRenameAsync(string key, string newKey); /// <summary> /// 判断key是否已存在 /// </summary> /// <param name="key"></param> /// <returns></returns> Task<bool> KeyExistsAsync(string key); #endregion #region Redis Transcation /// <summary> /// 在事务中执行一系列redis命令。注意:在委托中的一系列命令的所有 值 都需要进行字节数组序列化 /// </summary> /// <param name="ranOperations"></param> /// <returns></returns> Task<bool> DoInTranscationAsync(Action<ITransaction> ranOperations); #endregion Task<RedisResult> Test(); } }
RedisHelper 부분 클래스 RedisStringHelperAsync.cs
using System; using Fantasy.RedisRepository.CommonHelper; using StackExchange.Redis; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { /// <summary> /// Redis异步操作类 String部分类 /// </summary> internal partial class RedisHelper// : IRedisHelper { private static IDatabase _client; internal RedisHelper() { _client = RedisConnection.GenerateConnection.GetDatabase(); } #region String 写操作 /// <summary> /// 将任何数据添加到redis中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="timeout"></param> /// <returns></returns> public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null) { return await _client.StringSetAsync(key, SerializeHelper.Serialize(value), timeout); } public async Task<long> StringDecrementAsync(string key, long value = 1L) { return await _client.StringDecrementAsync(key, value); } public async Task<long> StringIncrementAsync(string key, long value = 1L) { return await _client.StringIncrementAsync(key, value); } #endregion #region String 读操作 /// <summary> /// 根据key获取指定类型数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public async Task<T> StringGetAsync<T>(string key) { return SerializeHelper.Deserialize<T>(await _client.StringGetAsync(key, CommandFlags.PreferSlave)); } #endregion } }
RedisHelper 부분 클래스 RedisHashHelperAsync.cs
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 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 } }
RedisHelper 부분 클래스 RedisListHelperAsync.cs
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 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 } }
RedisLuaHelper.cs는 여기에 일부 기능적 Lua 스크립트를 설치합니다. 외부적으로는 키와 같은 매개변수가 아직 전달되지 않으며 단지 예일 뿐입니다.
using StackExchange.Redis; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { internal partial class RedisHelper { public async Task<RedisResult> LuaMutilGetHash() { string lua = @"local result={} for i, v in ipairs(KEYS) do result[i] = redis.call('hgetall',v) end return result"; var res = await _client.ScriptEvaluateAsync(lua, new RedisKey[] { "people:1", "people:2", "people:3" }); var res1= LuaScript.GetCachedScriptCount(); return res; } } }
Transcation encapsulation에 대해서는 개인적으로 좋은 방법이 없습니다.
public async Task<bool> DoInTranscationAsync(Action<ITransaction> runOperations) { var tran = RedisConnection.GenerateConnection.GetDatabase().CreateTransaction(); runOperations(tran); return await tran.ExecuteAsync(); }
RedisFactory.cs
using Fantasy.RedisRepository.RedisHelpers; namespace Fantasy.RedisRepository { public class RedisFactory { /// <summary> /// 外部访问redis入口,暂时只暴露异步方法 /// </summary> /// <returns></returns> public static IRedisHelper CreateRedisRepository() { return new RedisHelper(); } } }
위 내용은 이 글의 전체 내용입니다. 이 글의 내용이 모든 분들의 공부나 업무에 조금이나마 도움이 되기를 바라며, PHP 중국어 사이트도 지원하고 싶습니다!
RedisRepository 공유 및 오류 수정에 관한 더 많은 글은 PHP 중국어 홈페이지를 주목해주세요!