码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作Redis类

时间:2021-01-13 10:54:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:add   sys   ring   try   堆栈   client   param   core   push   

    public class RedisHelper
    {
        static RedisClient client;
        static RedisHelper()
        {
            client = new RedisClient("127.0.0.1", 6379);
        }

        /// <summary>
        /// 清空数据库缓存
        /// </summary>
        public static void FlushRedis()
        {
            client.FlushAll();
        }

        /// <summary>
        /// 获取redis所有key
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllKeys()
        {
            try
            {
                return client.GetAllKeys();
            }
            catch (System.Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool KeyExists(string key)
        {
            try
            {
                return client.Exists(key) == 1 ? true : false;
            }
            catch (System.Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除key
        /// </summary>
        /// <param name="key"></param>
        public static void KeyDel(string key)
        {
            try
            {
                client.Del(key);
            }
            catch (System.Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取key类型
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string KeyType(string key)
        {
            try
            {
                return client.Type(key);
            }
            catch (System.Exception)
            {

                throw;
            }
        }

        #region 哈希集合
        /// <summary>
        /// 添加值到哈希集合
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddHash(string ID,string key,string value)
        {
            try
            {
                client.SetEntryInHash(ID, key, value);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取哈希集合的key
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static List<string> GetHashKeys(string ID)
        {
            try
            {
                return client.GetHashKeys(ID);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取哈希集合的value
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static List<string> GetHashValues(string ID)
        {
            try
            {
                return client.GetHashValues(ID);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取哈希集合指定ID、key的value
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetHashValue(string ID,string key)
        {
            try
            {
                return client.GetValueFromHash(ID, key);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        #endregion
        #region 无序集合
        /// <summary>
        /// 添加值到数据集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddSet(string key,string value)
        {
            try
            {
                client.AddItemToSet(key, value);
            }
            catch
            {

            }
        }
        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static HashSet<string> GetSet(string key)
        {
            try
            {
                return client.GetAllItemsFromSet(key);
            }
            catch
            {
                string[] strMsg = new string[0];
                HashSet<string> hSetMsg = new HashSet<string>(strMsg);
                return hSetMsg;
            }
        }
        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key1"></param>
        /// <param name="key2"></param>
        /// <returns></returns>
        public static HashSet<string> GetSet(string type,string key1,string key2)
        {
            try
            {
                HashSet<string> hSet = new HashSet<string>();
                switch (type.ToLower())
                {
                    case "union"://并集
                        hSet = client.GetUnionFromSets(new string[] { key1, key2 });
                        break;
                    case "intersect"://交集
                        hSet = client.GetIntersectFromSets(new string[] { key1, key2 });
                        break;
                    case "except"://差集,存在于key1,不存在于key2
                        hSet = client.GetDifferencesFromSet(key1, new string[] { key2 });
                        break;
                    default:
                        break;
                }
                return hSet;
            }
            catch
            {
                string[] strMsg = new string[0];
                HashSet<string> hSetMsg = new HashSet<string>(strMsg);
                return hSetMsg;
            }
        }
        #endregion
        #region 有序集合
        /// <summary>
        /// 添加值到有序集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddZset(string key,string value,double score)
        {
            try
            {
                client.AddItemToSortedSet(key, value, score);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取有序集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> GetZset(string key)
        {
            try
            {
                return client.GetAllItemsFromSortedSet(key);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 按score排序获取前n位的有序集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static List<string> GetZset(string key,int i)
        {
            try
            {
                var value = client.GetRangeWithScoresFromSortedSet(key, 0, i);
                List<string> llist = new List<string>();
                foreach (var item in value)
                {
                    llist.Add(item.Key);
                }
                return llist;
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        #endregion
        #region 字符串
        /// <summary>
        /// 添加值到字符串
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddString(string key,string value)
        {
            try
            {
                client.Set<string>(key, value);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetString(string key)
        {
            try
            {
                return client.Get<string>(key);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        #endregion
        #region 列表堆栈
        /// <summary>
        /// 添加值入堆:先进先出
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddListQueue(string key,string value)
        {
            try
            {
                client.EnqueueItemOnList(key, value);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加值入栈:先进后出
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddListStack(string key,string value)
        {
            try
            {
                client.PushItemToList(key, value);
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> GetList(string key)
        {
            List<string> lList = new List<string>();
            try
            {
                var p = client.GetListCount(key);
                for (int i = 0; i < p; i++)
                {
                    lList.Add(client.PopItemFromList(key));
                }
                return lList;
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        #endregion
    }

 

C#操作Redis类

标签:add   sys   ring   try   堆栈   client   param   core   push   

原文地址:https://www.cnblogs.com/blossomwei/p/14262175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!