zoukankan      html  css  js  c++  java
  • 缓存的有几种方式

    前台代码:

    <div>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
        </div>

    后台代码:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Cache["news"] == null)
                    {
                        DataTable dt = LoadData();
                        //Cache.Insert("news", dt);//将datatable添加到缓存中

                        //将缓存和外部文件相关联,外部文件以改变,缓存即失效
                        //Cache.Insert("news", dt, new CacheDependency(@"d:\cache.txt"));

                        //为缓存设定一个绝对时间,让缓存在这个时间到的时候失效
                        //Cache.Insert("news", dt, null, DateTime.Now.AddSeconds(20),TimeSpan.Zero);
                       // Cache.Insert("news", dt, null, DateTime.MaxValue, TimeSpan.FromSeconds(30));

                        Cache.Insert("news", dt, new SqlCacheDependency("sqlcache", "T_News1"));

                        //1)使用数据库安装工具aspnetsql,向数据库添加缓存依赖。让sqlserver支持asp.net缓存
                        //执行命令:cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
                        //执行命令:aspnet_regsql -C "Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111" -ed -et -t "T_News1"
                        // 注:为News数据库中的T_News1表启用缓存依赖项,即此表中的数据更改之后缓存失效
                        //    2)在web.config的sys.web节中添加依赖信息
                        //<caching>
                        //    <sqlCacheDependency enabled="true" pollTime="5000">
                        //      <databases>
                        //        <add name="sqlcache" connectionStringName="strcon"/>
                        //      </databases>
                        //    </sqlCacheDependency>
                        //  </caching>
                        //    注:其中 polltime表示更新频率,即每隔多长时间去数据重新查询一次来更新


                        this.GridView1.DataSource = dt;
                        this.GridView1.DataBind();
                    }
                    else
                    {
                        DataTable dt = Cache["news"] as DataTable;
                        this.GridView1.DataSource = dt;
                        this.GridView1.DataBind();
                    }

                }
            }

            private DataTable LoadData()
            {
                string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
                SqlConnection conn = new SqlConnection(strcon);
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "Pro_TNews1Paging";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pagesize", 500);
                cmd.Parameters.AddWithValue("@pageindex", 1);
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                cmd.Dispose();
                conn.Dispose();
                return dt;

            }

  • 相关阅读:
    oracle11g数据库安装
    WIN7+QT5.2.0 连接oracle11g问题及解决方法
    WIN7+Qt5.2.0连接oracle数据库的oci驱动的编译
    Qt编译Oracle OCI驱动
    Qt 中 Oracle 数据库 QOCI 驱动问题及解决
    qt QThread
    Qt之模型/视图(自定义风格)
    Qt之模型/视图(实时更新数据)
    Qt之模型/视图(委托)
    AE地图查询
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3134730.html
Copyright © 2011-2022 走看看