zoukankan      html  css  js  c++  java
  • 设置缓存失效的三种方法

    案例:

    效果当在规定时间内访问一个网页时,网页上的内容咱是保持不变,时间一过,网页上的内容就会重新访问服务器,获取数据。

    前台:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="缓存.WebForm1" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>

    后台:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Cache["news"] == null)
                    {
                        DataTable dt = LoadData();


                        //将缓存和外部文件关联,外部文件以改变,缓存立即失效
                        //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);
                        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["Sqlserver"].ConnectionString;
                SqlConnection conn = new SqlConnection(strcon);
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "pro_FenYe";
                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;
            }
        }

    web.config中的设置时间的代码:

      <system.web>
            <compilation debug="true" targetFramework="4.0" />
          <caching>
            <sqlCacheDependency pollTime="500">
              <databases>
                <add name="sqldependency" connectionStringName="Sqlserver"/>
              </databases>
            </sqlCacheDependency>
          </caching>
        </system.web>

  • 相关阅读:
    修复PLSQL Developer 与 Office 2010的集成导出Excel 功能
    Using svn in CLI with Batch
    mysql 备份数据库 mysqldump
    Red Hat 5.8 CentOS 6.5 共用 输入法
    HP 4411s Install Red Hat Enterprise Linux 5.8) Wireless Driver
    变更RHEL(Red Hat Enterprise Linux 5.8)更新源使之自动更新
    RedHat 5.6 问题简记
    Weblogic 9.2和10.3 改密码 一站完成
    ExtJS Tab里放Grid高度自适应问题,官方Perfect方案。
    文件和目录之utime函数
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/3134501.html
Copyright © 2011-2022 走看看