| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- 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类型操作
- /// <summary>
- /// set or update the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool SetStringValue(string key, string value)
- {
-
- return Db.StringSet(key, value);
- }
- /// <summary>
- /// 保存单个key value
- /// </summary>
- /// <param name="key">Redis Key</param>
- /// <param name="value">保存的值</param>
- /// <param name="expiry">过期时间</param>
- /// <returns></returns>
- public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
- {
-
- return Db.StringSet(key, value, expiry);
- }
- /// <summary>
- /// 保存一个对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <param name="obj"></param>
- /// <returns></returns>
- public bool SetStringKey<T>(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);
- }
- /// <summary>
- /// 获取一个key的对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public T GetStringKey<T>(string key) where T : class
- {
- var res = Db.StringGet(key);
- if (!res.IsNull)
- return JsonConvert.DeserializeObject<T>(res);
- return null;
- }
- /// <summary>
- /// get the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string GetStringValue(string key)
- {
- return Db.StringGet(key);
- }
- /// <summary>
- /// Delete the value for string key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool DeleteStringKey(string key)
- {
- return Db.KeyDelete(key);
- }
- #endregion
- #region 哈希类型操作
- /// <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);
- }
- /// <summary>
- /// 保存一个集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key">Redis Key</param>
- /// <param name="list">数据集合</param>
- /// <param name="getModelId"></param>
- public void HashSet<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>
- /// 获取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)
- // {
- // T t;
- // if (JsonConvert.DeserializeObject<T>(item.Value, out t))
- // {
- // result.Add(t);
- // }
- // }
- // }
- // return result;
- // //result =JsonHelper.DeserializeJsonToList<T>(arr.ToString());
- // //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;
- // if (JsonConvert.DeserializeObject<T>(result, out t))
- // {
- // return t;
- // }
- // 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);
- }
- #endregion
- public void Dispose()
- {
- Redis?.Dispose();
- }
- }
- }
|