Redis ASP.NET 配置链接

对于安装Redis后 很是不明白如何建立Redis 和 .net 的链接配置 于是查找了很多的资料

首先第一步:安装ASP.NET  NuGet 包 (ServiceStack.Redis) 安装好后 查看引用如下:

这时候 首先在 ASP.NET Web.Config中<appSettings>节点中配置如下

<!--Redis 配置-->
    <add key="redis_server_write" value="Admin2018@127.0.0.1:6379" />
    <add key="redis_server_read" value="Admin2018@127.0.0.1:6379" />
    <add key="redis_max_read_pool" value="3" />
    <add key="redis_max_write_pool" value="1" />
<!--Redis 配置-->

第二步:就开始配置链接Redis的链接了:

  1>自定义创建一个RedisCacheHelper的配置类,代码如下:

public class RedisCacheHelper : ConfigurationSection
    {
        //读取Redis接口
        private static string GetRedis = ConfigurationManager.AppSettings["redis_server_read"];
        //读取数量
        private static int GetRedisNum =Convert.ToInt32(ConfigurationManager.AppSettings["redis_max_read_pool"]);
        //写入Redis接口
        private static string SetRedis = ConfigurationManager.AppSettings["redis_server_write"];
        //写入数量
        private static int SetRedisNum = Convert.ToInt32(ConfigurationManager.AppSettings["redis_max_write_pool"]);

        //定义连接池
        private static readonly PooledRedisClientManager Pool = null;

        //定义构造函数
         static RedisCacheHelper()
        {
            string [] GetRedisHost = GetRedis.Split(',');
            string [] SetRedisHost = SetRedis.Split(',');
            if(GetRedisHost.Length>0&&SetRedisHost.Length>0)
            {
                Pool = new PooledRedisClientManager(GetRedisHost, SetRedisHost, new RedisClientManagerConfig()
                {
                    MaxWritePoolSize = SetRedisNum,
                    MaxReadPoolSize = GetRedisNum,
                    AutoStart = true
                });
              
            }
           

        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Add<T>(string key,List<T> value)
        {
            if (value == null)
                return;
            try
            {
                if (Pool != null)
                {
                    using (var r = Pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Set<List<T>>(key, value);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog(ex);
            }
            
        }

        /// <summary>
        /// 查询单挑
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            if(key==null)
                return default(T);
            try
            {
                if (Pool != null)
                {
                    using (var r = Pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            return r.Get<T>(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog(ex);
            }
           
            return default(T);
        
        }

        /// <summary>
        /// 查询多条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<T> GetAll<T>(string key)
        {
            if (key == null)
                return null;
            try
            {
                if (Pool != null)
                {
                    using (var r = Pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            return r.Get<List<T>>(key);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog(ex);
            }
            
            return null;
        }

        /// <summary>
        /// 删除指定key缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            if (key == null)
                return;
            try
            {
                if (Pool != null)
                {
                    using (var r = Pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Remove(key);
                        }
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// 判断缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exists(string key)
        {
            if (key == null)
                return false;
            try
            {
                if (Pool != null)
                {
                    using (var r = Pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            return  r.ContainsKey(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog(ex);
            }
            return false;
        }
    }

  

 在调用的时候 我们还可以在自定义一个Key键的类 用于方便操作存储已经修改。

原文地址:https://www.cnblogs.com/cenwenlin/p/9118058.html