• .NET中的缓存


    构建高性能的应用程序的非常重要一项就是使用缓存。使用缓存可以避免重新从读取服务器端读取数据,节省数据从客户端到服务器间往返的时间,同时也减轻了服务器数据存取的压力。如果客户端非常频繁地读取服务器上的数据,比如生成报表,并且服务器每次获取这些数据都经过复杂的处理逻辑,那么就可能非常有必要使用缓存。应用程序需要.NET2.0中提供了两种不同方式的缓存:页面输出缓存和应用程序数据缓存。 

    页面输出缓存

    我们知道asp.net服务器控件每次生成数据都要经过一个复杂的生存周期过程,参见[服务器端控件页面生存周期]。使用页面输出缓存就是指内存中缓存asp.net页面的内容,这样每次需要这些内容都无需重新生成,取而代之的是从内存中直接读取,这样节省了asp.net页面控件生成这些内容的时间,从而大大地提高了应用程序的性能。如果客户访问的这些页面的内容不经常变化,这些页面的访问量较大,那么就非常适宜使用页面输出缓存。

    我们可以设置两种不同类型的页面输出缓存:全局页面缓存和页面片断缓存。全局页面缓存是指将整个页面的内容都缓存在内存中供客户端调用。而页面片断缓存是指在内存中缓存部分页面的内容,而其他的部分是动态重新生成的。

    页面片断缓存有一种较为特殊的情形是,除了页面的某一局部内容不进行缓存,其他整个页面是缓存起来的,这种情形叫做Post-Cache Substitution。比如,登陆后在页面某个部分显示用户名处,我们就有可能用到这种情形。

     使用页面输出缓存

    可以通过两种方式设置使用缓存。

    通过web.config配置缓存如下:

    <System.web>

             <caching>

                    <outputCache enableOutputCache="true"

                     enableFragmentCache="true"

                     sendCacheControlHeader="true"

                     omitVaryStar="false">

                    </outputCache>

             <caching>

    </system.web>

    在页面中配置使用缓存如下:

    如何从缓存中读取数据

    要从缓存中读取数据,需要先判断一下缓存的键值是否存在,因为缓存中存储的信息是不稳定的,可能它已经被ASP.NET移去。因此推荐采用如下方式读取缓存的内容:

    string cachedString;

    if (Cache["CacheItem"] != null)

    {

        cachedString = (string)Cache["CacheItem"];

    }

    else

    {

        Cache.Insert("CacheItem", "Hello, World.");   // 设置缓存

        cachedString = (string)Cache["CacheItem"];    // 读取缓存

    }

     

     

    将数据添加到缓存中

     

    string str = "a";


    1。通过指定其键和值将项添加到缓存中 
    Cache["txt"] = str;

    2.通过使用 Insert(重载Insert方法)方法将项添加到缓存中 

    Cache.Insert("txt", 
    str);

    全局页面缓存 

    下面例子表示缓存时间10秒,就是说每隔10秒读取一次系统时间。

    对VaryByParam参数的一点说明:

    VaryByParam 属性功能十分强大,它允许用户控件作者指示 ASP.NET 在服务器上缓存/存储输出缓存区域的多个实例。例如,前一个用户控件的宿主页的下列 URL 缓存用户控件内容的单独实例。

    http://localhost/mypage.aspx?categoryid=foo&selectedid=0

    http://localhost/mypage.aspx?categoryid=foo&selectedid=1

    用户控件内的逻辑因此能够根据提供的参数动态生成不同的内容(单独缓存)。

    除了支持 VaryByParam 属性外,片段缓存还支持 VaryByControl 属性。VaryByParam 属性基于使用 POST 或 GET 发送的名称/值对改变缓存结果,而 VaryByControl 属性则通过用户控件中的控件改变缓存片段。例如:

    <%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="Category" %>

    注意:与输出缓存页相似,即使不使用 VaryByParam,也要求显式使用它。

    例如,下列指令指示 ASP.NET 输出缓存用户控件 120 秒,并使用“CategoryID”和“SelectedID”查询字符串或窗体发布参数改变缓存。

    <%@ OutputCache Duration="120" VaryByParam="CategoryID;SelectedID"%>

    示例代码:

    <%@ Page Language="C#" %>

    <%@ OutputCache Duration="10" VaryByParam="*"%>

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

    <script runat="server"> 

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

        <%Response.Write(System.DateTime.Now);%>

        </div>

        </form>

    </body>

    </html>

    页面片断缓存

    设置页面片断缓存可以采用<%@ Control Language="C#" ClassName="WebUserControl" %>

    或者在类名前加特性[PartialCaching(3)]。

    如:

    <%@ Control Language="C#" ClassName="WebUserControl" %>

    <%@ OutputCache Duration="10" VaryByParam="*"%>

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)

        {

            this.Label1.Text = System.DateTime.Now.ToString("hh:mm:ss");

            this.timer.Style.Add("width", (DateTime.Now.Second * 4).ToString() + "px");

        }

    </script>

    <div id = "timer" runat="server" style="">

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </div>

    或,

    [PartialCaching(3)]

    public partial class WebUserControl3 : System.Web.UI.UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            this.Label1.Text = System.DateTime.Now.ToString("hh:mm:ss");

            this.timer.Style.Add("width", (DateTime.Now.Second * 4).ToString() + "px");

        }

    }

    应用程序数据缓存

    https://files.cnblogs.com/Ring1981/Cache.rar]:

    1. 建立default.aspx页面

    <%@ Page Trace="true" TraceMode="SortByCategory" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!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>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            <asp:Button ID="Button1" runat="server" Text="Flush Cache" OnClick="Button1_Click" />

        <asp:DataGrid ID="dg1" runat="server"/>

        </div>

        </form>

    </body>

    </html>

    2. 添加一个names.xml

    <?xml version="1.0" encoding="utf-8" ?>

    <people>

     <person first="Scott" last="stafield"></person>

     <person first="jim" last="Green"></person>

     <person first="kate" last="Green"></person>

    </people>

    3.添加后台代码

     using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Data;

    using System.Web.Caching;

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                DataSet ds = null;

                ds = (DataSet)Cache["names"];

                if (ds == null)

                {

                    string path = @"c:inetpubwwwrootCache ames.xml";

                    ds = new DataSet();

                    ds.ReadXml(path);

                    CacheDependency cd = new CacheDependency(path);

                    Cache.Insert("names", ds, cd);

                    Trace.Warn("Names read from XML file");

                }

                else

                {

                    Trace.Warn("Names read from cache");

                }

                dg1.DataSource = ds;

                dg1.DataBind();

            }

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

           Cache.Remove("names");

            Response.Redirect("default.aspx");

        }

    }

    运行程序, 第一次运行时,DataGrid里的数据是从xml文件中读取的。 以后每次刷新,文件都是从缓存读取的。 我们可以从每次运行时间看到,每次读取xml文件花费时间大约0.001275s,而每次读取缓存花费时间大约0.000044。 可见读取缓存数据性能能够大大地改善。

    Category

    Message

    From First(s)

    From Last(s)

     

    Names read from XML file

    0.00155997480126664

    0.001275

           
     

    Names read from cache

    0.000343619091253218

    0.000044

    使用数据库缓存:

    待续... 

    参考文件:

    MSDN 文档

    Microsoft ASP.NET 入门教: http://chs.gotdotnet.com/quickstart/aspplus/doc/quickstart.aspx 

    Scott stafield先生的视频教程

    添加禁查词案例:

    添加缓存:

     Cache["key"] = str;

    读取缓存:

     str = (StringBuilder)Cache["key"];

    清除缓存:

     Cache.Remove("key");

  • 相关阅读:
    Qt 4.x调试器问题,缺失调试助手。
    Qt 如何判断文件是不是一个目录
    Qt 5.7设置调试器
    Qt 无法解析外部文件2001,2019之类的
    TCP/UDP socket
    Qt 5.7 版本+2013VS环境配置
    骑行、318总结心得啊经验什么的
    蓝牙硬件交互数据传输Demo
    iOS蓝牙开发总结-4
    iOS蓝牙传输数据演示-3
  • 原文地址:https://www.cnblogs.com/brain008/p/10884885.html
Copyright © 2020-2023  润新知