Thread: Caching
View Single Post
  #2 (permalink)  
Old May 28th, 2010, 02:59 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

all the application code which runs from the App_Code folder is destroyed at the end of a request correct?
Waht makes you think that?

Objects created in your code and stored in the Cache will remain there until a) they expire due to one of their constraints, b) the cache decides to ditch them or c) the application restarts.

Let's say you have a class Person in App_Code:

public class Person
  public string FirstName { get; set; }
  public string LastName { get; set; }
Then create one in Default.aspx.cs:

Person imar = new Person() { FirstName = "Imar", LastName = "Spaanjaars" };
and then store it in the Cache:

Cache["MyCacheKey"] = imar;
You can retrieve the item later; possible in a different page, during a different user's request, from a different session or even days later, provided the cache maintained the item.

The objects are stored within the Application Domain and as such are globally available from the perspective of the application.

Hope this helps,

Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!