AppFabric Caching

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";
        }
    }
}

Default.aspx
<%@ 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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值