zoukankan      html  css  js  c++  java
  • 缓存依赖

           public List<MenuEntity> GetMenus()
            {
                List<MenuEntity> menuList = null;
    
                SqlCacheDependency sqlDep;
                if (HttpContext.Current.Cache["menu"] == null)
                {
    
                    menuList = new List<MenuEntity>();
                    using (SqlConnection conn = new SqlConnection(DalCommon.connStr))
                    {
                        conn.Open();
                        string sql = "select * from tbmenu";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        //在数据库中需要执行这两句启用broker
                        //ALTER DATABASE MvcDB SET NEW_BROKER WITH ROLLBACK IMMEDIATE;  
                        //ALTER DATABASE MvcDB SET ENABLE_BROKER; 
                        SqlDependency.Start(DalCommon.connStr);
                        sqlDep = new SqlCacheDependency(cmd);
                        SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        sqlda.Fill(dt);
                        conn.Close();
                        foreach (DataRow dr in dt.Rows)
                        {
                            menuList.Add(new MenuEntity(Convert.ToInt32(dr["menuid"]),
                                Convert.ToInt32(dr["parentid"]),
                                dr["menuname"].ToString(),
                                dr["menuurl"].ToString()));
                        }
    
                    }
                    //HttpContext.Current.Cache.Insert("menu", menuList, sqlDep, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration);
                    HttpContext.Current.Cache.Add("menu", menuList, sqlDep, DateTime.Now.AddSeconds(120), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
                else
                {
                    menuList = HttpContext.Current.Cache["menu"] as List<MenuEntity>;
                }
                return menuList;
            }


    另外一种方式:
    1、配置web.config
     <system.web>
        <caching>
          <sqlCacheDependency enabled="true">
            <databases>
              <add connectionStringName="DbConn" name="DbCache" pollTime="700"/>
            </databases>
          </sqlCacheDependency>
        </caching>
    代码如下:
    public List<MenuEntity> GetMenus()
            {
                List<MenuEntity> menuList = null;
    
                SqlCacheDependency sqlDep = null;
                if (HttpContext.Current.Cache["menu"] == null)
                {
    
                    menuList = new List<MenuEntity>();
                    using (SqlConnection conn = new SqlConnection(DalCommon.connStr))
                    {
                        conn.Open();
                        string sql = "select * from tbmenu";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        try
                        {
                            sqlDep = new SqlCacheDependency("DbCache", "tbMenu");
                        }
                        catch
                        {
                            //这句建立数据表AspNet_SqlCacheTablesForChangeNotification
                            SqlCacheDependencyAdmin.EnableNotifications(DalCommon.connStr);
                            SqlCacheDependencyAdmin.EnableTableForNotifications(DalCommon.connStr, "tbMenu");
                        }
                        SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        sqlda.Fill(dt);
                        conn.Close();
                        foreach (DataRow dr in dt.Rows)
                        {
                            menuList.Add(new MenuEntity(Convert.ToInt32(dr["menuid"]),
                                Convert.ToInt32(dr["parentid"]),
                                dr["menuname"].ToString(),
                                dr["menuurl"].ToString()));
                        }
    
                    }
                    //HttpContext.Current.Cache.Insert("menu", menuList, sqlDep, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration);
                    HttpContext.Current.Cache.Add("menu", menuList, sqlDep, DateTime.Now.AddSeconds(120), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
                else
                {
                    menuList = HttpContext.Current.Cache["menu"] as List<MenuEntity>;
                }
                return menuList;
            }


    
    
  • 相关阅读:
    【web前端面试题整理03】来看一点CSS相关的吧
    【web前端面试题整理02】前端面试题第二弹袭来,接招!
    【web前端优化之图片模糊到清晰】看我QQ空间如何显示相片
    【web前端面试题整理01】各位加班累了吧,来做点前端面试题吧
    【javascript激增的思考03】MVVM与Knockout
    【javascript激增的思考02】模块化与MVC
    【javascript激增的思考01】模块化编程
    【position也可以很复杂】当弹出层遇上了鼠标定位(下)
    【position也可以很复杂】当弹出层遇上了鼠标定位(上)
    iOS开发拓展篇—音乐的播放
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/2479269.html
Copyright © 2011-2022 走看看