ASP.NET

ASP.NET Caching

Data Cache

ASP.Net exposes Cache API to store data in the cache for retrieval later.

Getting Started

Store string

   Cache["key"]="value";

Retrieve string

var value="";
if (Cache["key"] != null)
   value = Cache["key"].ToString();

You can also use the Add or the Insert methods.

protected void Page_Load( object sender, EventArgs e)
{
    if ( this.IsPostBack )
    {
        label1.Text + = "Page is posted back";
    }
    else
    {
        label1.Text + = "Page is created";
    }
    
    if ( Cache [ "item"] == null )
    {
        label1.Text + = "New item is created";
        DateTime item = DateTime.Now;
        label1.Text + = "Item is stored";
        Cache.Insert ( "item", item, null );
        DateTime.Now.AddSeconds ( 20 ), TimeSpan.Zero;
    }

    else
    {
        label1.Text + = "Item is accesses";
        DateTime item = ( DateTime) Cache [ "item" ];
        label1.Text + = "Time is: " + item.ToString();
        label1.Text + = <br/>";
    }
    
    label1.Text + = "<br/>";
}
    

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow