123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- using System;
- using System.Collections.Generic;
- using Library;
- namespace MySystem
- {
- public class BothdisDbconn
- {
- public readonly static BothdisDbconn Instance = new BothdisDbconn();
- public void SendMq(string key, object obj)
- {
- SetRedisDataList data = new SetRedisDataList()
- {
- key = key,
- val = Newtonsoft.Json.JsonConvert.SerializeObject(obj),
- };
- // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(data), "SetRedisDataList");
- RedisDbconn.Instance.AddList("SetRedisDataList", Newtonsoft.Json.JsonConvert.SerializeObject(data));
- }
- #region 设置单个字段
- public void Set(string key, object value)
- {
- try
- {
- if (RedisDbconn.Instance.Set(key, value))
- {
- RedisDbconn.Instance.Clear(key);
- }
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Set:Error");
- }
- }
- #endregion
- #region 整数累加
- public void AddInt(string key, long value = 1)
- {
- try
- {
- if (RedisDbconn.Instance.AddInt(key, value) > 0)
- {
- RedisDbconn.Instance.Clear(key);
- }
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddInt:Error");
- }
- }
- #endregion
- #region 数字累加
- public void AddNumber(string key, decimal value = 1)
- {
- try
- {
- if (RedisDbconn.Instance.AddNumber(key, value) > 0)
- {
- RedisDbconn.Instance.Clear(key);
- }
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddNumber:Error");
- }
- }
- #endregion
- #region 获取单个字段
- public T Get<T>(string key)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- T obj = RedisDbconn.Instance.Get<T>(key);
- if (obj != null)
- {
- return obj;
- }
- }
- T newobj = TendisDbconn.Instance.Get<T>(key);
- if (newobj != null)
- {
- RedisDbconn.Instance.Set(key, newobj);
- RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
- }
- return newobj;
- }
- public decimal GetNumber(string key)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- decimal obj = RedisDbconn.Instance.Get<decimal>(key);
- if (obj > 0)
- {
- return obj;
- }
- }
- decimal newobj = TendisDbconn.Instance.Get<decimal>(key);
- if (newobj > 0)
- {
- RedisDbconn.Instance.Clear(key);
- RedisDbconn.Instance.AddNumber(key, newobj);
- RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
- }
- return 0;
- }
- public int GetInt(string key)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- int obj = RedisDbconn.Instance.Get<int>(key);
- if (obj > 0)
- {
- return obj;
- }
- }
- int newobj = TendisDbconn.Instance.Get<int>(key);
- if (newobj > 0)
- {
- RedisDbconn.Instance.Clear(key);
- RedisDbconn.Instance.AddInt(key, newobj);
- RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
- }
- return 0;
- }
- #endregion
- #region 设置散列字段
- public void HSet(string key, string field, object value)
- {
- try
- {
- RedisDbconn.Instance.HSet(key, field, value);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HSet:Error");
- }
- RedisDbconn.Instance.HSet(key, field, value);
- }
- #endregion
- #region 散列整数累加
- public void HAddInt(string key, string field, long value = 1)
- {
- try
- {
- RedisDbconn.Instance.HAddInt(key, field, value);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddInt:Error");
- }
- RedisDbconn.Instance.HAddInt(key, field, value);
- }
- #endregion
- #region 散列数字累加
- public void HAddNumber(string key, string field, decimal value = 1)
- {
- try
- {
- RedisDbconn.Instance.HAddNumber(key, field, value);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddNumber:Error");
- }
- RedisDbconn.Instance.HAddNumber(key, field, value);
- }
- #endregion
- #region 获取散列元素
- public T HGet<T>(string key, string field)
- {
- if (RedisDbconn.Instance.HExists(key, field))
- {
- T obj = RedisDbconn.Instance.HGet<T>(key, field);
- if (obj != null)
- {
- return obj;
- }
- }
- T newobj = RedisDbconn.Instance.HGet<T>(key, field);
- // if (newobj != null)
- // {
- // RedisDbconn.Instance.HSet(key, field, newobj);
- // }
- return newobj;
- }
- #endregion
- #region 获取散列所有元素
- public Dictionary<string, T> HGetAll<T>(string key)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- Dictionary<string, T> obj = RedisDbconn.Instance.HGetAll<T>(key);
- if (obj != null)
- {
- return obj;
- }
- }
- Dictionary<string, T> newobj = RedisDbconn.Instance.HGetAll<T>(key);
- // if (newobj != null)
- // {
- // foreach (string sub in newobj.Keys)
- // {
- // RedisDbconn.Instance.HSet(key, sub, newobj[sub]);
- // }
- // }
- return newobj;
- }
- #endregion
- #region 添加集合对象
- public void SAdd(string key, object value)
- {
- try
- {
- RedisDbconn.Instance.SAdd(key, value);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd:Error");
- }
- RedisDbconn.Instance.SAdd(key, value);
- }
- public void SAdd(string key, object[] value)
- {
- try
- {
- RedisDbconn.Instance.SAdd(key, value);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd:Error");
- }
- RedisDbconn.Instance.SAdd(key, value);
- }
- #endregion
- #region 获取集合对象
- public T[] SGetList<T>(string key)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- T[] obj = RedisDbconn.Instance.SGetList<T>(key);
- if (obj != null)
- {
- if (obj.Length > 0)
- {
- return obj;
- }
- }
- }
- T[] newobj = RedisDbconn.Instance.SGetList<T>(key);
- // if (newobj != null)
- // {
- // foreach (T sub in newobj)
- // {
- // RedisDbconn.Instance.SAdd(key, sub);
- // }
- // }
- return newobj;
- }
- #endregion
- #region 添加列表对象
- public void AddList(string key, object value)
- {
- try
- {
- if (RedisDbconn.Instance.AddList(key, value) > 0)
- {
- RedisDbconn.Instance.Clear(key);
- }
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList:Error");
- }
- }
- public void AddList(string key, object[] value)
- {
- try
- {
- if (RedisDbconn.Instance.AddList(key, value) > 0)
- {
- RedisDbconn.Instance.Clear(key);
- }
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList:Error");
- }
- }
- #endregion
- #region 获取列表
- public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- List<T> list = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
- if (list.Count > 0)
- {
- return list;
- }
- }
- List<T> tlist = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
- if (tlist.Count > 0)
- {
- RedisDbconn.Instance.Clear(key);
- object[] tmplist = new object[tlist.Count];
- for (int i = 0; i < tlist.Count; i++)
- {
- tmplist[i] = tlist[i];
- }
- RedisDbconn.Instance.AddList(key, tmplist);
- }
- return tlist;
- }
- #endregion
- #region 添加排序列表对象
- public void AddSort(string key, object value, decimal score)
- {
- try
- {
- RedisDbconn.Instance.AddSort(key, value, score);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- value = value,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddSort:Error");
- }
- RedisDbconn.Instance.AddSort(key, value, score);
- int Expired = 60 * 60 * 24 * 180;
- RedisDbconn.Instance.SetExpire(key, Expired);
- }
- #endregion
- #region 获取排序列表
- public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- List<T> list = RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
- if (list.Count > 0)
- {
- return list;
- }
- }
- return RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
- }
- public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- if (RedisDbconn.Instance.Exists(key))
- {
- List<T> list = RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
- if (list.Count > 0)
- {
- return list;
- }
- }
- return RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
- }
- #endregion
- public void Remove(string key, long start, long end)
- {
- try
- {
- RedisDbconn.Instance.AddSort(key, start, end);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = key,
- start = start,
- end = end,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Remove:Error");
- }
- RedisDbconn.Instance.Remove(key, start, end);
- }
- public void RemoveTop(string key, long count)
- {
- Remove(key, count, RedisDbconn.Instance.Count(key) - 1); ;
- }
- public void Clear(string pattern)
- {
- try
- {
- RedisDbconn.Instance.Clear(pattern);
- RedisDbconn.Instance.Clear(pattern);
- }
- catch (Exception ex)
- {
- TendisErr err = new TendisErr()
- {
- key = pattern,
- errMsg = ex.ToString(),
- };
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Clear:Error");
- }
- }
- }
- }
|