Redis ASP.NET 配置链接

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

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

Redis ASP.NET 配置链接_第1张图片

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


    
    
    
    

 

第二步:就开始配置链接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
                });
              
            }
           

        }

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

        /// 
        /// 查询单挑
        /// 
        /// 
        /// 
        /// 
        public static T Get(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(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog(ex);
            }
           
            return default(T);
        
        }

        /// 
        /// 查询多条数据
        /// 
        /// 
        /// 
        /// 
        public static List GetAll(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>(key);
                        }
                    }

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

        /// 
        /// 删除指定key缓存
        /// 
        /// 
        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;
            }
        }

        /// 
        /// 判断缓存是否存在
        /// 
        /// 
        /// 
        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

你可能感兴趣的:(Redis ASP.NET 配置链接)