代码改变世界

缓存依赖

2012-05-02 16:48  Carl Xing  阅读(234)  评论(0编辑  收藏  举报
       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;
        }