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;

            }

  • 相关阅读:
    brctl 使用说明
    Flash文件系统介绍和平台采用squashfs+ubifs原因
    xargs
    svn的常用命令
    shell中的if用法
    shell中单双引号
    删除文件中的 ^M 字符
    博客园添加live2d看板娘
    IOS 自定义转场动画
    rails
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3134730.html
Copyright © 2011-2022 走看看