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;
            }


    
    
  • 相关阅读:
    jdbc的数据库驱动类DriverManager.getConnection()详解
    BootStrap_table.js 学习
    Oracle 练习
    Oracle游标
    Oracle流程控制语句
    Oracle定义变量、常量
    Oracle中的数据类型
    初识Oracle中的正则表达式
    Oracle primary key&foreign key
    oracle Extract 函数
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/2479269.html
Copyright © 2011-2022 走看看