zoukankan      html  css  js  c++  java
  • 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键的类 用于方便操作存储已经修改。

  • 相关阅读:
    C语言PRO2
    pro5
    自我介绍
    李喆第5次作业
    李喆的作业
    一个队列类的实现(比delphi自带的速度快70倍)
    关于 IHTMLDocument4 在 Delphi7.0 中不能编译的的解决方法
    高吞吐量的一个日志函数类_用于IOCP (Delphi)
    PostThreadMessage在线程中应用(以多线程网站数据采集为例)
    微软企业库5 加密篇
  • 原文地址:https://www.cnblogs.com/cenwenlin/p/9118058.html
Copyright © 2011-2022 走看看