Members
Technology Zones
Articles
Hosted By
Info
|
Rated
Read 39,919 times
Contents
Related Categories
.NET Data Caching - Applications, Sessions and Cookies
Applications, Sessions and Cookies
While classic ASP does not have the rich data caching API found in the .NET Framework, it does give us the ability to maintain state and to cache data with the help of session, application and even cookie objects. For starters cookies, which are stored on the Web visitor's computer (disk) don't hold a lot of data. They have a 4k limit and can contain only string information. Additionally, a user may have their browser configured not to accept cookies. For more information on using cookies in classic ASP, see the Cookies FAQs on ASPFAQs.com.
Session variables can also be used to cache information in classic ASP, although, as with the cookie approach each session variable is specific to a particular user, and to tie a session variable to a particular user the user's browser must accept cookies. The advantages of using session variable's over cookies is that you can store objects, such as an array, or Dictionary object. Since session variables are stored on the Web server's memory, storing large objects in a user's session on a site with many simultaneous users can lead to reduced memory on the Web server. For more information on session variables see the Session Variables FAQs on ASPFAQs.com.
Most often application variables were the means one would use to cache information in classic ASP. Since a given application variable is "global" to the entire Web application, application variables are primarily candidates for caching information that is global across all Web pages on the site. That is, imagine that you ran an eCommerce Web site and that on every page you wanted to list the top 10 selling products. Rather than do a database access on every page, you could cache the results in an application variable. This is an excellent example of when an application variable would be a good use for caching. If you need to cache more user-specific information, such as the 10 most recently purchased items for the user who's visiting the site, you'd likely want to employ the session object or cookies to do this. (For more information on caching database values in an application variable be sure to read : A Real-World Example of Caching Data in the Application Object.)
You can use application variables for caching in ASP.NET much like you did in classic ASP. However, since ASP.NET Web pages can utilize the .NET data cache APIs there's really no reason to ever resort to using application variables for caching in an ASP.NET Web application. In fact, caching data through the data cache API as opposed to through application variables in an ASP.NET Web application has its advantages, including: items in the data cache will be evicted from memory if memory becomes scarce; when adding items to the data cache you can specify how long they should persist in the cache in terms of an absolute or slidign time; and many other advantages, which we will examine in this article.
So to sum up the use of Application, Session and or Cookie objects for caching in a classic ASP page:
- Use cookies for small non-critical data.
- Use Sessions on a user-to-user basis as in an eCommerce site
- And use Application variables for site-wide information that doesn't require constant revision.
While this article will focus on .NET data caching in detail, a good general article on .NET caching basics can be found here at Caching with ASP.NET.
Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.
Comments
-
Posted by ojemuyiwa on 15 May 2008
To Access the Pages Cache from class code c#
Cache ce = new Cache();
ce = System.Web.HttpRuntime.Cache;
Lol -
Posted by muthup on 30 Apr 2005
So , Does That mean this is a Genuine Microsoft Bug??
No matter What If I Use this Syntax[ Colorede red] to acess the Cache Object I get an Exception...
internal class EnvUtil :System.Web.UI.Page {...
Just because the Cache cannot be inherited, doesn't mean you can't access it when you are inheriting the Page class -
public class MyClass : System.Web.UI.Page {
public Object SomeMethod() {
... -
Posted by muthup on 30 Apr 2005
I stumbled into this problem. The comment is correct The Cache class is a sealed class, which means it cannot be inherited. Since you are inheriting the Page class in your X testClass the Cache object...
Posted by DMarko1 on 23 Mar 2005
Hi Charlie,
You can use .NET remoting when you need to keep your cache across processes. The process is discussed here [url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda...
|
Search
Related Content
Code Samples
New Members
|