Csharp Programmatically Setting Page Caching

The example code below shows a page whose expiration is set programmatically and uses the sliding expiration scheme. The sliding expiration scheme means that whenever a page is hit, the timeout is reset. This ensures that only items that are being used are kept in cache. Pages that are cached once and then never accessed again are a waste of resources.

<%@ Page Language="C#" %>
<html>
  <script runat="server">
    void Page_Load(Object sender, EventArgs e) {
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
      Response.Cache.SetCacheability(
                   HttpCacheability.Public);
      Response.Cache.SetSlidingExpiration(true);
      _msg.Text = DateTime.Now.ToString();
    }
  </script>
 
  <body>
    <h3>Output Cache example</font></h3>
    <p>Last generated on:
              <asp:label id="_msg" runat="server"/>
  </body>
</html>

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Filed Under: Language Basics

Tags:

RSSComments (0)

Trackback URL

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.