123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- using ERP.Framework.Config;
- using StackExchange.Redis;
- using System.Diagnostics.CodeAnalysis;
- namespace ERP.Framework.Cache
- {
- public static class RedisHelper
- {
- [NotNull]
- private static IDatabase? DB { get; set; }
- private static IConnectionMultiplexer? _connMultiplexer;
- private static readonly object _lock = new object();
- public static void Init(RedisConfig redisConfig)
- {
- try
- {
- var redisConnectionStr = $"{redisConfig.Host}:{redisConfig.Port}";
- var options = ConfigurationOptions.Parse(redisConnectionStr);
- options.Password = redisConfig.Password;
- var connectionMultiplexer = GetConnectRedisMultiplexer(options);
- DB = connectionMultiplexer.GetDatabase();
- }
- catch (Exception err)
- {
- // Todo Redis初始化异常
- }
- }
- private static IConnectionMultiplexer GetConnectRedisMultiplexer(ConfigurationOptions options)
- {
- if (_connMultiplexer == null)
- {
- lock (_lock)
- {
- if (_connMultiplexer != null)
- {
- return _connMultiplexer;
- }
- _connMultiplexer = ConnectionMultiplexer.Connect(options);
- //Todo 注册事件
- //_connMultiplexer.ConnectionFailed
- //_connMultiplexer.ConnectionFailed += MuxerConnectionFailed;
- //_connMultiplexer.ConnectionRestored += MuxerConnectionRestored;
- //_connMultiplexer.ErrorMessage += MuxerErrorMessage;
- //_connMultiplexer.ConfigurationChanged += MuxerConfigurationChanged;
- //_connMultiplexer.HashSlotMoved += MuxerHashSlotMoved;
- //_connMultiplexer.InternalError += MuxerInternalError;
- }
- }
- return _connMultiplexer;
- }
- #region Event
- /// <summary>
- /// 添加注册事件
- /// </summary>
- private static void AddRegisterEvent()
- {
- //_connMultiplexer.ConnectionRestored += ConnMultiplexer_ConnectionRestored;
- //_connMultiplexer.ConnectionFailed += ConnMultiplexer_ConnectionFailed;
- //_connMultiplexer.ErrorMessage += ConnMultiplexer_ErrorMessage;
- //_connMultiplexer.ConfigurationChanged += ConnMultiplexer_ConfigurationChanged;
- //_connMultiplexer.HashSlotMoved += ConnMultiplexer_HashSlotMoved;
- //_connMultiplexer.InternalError += ConnMultiplexer_InternalError;
- //_connMultiplexer.ConfigurationChangedBroadcast += ConnMultiplexer_ConfigurationChangedBroadcast;
- }
- ///// <summary>
- ///// 重新配置广播时(通常意味着主从同步更改)
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 发生内部错误时(主要用于调试)
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 更改集群时
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_HashSlotMoved(object sender, HashSlotMovedEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 配置更改时
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 发生错误时
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 物理连接失败时
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e)
- //{
- //}
- ///// <summary>
- ///// 建立物理连接时
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e)
- //{
- //}
- #endregion Event
- #region String
- /// <summary>
- /// set or update the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool Save(string key, string value)
- {
- return DB.StringSet(key, value);
- }
- /// <summary>
- /// set or update the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool SaveExpire(string key, string value, TimeSpan? expireMinutes)
- {
- return DB.StringSet(key, value, expireMinutes);
- }
- /// <summary>
- /// set or update the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool SaveExpire(string key, string value, int expireMinutes)
- {
- return DB.StringSet(key, value, TimeSpan.FromMinutes(expireMinutes));
- }
- /// <summary>
- /// get the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string Get(string key)
- {
- return DB.StringGet(key);
- }
- /// <summary>
- /// get the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static RedisValueWithExpiry GetWithExpire(string key)
- {
- return DB.StringGetWithExpiry(key);
- }
- /// <summary>
- /// Delete the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static bool Delete(string key)
- {
- return DB.KeyDelete(key);
- }
- #endregion String
- ///// <summary>
- ///// 新增Redis值(有时限)
- ///// </summary>
- ///// <param name="key">id</param>
- ///// <param name="value">值</param>
- ///// <param name="expireMinutes">时限(分钟,默认30天)</param>
- ///// <returns></returns>
- //public static bool AddExpire(string key, string value, int expireMinutes = 24 * 60 * 30)
- // => new RedisHelper().AddExpireValue(key, value, expireMinutes);
- ///// <summary>
- ///// 保存一个对象
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key"></param>
- ///// <param name="obj"></param>
- ///// <returns></returns>
- //public bool SetStringKey<T>(string key, T obj, int expireMinutes = 0)
- //{
- // string json = JsonConvert.SerializeObject(obj);
- // if (expireMinutes > 0)
- // {
- // return DB.StringSet(key, json, TimeSpan.FromMinutes(expireMinutes));
- // }
- // else
- // return DB.StringSet(key, json);
- //}
- ///// <summary>
- ///// 获取一个key的对象
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key"></param>
- ///// <returns></returns>
- //public T GetStringKey<T>(string key) where T : class
- //{
- // var result = DB.StringGet(key);
- // if (string.IsNullOrEmpty(result))
- // {
- // return null;
- // }
- // try
- // {
- // return JsonConvert.DeserializeObject<T>(result);
- // }
- // catch
- // {
- // return null;
- // }
- //}
- ///// <summary>
- ///// set or update the HashValue for string key
- ///// </summary>
- ///// <param name="key"></param>
- ///// <param name="hashkey"></param>
- ///// <param name="value"></param>
- ///// <returns></returns>
- //public bool SetHashValue(string key, string hashkey, string value)
- //{
- // return DB.HashSet(key, hashkey, value);
- //}
- ///// <summary>
- ///// set or update the HashValue for string key
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key"></param>
- ///// <param name="hashkey"></param>
- ///// <param name="t">defined class</param>
- ///// <returns></returns>
- //public bool SetHashValue<T>(String key, string hashkey, T t) where T : class
- //{
- // var json = JsonConvert.SerializeObject(t);
- // return DB.HashSet(key, hashkey, json);
- //}
- //public static void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
- // => new RedisHelper().HashSetValue(key, list, getModelId);
- //public static void HashSetExpire<T>(string key, List<T> list, Func<T, string> getModelId, int expireTime = 24 * 60 * 30)
- //=> new RedisHelper().HashSetExpireValue(key, list, getModelId, expireTime);
- ///// <summary>
- ///// 保存一个集合
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key">Redis Key</param>
- ///// <param name="list">数据集合</param>
- ///// <param name="getModelId"></param>
- //public void HashSetValue<T>(string key, List<T> list, Func<T, string> getModelId)
- //{
- // List<HashEntry> listHashEntry = new List<HashEntry>();
- // foreach (var item in list)
- // {
- // string json = JsonConvert.SerializeObject(item);
- // listHashEntry.Add(new HashEntry(getModelId(item), json));
- // }
- // DB.HashSet(key, listHashEntry.ToArray());
- //}
- ///// <summary>
- ///// 保存一个集合
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key">Redis Key</param>
- ///// <param name="list">数据集合</param>
- ///// <param name="getModelId"></param>
- //private void HashSetExpireValue<T>(string key, List<T> list, Func<T, string> getModelId, int expireTime)
- //{
- // List<HashEntry> listHashEntry = new List<HashEntry>();
- // foreach (var item in list)
- // {
- // string json = JsonConvert.SerializeObject(item);
- // listHashEntry.Add(new HashEntry(getModelId(item), json));
- // }
- // DB.HashSet(key, listHashEntry.ToArray());
- // DB.KeyExpire(key, TimeSpan.FromMinutes(expireTime));
- //}
- ///// <summary>
- ///// 获取hashkey所有的值
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="key"></param>
- ///// <returns></returns>
- //public List<T> HashGetAll<T>(string key) where T : class
- //{
- // List<T> result = new List<T>();
- // HashEntry[] arr = DB.HashGetAll(key);
- // foreach (var item in arr)
- // {
- // if (!item.Value.IsNullOrEmpty)
- // {
- // result.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(item.Value));
- // }
- // }
- // return result;
- //}
- ///// <summary>
- ///// get the HashValue for string key and hashkey
- ///// </summary>
- ///// <param name="key">Represents a key that can be stored in redis</param>
- ///// <param name="hashkey"></param>
- ///// <returns></returns>
- //public RedisValue GetHashValue(string key, string hashkey)
- //{
- // RedisValue result = DB.HashGet(key, hashkey);
- // return result;
- //}
- ///// <summary>
- ///// get the HashValue for string key and hashkey
- ///// </summary>
- ///// <param name="key">Represents a key that can be stored in redis</param>
- ///// <param name="hashkey"></param>
- ///// <returns></returns>
- //public T GetHashValue<T>(string key, string hashkey) where T : class
- //{
- // RedisValue result = DB.HashGet(key, hashkey);
- // if (string.IsNullOrEmpty(result))
- // {
- // return null;
- // }
- // T t = JsonConvert.DeserializeObject<T>(result);
- // return null;
- //}
- ///// <summary>
- ///// delete the HashValue for string key and hashkey
- ///// </summary>
- ///// <param name="key"></param>
- ///// <param name="hashkey"></param>
- ///// <returns></returns>
- //public bool DeleteHashValue(string key, string hashkey)
- //{
- // return DB.HashDelete(key, hashkey);
- //}
- }
- }
|