using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using MqttMsgServer; namespace MqttMsgServer.Tools { public class RedisHelper: IDisposable { private readonly string _redisConnectionStr = AppSetting.GetValue("redis"); private IDatabase Db { get; set; } private static Object lockObj = new Object(); //public RedisHelper() //{ // redis = ConnectionMultiplexer.Connect(RedisConnectionStr); // db = redis.GetDatabase(); //} public static ConnectionMultiplexer Redis { get; private set; } static RedisHelper() { lock (lockObj) { Instance = new RedisHelper(); } } private RedisHelper() { if (Redis == null) { Redis = ConnectionMultiplexer.Connect(_redisConnectionStr); Db = Redis.GetDatabase(); } } public static RedisHelper Instance { get; } #region string类型操作 /// /// set or update the value for string key /// /// /// /// public bool SetStringValue(string key, string value) { return Db.StringSet(key, value); } /// /// 保存单个key value /// /// Redis Key /// 保存的值 /// 过期时间 /// public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return Db.StringSet(key, value, expiry); } /// /// 保存一个对象 /// /// /// /// /// public bool SetStringKey(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) { //LogHelper.LogInfo(this, Marshal.SizeOf(Db)); string json = JsonConvert.SerializeObject(obj); return Db.StringSet(key, json, expiry); } /// /// 获取一个key的对象 /// /// /// /// public T GetStringKey(string key) where T : class { var res = Db.StringGet(key); if (!res.IsNull) return JsonConvert.DeserializeObject(res); return null; } /// /// get the value for string key /// /// /// public string GetStringValue(string key) { return Db.StringGet(key); } /// /// Delete the value for string key /// /// /// public bool DeleteStringKey(string key) { return Db.KeyDelete(key); } #endregion #region 哈希类型操作 /// /// set or update the HashValue for string key /// /// /// /// /// public bool SetHashValue(string key, string hashkey, string value) { return Db.HashSet(key, hashkey, value); } /// /// set or update the HashValue for string key /// /// /// /// /// defined class /// public bool SetHashValue(String key, string hashkey, T t) where T : class { var json = JsonConvert.SerializeObject(t); return Db.HashSet(key, hashkey, json); } /// /// 保存一个集合 /// /// /// Redis Key /// 数据集合 /// public void HashSet(string key, List list, Func getModelId) { List listHashEntry = new List(); foreach (var item in list) { string json = JsonConvert.SerializeObject(item); listHashEntry.Add(new HashEntry(getModelId(item), json)); } Db.HashSet(key, listHashEntry.ToArray()); } /// /// 获取hashkey所有的值 /// /// /// /// //public List HashGetAll(string key) where T : class //{ // List result = new List(); // HashEntry[] arr = db.HashGetAll(key); // foreach (var item in arr) // { // if (!item.Value.IsNullOrEmpty) // { // T t; // if (JsonConvert.DeserializeObject(item.Value, out t)) // { // result.Add(t); // } // } // } // return result; // //result =JsonHelper.DeserializeJsonToList(arr.ToString()); // //return result; //} /// /// get the HashValue for string key and hashkey /// /// Represents a key that can be stored in redis /// /// public RedisValue GetHashValue(string key, string hashkey) { RedisValue result = Db.HashGet(key, hashkey); return result; } /// /// get the HashValue for string key and hashkey /// /// Represents a key that can be stored in redis /// /// //public T GetHashValue(string key, string hashkey) where T : class //{ // RedisValue result = db.HashGet(key, hashkey); // if (string.IsNullOrEmpty(result)) // { // return null; // } // T t; // if (JsonConvert.DeserializeObject(result, out t)) // { // return t; // } // return null; //} /// /// delete the HashValue for string key and hashkey /// /// /// /// public bool DeleteHashValue(string key, string hashkey) { return Db.HashDelete(key, hashkey); } #endregion public void Dispose() { Redis?.Dispose(); } } }