123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839 |
-
- using Newtonsoft.Json;
- using StackExchange.Redis;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace OASystem.Infrastructure.Tools
- {
- #region MyRegion
- //**************操作记录******************
- //内容描述:Redis帮助类
- //***************************************
- //public static class RedisHelper
- //{
- // private static string _strConn = "132.232.92.186:6379";
- // private static string _strPwd = "";
- // private static int _strDb = -1;
- // static ConnectionMultiplexer _redis;
- // static readonly object _locker = new object();
- // #region 单例模式
- // public static ConnectionMultiplexer Manager
- // {
- // get
- // {
- // if (_redis == null)
- // {
- // lock (_locker)
- // {
- // if (_redis != null) return _redis;
- // _redis = GetManager();
- // return _redis;
- // }
- // }
- // return _redis;
- // }
- // }
- // private static ConnectionMultiplexer GetManager(string connectionString = null)
- // {
- // if (string.IsNullOrEmpty(connectionString))
- // {
- // connectionString = _strConn;
- // }
- // var options = ConfigurationOptions.Parse(connectionString);
- // options.Password = _strPwd;
- // return ConnectionMultiplexer.Connect(options);
- // }
- // #endregion
- // #region 辅助方法
- // /// <summary>
- // /// 对象序列化成字符串
- // /// </summary>
- // /// <typeparam name="T">泛型对象</typeparam>
- // /// <param name="value"></param>
- // /// <returns></returns>
- // private static string ConvertJson<T>(T value)
- // {
- // string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
- // return result;
- // }
- // /// <summary>
- // /// RedisValue序列化成对象
- // /// </summary>
- // /// <typeparam name="T">序列化后的对象</typeparam>
- // /// <param name="value">RedisValue</param>
- // /// <returns></returns>
- // private static T ConvertObj<T>(RedisValue value)
- // {
- // if (value.IsNull)
- // {
- // return default(T);
- // }
- // else
- // {
- // return JsonConvert.DeserializeObject<T>(value);
- // }
- // }
- // /// <summary>
- // /// 多个值序列化成list集合
- // /// </summary>
- // /// <typeparam name="T">集合对象</typeparam>
- // /// <param name="values">RedisValue</param>
- // /// <returns></returns>
- // private static List<T> ConvetList<T>(RedisValue[] values)
- // {
- // List<T> result = new List<T>();
- // foreach (var item in values)
- // {
- // var model = ConvertObj<T>(item);
- // if (model != null)
- // result.Add(model);
- // }
- // return result;
- // }
- // private static RedisKey[] ConvertRedisKeys(List<string> redisKeys, string prefix)
- // {
- // if (string.IsNullOrEmpty(prefix))
- // {
- // return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
- // }
- // else
- // {
- // return redisKeys.Select(redisKey => (RedisKey)(prefix + ":" + redisKey)).ToArray();
- // }
- // }
- // /// <summary>
- // /// 获取要操作的库
- // /// </summary>
- // /// <param name="db">库,0和-1都是第一个库,1是第二个库...</param>
- // /// <returns></returns>
- // private static int GetOperationDB(RedisEnum db)
- // {
- // if (db == RedisEnum.DB0)
- // {
- // return _strDb;
- // }
- // else
- // {
- // return (int)db;
- // }
- // }
- // /// <summary>
- // /// 获得枚举的Description
- // /// </summary>
- // /// <param name="value">枚举值</param>
- // /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
- // /// <returns>枚举的Description</returns>
- // private static string GetDescription(this Enum value, Boolean nameInstead = true)
- // {
- // Type type = value.GetType();
- // string name = Enum.GetName(type, value);
- // if (name == null)
- // {
- // return null;
- // }
- // FieldInfo field = type.GetField(name);
- // DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
- // if (attribute == null && nameInstead == true)
- // {
- // return name;
- // }
- // return attribute == null ? null : attribute.Description;
- // }
- // #endregion
- // /// <summary>
- // /// 是否存在
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // public static bool KeyExists(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // try
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).KeyExists(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- // }
- // /// <summary>
- // /// 设置过期时间
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="min">过期时间,单位:分钟</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // public static bool KeyExpire(string key, int min = 600, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // try
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).KeyExpire(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, DateTime.Now.AddMinutes(min));
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- // }
- // /// <summary>
- // /// 修改键
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="newKey">新键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool KeyRename(string key, string newKey, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // try
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).KeyRename(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, string.IsNullOrEmpty(strFd) ? newKey : strFd + ":" + newKey);
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- // }
- // /// <summary>
- // /// 清空
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static IEnumerable<RedisKey> AllClear(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // return Manager.GetServer(_strConn, _strPwd).Keys(GetOperationDB(db), key);
- // }
- // /// <summary>
- // /// 删除
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool KeyDelete(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // try
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).KeyDelete(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- // }
- // /// <summary>
- // /// 批量删除
- // /// </summary>
- // /// <param name="keys">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static long KeyDelete(List<string> keys, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // try
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).KeyDelete(ConvertRedisKeys(keys, strFd));
- // }
- // catch (Exception)
- // {
- // return 0;
- // }
- // }
- // /// <summary>
- // /// 缓存单个字符串
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="value">值</param>
- // /// <param name="expireMinutes">过期时间,单位:分钟</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool StringSet(string key, string value, int expireMinutes = 600, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).StringSet(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, value, TimeSpan.FromMinutes(expireMinutes));
- // }
- // /// <summary>
- // /// 批量缓存字符串
- // /// </summary>
- // /// <param name="keysStr">键</param>
- // /// <param name="valuesStr">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool StringSet(string[] keysStr, string[] valuesStr, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // var vCount = keysStr.Length;
- // var vKeyValuePair = new KeyValuePair<RedisKey, RedisValue>[vCount];
- // for (int i = 0; i < vCount; i++)
- // {
- // vKeyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(string.IsNullOrEmpty(strFd) ? keysStr[i] : strFd + ":" + keysStr[i], valuesStr[i]);
- // }
- // return Manager.GetDatabase(GetOperationDB(db)).StringSet(vKeyValuePair);
- // }
- // /// <summary>
- // /// 缓存限时对象
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="obj">值</param>
- // /// <param name="expireMinutes">过期时间,单位:分钟</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool StringSet<T>(string key, T obj, int expireMinutes = 600, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).StringSet(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, JsonConvert.SerializeObject(obj), TimeSpan.FromMinutes(expireMinutes));
- // }
- // /// <summary>
- // /// 缓存对象
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="obj">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static bool StringSet<T>(string key, T obj, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).StringSet(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, JsonConvert.SerializeObject(obj));
- // }
- // /// <summary>
- // /// 根据key获取值
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static string StringGet(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).StringGet(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // }
- // /// <summary>
- // /// 批量根据key获取
- // /// </summary>
- // /// <param name="keys">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static RedisValue[] StringGet(List<string> keys, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).StringGet(ConvertRedisKeys(keys, strFd));
- // }
- // /// <summary>
- // /// 根据key获取单个对象
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static T StringGet<T>(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // string vValue = Manager.GetDatabase(GetOperationDB(db)).StringGet(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // return ConvertObj<T>(vValue);
- // }
- // /// <summary>
- // /// 入栈(后插入的在List前面)
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="value">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // public static long ListLeftPush<T>(string key, T value, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).ListLeftPush(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, ConvertJson(value));
- // }
- // /// <summary>
- // /// 批量入栈(后插入的在List前面)
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="values">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static long ListLeftPush<T>(string key, List<T> values, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // var vRedisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
- // return Manager.GetDatabase(GetOperationDB(db)).ListLeftPush(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, vRedisValues);
- // }
- // /// <summary>
- // /// 出栈(删除最前面的一个元素并返回)
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static T ListLeftPop<T>(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // var vRedisValues = Manager.GetDatabase(GetOperationDB(db)).ListLeftPop(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // return ConvertObj<T>(vRedisValues);
- // }
- // /// <summary>
- // /// 入队(后插入的在List后面)
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="value">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // public static long ListRightPush<T>(string key, T value, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).ListRightPush(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, ConvertJson(value));
- // }
- // /// <summary>
- // /// 批量入队(后插入的在List后面)
- // /// </summary>
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="values">值</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static long ListRightPush<T>(string key, List<T> values, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // var vRedisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
- // return Manager.GetDatabase(GetOperationDB(db)).ListRightPush(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, vRedisValues);
- // }
- // /// <summary>
- // /// 获取
- // /// <typeparam name="T">类型</typeparam>
- // /// <param name="key">键</param>
- // /// <param name="start">索引开始</param>
- // /// <param name="stop">索引结束</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static List<T> ListRange<T>(string key, long start = 0, long stop = -1, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // var vRedisValues = Manager.GetDatabase(GetOperationDB(db)).ListRange(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key, start, stop);
- // return ConvetList<T>(vRedisValues);
- // }
- // /// <summary>
- // /// 获取个数
- // /// </summary>
- // /// <param name="key">键</param>
- // /// <param name="folder">目录,默认根目录</param>
- // /// <param name="db">库,默认读取配置文件</param>
- // /// <returns></returns>
- // public static long ListLength(string key, RedisFolderEnum folder = RedisFolderEnum.Root, RedisEnum db = RedisEnum.DB0)
- // {
- // string strFd = GetDescription(folder);
- // return Manager.GetDatabase(GetOperationDB(db)).ListLength(string.IsNullOrEmpty(strFd) ? key : strFd + ":" + key);
- // }
- //}
- #endregion
- #region MyRegion
- /// <summary>
- /// Redis帮助类
- /// </summary>
- //public class RedisHelper : IDisposable
- //{
- // private static readonly object Locker = new object();
- // private static ConnectionMultiplexer redis = null;
- // private static bool connected = false;
- // private IDatabase db = null;
- // private static string _ip;
- // private static string _port;
- // private static string _pwd;
- // public bool IsConnected { get { GetRedisConnect(); return redis.IsConnected; } }
- // //public RedisHelper(string ip, string port, string pwd = "")
- // //{
- // // _ip = ip;
- // // _port = port;
- // // _pwd = pwd;
- // //}
- // /// <summary>
- // /// Redis连接
- // /// </summary>
- // /// <returns></returns>
- // private int GetRedisConnect()
- // {
- // if (connected)
- // {
- // return 0;
- // }
- // lock (Locker)
- // {
- // if (redis == null || !redis.IsConnected)
- // {
- // redis = ConnectionMultiplexer.Connect($"{_ip}:{_port},password={_pwd},abortConnect = false");
- // }
- // }
- // connected = true;
- // return 0;
- // }
- // public void Using(Action<RedisHelper> action)
- // {
- // using (var redisHelper = new RedisHelper(_ip, _port, _pwd))
- // {
- // action(redisHelper);
- // }
- // }
- // public RedisHelper Use(int redisEnum)
- // {
- // db = redis.GetDatabase(redisEnum);
- // //Log.Logs($"RedisDB Conntet State: {redis.IsConnected}");
- // var t = db.Ping();
- // //Log.Logs($"RedisDB Select {i}, Ping.{t.TotalMilliseconds}ms");
- // return this;
- // }
- // public void Close(bool allowCommandsToComplete = true)
- // {
- // if (redis != null)
- // {
- // redis.Close(allowCommandsToComplete);
- // }
- // }
- // public void CloseAsync(bool allowCommandsToComplete = true)
- // {
- // if (redis != null)
- // {
- // redis.CloseAsync(allowCommandsToComplete);
- // }
- // }
- // public void Dispose()
- // {
- // db = null;
- // }
- // #region 发布订阅
- // public delegate void RedisDeletegate(string str);
- // public event RedisDeletegate RedisSubMessageEvent;
- // /// <summary>
- // /// 发布消息
- // /// </summary>
- // /// <param name="channel"></param>
- // /// <param name="msg"></param>
- // /// <returns></returns>
- // public long RedisPublish(string channel, string msg)
- // {
- // ISubscriber sub = redis.GetSubscriber();
- // return sub.Publish(channel, msg);
- // }
- // /// <summary>
- // /// 订阅消息
- // /// </summary>
- // /// <param name="subChannael"></param>
- // /// <param name=""></param>
- // public void RedisSubScribe(string subChannael)
- // {
- // ISubscriber sub = redis.GetSubscriber();
- // sub.Subscribe(subChannael, (channel, message) =>
- // {
- // RedisSubMessageEvent?.Invoke(message); //触发事件
- // });
- // }
- // /// <summary>
- // /// 取消订阅
- // /// </summary>
- // /// <param name="channel"></param>
- // public void Unsubscribe(string channel)
- // {
- // ISubscriber sub = redis.GetSubscriber();
- // sub.Unsubscribe(channel);
- // }
- // /// <summary>
- // /// 取消全部订阅
- // /// </summary>
- // public void UnsubscribeAll()
- // {
- // ISubscriber sub = redis.GetSubscriber();
- // sub.UnsubscribeAll();
- // }
- // #endregion
- // #region String类型操作
- // /// <summary>
- // /// 设置指定key 的值(默认第0个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="strValue"></param>
- // /// <returns></returns>
- // public bool InsertStr(string strKey, string strValue)
- // {
- // var db = redis.GetDatabase();
- // return db.StringSet(strKey, strValue);
- // }
- // /// <summary>
- // /// 设置指定key 的值(指定库)
- // /// </summary>
- // /// <param name="strKey"></param>
- // /// <param name="strValue"></param>
- // /// <param name="database"></param>
- // /// <returns></returns>
- // public bool InsertStr(string strKey, string strValue, int database)
- // {
- // var db = redis.GetDatabase(database);
- // return db.StringSet(strKey, strValue);
- // }
- // /// <summary>
- // /// 设置指定key 的值(指定第一个库,指定过期时间)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="strValue"></param>
- // /// <param name="expire"></param>
- // public bool InsertStrExpire(string strKey, string strValue, DateTime expire)
- // {
- // var db = redis.GetDatabase(1);
- // db.StringSet(strKey, strValue);
- // return db.KeyExpire(strKey, expire);
- // }
- // /// <summary>
- // /// 设置指定key 的值(指定第一个库,指定过期分钟数)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="strValue"></param>
- // /// <param name="timespanmin"></param>
- // public bool InsertStrExpire(string strKey, string strValue, int timespanmin)
- // {
- // var db = redis.GetDatabase(1);
- // return db.StringSet(strKey, strValue, TimeSpan.FromMinutes(timespanmin));
- // }
- // /// <summary>
- // /// 设置指定key 的值(指定第一个库,指定过期分钟数)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="strValue"></param>
- // /// <param name="timespanmin"></param>
- // public void InsertStrExpireByDatabaseOne(string strKey, string strValue, int timespanmin)
- // {
- // var db = redis.GetDatabase(1);
- // db.StringSet(strKey, strValue, TimeSpan.FromMinutes(timespanmin));
- // }
- // /// <summary>
- // /// 获取指定 key 的值(默认第0个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <returns></returns>
- // public string ReadStr(string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.StringGet(strKey);
- // }
- // /// <summary>
- // /// 获取指定 key 的值(指定第一个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <returns></returns>
- // public string ReadStrByDatabaseOne(string strKey)
- // {
- // var db = redis.GetDatabase(1);
- // return db.StringGet(strKey);
- // }
- // /// <summary>
- // /// 获取指定 key 的值(指定第一个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="database"></param>
- // /// <returns></returns>
- // public string ReadStrByDatabase(string strKey, int database)
- // {
- // var db = redis.GetDatabase(database);
- // return db.StringGet(strKey);
- // }
- // /// <summary>
- // /// 删除指定key的值(默认第0个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // public bool DeleteStr(string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.KeyDelete(strKey);
- // }
- // /// <summary>
- // /// 删除指定key的值(默认第0个库)
- // /// </summary>
- // /// <param name="strKey">指定key的值</param>
- // /// <param name="database">指定库的值</param>
- // public bool DeleteStrByDatabase(string strKey, int database)
- // {
- // var db = redis.GetDatabase(database);
- // return db.KeyDelete(strKey);
- // }
- // public bool Exist(string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.KeyExists(strKey);
- // }
- // #endregion
- // #region Hash类型操作
- // public bool InsertHash(string tablename, string strKey, string strValue)
- // {
- // var db = redis.GetDatabase();
- // return db.HashSet(tablename, strKey, strValue);
- // }
- // public string ReadHash(string tablename, string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.HashGet(tablename, strKey);
- // }
- // public bool ExistHash(string tablename, string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.HashExists(tablename, strKey);
- // }
- // public bool DeleteHash(string tablename, string strKey)
- // {
- // var db = redis.GetDatabase();
- // return db.HashDelete(tablename, strKey);
- // }
- // #endregion
- //}
- #endregion
- public class RedisHelper : IDisposable
- {
- //连接字符串
- private string _connectionString;
- //实例名称
- private string _instanceName;
- //默认数据库
- private int _defaultDB;
- private ConcurrentDictionary<string, ConnectionMultiplexer> _connections;
- public RedisHelper(string connectionString, string instanceName, int defaultDB = 0)
- {
- _connectionString = connectionString;
- _instanceName = instanceName;
- _defaultDB = defaultDB;
- _connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
- }
- /// <summary>
- /// 获取ConnectionMultiplexer
- /// </summary>
- /// <returns></returns>
- private ConnectionMultiplexer GetConnect()
- {
- return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString));
- }
- /// <summary>
- /// 获取数据库
- /// </summary>
- /// <param name="configName"></param>
- /// <param name="db">默认为0:优先代码的db配置,其次config中的配置</param>
- /// <returns></returns>
- public IDatabase GetDatabase(RedisEnum redisEnum)
- {
- return GetConnect().GetDatabase((int)redisEnum);
- }
- public IServer GetServer(string configName = null, int endPointsIndex = 0)
- {
- var confOption = ConfigurationOptions.Parse(_connectionString);
- return GetConnect().GetServer(confOption.EndPoints[endPointsIndex]);
- }
- public ISubscriber GetSubscriber(string configName = null)
- {
- return GetConnect().GetSubscriber();
- }
- public void Dispose()
- {
- if (_connections != null && _connections.Count > 0)
- {
- foreach (var item in _connections.Values)
- {
- item.Close();
- }
- }
- }
- }
- }
|