123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System.Collections.Generic;
- using Library;
- namespace MySystem
- {
- public class RedisDbconn
- {
- public readonly static RedisDbconn Instance = new RedisDbconn();
- private RedisDbconn()
- {
- var csredis = new CSRedis.CSRedisClient(ConfigurationManager.AppSettings["RedisConnStr"].ToString());
- //初始化 RedisHelper
- RedisHelper.Initialization(csredis);
- }
- #region 设置单个字段
- public bool Set(string key, object value)
- {
- return RedisHelper.Set(key, value);
- // return false;
- }
- #endregion
- #region 整数累加
- public long AddInt(string key, long value = 1)
- {
- return RedisHelper.IncrBy(key, value);
- // return 0;
- }
- #endregion
- #region 数字累加
- public decimal AddNumber(string key, decimal value = 1)
- {
- return RedisHelper.IncrByFloat(key, value);
- // return 0;
- }
- #endregion
- #region 获取单个字段
- public T Get<T>(string key)
- {
- return RedisHelper.Get<T>(key);
- }
- #endregion
- #region 设置散列字段
- public bool HSet(string key, string field, object value)
- {
- return RedisHelper.HSet(key, field, value);
- // return false;
- }
- #endregion
- #region 散列整数累加
- public long HAddInt(string key, string field, long value = 1)
- {
- return RedisHelper.HIncrBy(key, field, value);
- // return 0;
- }
- #endregion
- #region 散列数字累加
- public decimal HAddNumber(string key, string field, decimal value = 1)
- {
- return RedisHelper.HIncrByFloat(key, field, value);
- // return 0;
- }
- #endregion
- #region 获取散列元素
- public T HGet<T>(string key, string field)
- {
- return RedisHelper.HGet<T>(key, field);
- }
- #endregion
- #region 获取散列所有元素
- public Dictionary<string, T> HGetAll<T>(string key)
- {
- return RedisHelper.HGetAll<T>(key);
- }
- #endregion
- #region 添加列表对象
- public long AddList(string key, object value)
- {
- return RedisHelper.LPush(key, value);
- // return 0;
- }
- public long AddList(string key, object[] value)
- {
- return RedisHelper.LPush(key, value);
- // return 0;
- }
- public long AddRightList(string key, object value)
- {
- return RedisHelper.RPush(key, value);
- // return 0;
- }
- public T RPop<T>(string key)
- {
- return RedisHelper.RPop<T>(key);
- }
- #endregion
- #region 添加集合对象
- public long SAdd(string key, object value)
- {
- return RedisHelper.SAdd(key, value);
- // return 0;
- }
- public long SAdd(string key, object[] value)
- {
- return RedisHelper.SAdd(key, value);
- // return 0;
- }
- #endregion
- #region 获取集合对象
- public T[] SGetList<T>(string key)
- {
- return RedisHelper.SMembers<T>(key);
- }
- #endregion
- #region 修改列表对象
- public bool SetList(string key, int index, object value)
- {
- return RedisHelper.LSet(key, index, value);
- // return false;
- }
- #endregion
- #region 获取列表
- public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- int start = (pageNum - 1) * pageSize;
- int end = start + pageSize - 1;
- string[] list = RedisHelper.LRange(key, start, end);
- List<T> lists = new List<T>();
- foreach (string record in list)
- {
- lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
- }
- return lists;
- }
- #endregion
- #region 移除列表对象
- public long DelFromList(string key, object value, int count = 1)
- {
- return RedisHelper.LRem(key, count, value);
- }
- #endregion
- #region 移动列表元素位置
- public T RPopLPush<T>(string key, string tokey)
- {
- return RedisHelper.RPopLPush<T>(key, tokey);
- }
- #endregion
- #region 添加排序列表对象
- public long AddSort(string key, object value, decimal score)
- {
- return RedisHelper.ZAdd(key, (score, value));
- // return 0;
- }
- #endregion
- #region 获取排序列表
- public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- int start = (pageNum - 1) * pageSize;
- int end = start + pageSize;
- string[] list = RedisHelper.ZRangeByScore(key, start, end);
- List<T> lists = new List<T>();
- foreach (string record in list)
- {
- lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
- }
- return lists;
- }
- public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
- {
- int start = (pageNum - 1) * pageSize;
- int end = start + pageSize;
- string[] list = RedisHelper.ZRevRangeByScore(key, start, end);
- List<T> lists = new List<T>();
- foreach (string record in list)
- {
- lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
- }
- return lists;
- }
- #endregion
- public bool Remove(string key, long start, long end)
- {
- return RedisHelper.LTrim(key, start, end);
- }
- public bool RemoveTop(string key, long count)
- {
- return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
- }
- public long Count(string key)
- {
- return RedisHelper.LLen(key);
- }
- public void Clear(string pattern)
- {
- string[] keys = RedisHelper.Keys(pattern);
- RedisHelper.Del(keys);
- }
- public string[] GetKeys(string pattern)
- {
- string[] keys = RedisHelper.Keys(pattern);
- return keys;
- }
- public void SetExpire(string key, int expire)
- {
- RedisHelper.Expire(key, expire); //秒为单位
- }
- }
- }
|