The Caching service includes the following features:
- Pre-built ASP.NET providers for session state and page output caching, enabling acceleration of web applications without having to modify application code.
- Caches any managed object - for example: CLR objects, rows, XML, binary data.
- Consistent development model across both Windows Azure and Windows Server AppFabric.
- Secured access and authorization provided by the Access Control Service (ACS).
1) Get Web.config Setting from Portal
2) Code for caching data, session and page
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationServer.Caching;
namespace Caching
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region data caching
DataCacheFactory cacheFactory = new DataCacheFactory();
DataCache cache = cacheFactory.GetDefaultCache();
// Add the string "value" to the cache, keyed by "item"
//cache.Add("item", "value", TimeSpan.FromMinutes(30));
cache.Put("item", "value");
// Add the string "value" to the cache, keyed by "key"
object result = cache.Get("item");
if (result == null)
{
// "Item" not in cache. Obtain it from specified data source
// and add it.
string value = GetItemValue();
cache.Put("item", value);
result = cache.Get("item");
}
else
{
// "Item" is in cache, cast result to correct type.
}
Response.Write(result);
#endregion
#region session caching
Session["productId"] = "1234";
Response.Write(Session["productId"]);
#endregion
#region output caching
Response.Write(System.DateTime.Now.ToString());
#endregion
}
protected string GetItemValue()
{
return "value";
}
}
}
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Caching._Default" %>
<%@ OutputCache Duration="10" VaryByParam="none"%>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
</asp:Content>