惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
腾讯CDC
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
A
About on SuperTechFans
博客园 - 叶小钗

博客园 - o0myself0o

利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现 生产者消费者模式,代码中碰到的疑问(已解决) 利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式 Entity Framework 4.0 ObjectContext下的各种方法实践 题目:若干个不重复数,打乱顺序输出 wtf js(四) - o0myself0o - 博客园 wtf js(三) number的类型不是number wtf js(二) 算法:给定两个已从小到大排好序的整型数组arrA和arrB,将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序 应用中的单例模式 面试题:给你三个bool类型变量a, b, c,判断至少有两个为true javascript面向对象编程(一) - o0myself0o - 博客园 wtf js(一) - o0myself0o - 博客园 社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式 社区网站功能实现系列(二):社区内容分享到别的SNS 社区网站功能实现系列(一):多国语言的实现 反射获取Class中Property的值 A*寻路初探 使用 jQuery 简化 Ajax 开发
闲谈ASP.NET 2.0缓存技术
o0myself0o · 2008-06-25 · via 博客园 - o0myself0o

    这一个学期一直都生活在忙忙碌碌、犹犹豫豫之中,也一直没有什么心情写一些技术性的东西。这两天刚从考研的囫囵里跳出来,顿感轻松了许多。人也活力了很多。遂决定时不时的写点东西。今天刚好把袁阔成的评三国下下来了,一边听着三国,一边写。今天想闲谈一下ASP.NET 2.0缓存技术。
    1.在.aspx页面加<%@ OutputCache Duration="60" VaryByParam="none" %>  Duration为缓存时间,VaryByParam为QueryString,none表示地址没有参数。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblTime" Text="" runat="server"></asp:Label>
    </div>
    </form>
    2.同1,只是VaryByParam值不为none,可以任设,比如state。例如:链接<a href="default.aspx?state=...">链接</a>,state的值做为一个GridView数据源的参数,为了能看出效果,最好加一个lable放时间,点链接之后,选出了一部分数据,这就是对一个页面不同的参数的分别缓存效果。
    ......
    <%@ OutputCache Duration="60" VaryByParam="state" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["state"]==null)
            lblTime.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="1")
            lblTime1.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="2")
            lblTime2.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <a href="OutputCacheWithParam.aspx">刷新</a><br />
        <a href="OutputCacheWithParam.aspx?state=1">刷新1</a><br />
        <a href="OutputCacheWithParam.aspx?state=2">刷新2</a><br />
        <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime1" ForeColor="green" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime2" ForeColor="blue" Text="" runat="server"></asp:Label><br />
    </div>
    </form>
    3.上面两种方法的缺点就是页面的数据不能及时更新。有的时候要给用户显示一些很重要的数据,这样的方式就不能满足了,下面的部分缓存可以解决这以问题。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    static string GetCurentTime(HttpContext context)
    {
        return DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    缓存时间:
    <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"><%=DateTime.Now.ToString() %></asp:Label><br /><br />
    现在时间:
    <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurentTime" />
    </div>
    </form>
    
    这里有一个方法静态GetCurentTime,返回值作为动态数据传给Substitution,这样,第一个时间在缓存期内不改变,第二个时间不受缓存影响。
    4.用API
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));//相当于duration
        Response.Cache.SetCacheability(HttpCacheability.Public);//相当于VaryByParam
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    5.利用Page.Cache缓存数据
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DataView source = (DataView)Cache["UsersCache"];

        if (source == null)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter("select * from Users", conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "UsersTable");

            source = new DataView(ds.Tables["UsersTable"]);
            Cache["UsersCache"] = source;
        }
        this.GridView1.DataSource = source;
        this.GridView1.DataBind();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>

    6.实现用户控件缓存,但页面其他地方不缓冲,也是实现部分缓存
    --WebUserControl.ascx部分代码
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    <%--这里可以是很大的数据库数据,所以缓存非常有必要--%>
    这是用户控件中的内容:<br />
    <asp:Label runat="server" ID="lblTime1" ForeColor="red"><%=DateTime.Now.ToString() %></asp:Label>

    --PageFragmentCaching.aspx部分代码
    ......
    <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
    .......
    <form id="form1" runat="server">
    <div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <hr />
        这里是主文件的内容:<br />
        <asp:Label runat="server" ID="lblTime" ForeColor="green"><%=DateTime.Now.ToString() %></asp:Label>
    </div>
    </form>

    今天就写到这里了。唉,不写还好,一些发现了太多的问题,文笔不好不说,写出来的代码估计都是有问题的。看来以后不管多紧张,还是要坚持写博啊。