Home arrow ASP.NET arrow Page 3 - How Caching Means More Ca-ching, Part 2

How Caching Means More Ca-ching, Part 2

I've introduced you to Output caching and Fragment caching in my last article. By means of both we are able to increase the performance of our web applications significantly by storing and retrieving commonly accessed pages and fragments of pages in memory. This article will explain the use of the third and most granular cache method, Data Caching. I will show you how to cache at the object level. Translation: more flexibility, more speed. Therefore, happier users, and not to mention (cough cough), more ca-ching.

Author Info:
By: Justin Cook
Rating: 4 stars4 stars4 stars4 stars4 stars / 66
April 27, 2004
  1. · How Caching Means More Ca-ching, Part 2
  2. · Methods
  3. · You're Too Old, Please Leave
  4. · Learn to Set Priorities!

print this article

How Caching Means More Ca-ching, Part 2 - You're Too Old, Please Leave
(Page 3 of 4 )

So how exactly can we set a maximum age requirement for the cached data? Well I hate you tell you that you have more options, but it's true. I know, first you have the option of which caching method to choose. Then if you've chosen data caching, you've got three more option of how to implement it. Then, if you've decided on .insert or .add, you have the control options. You've finally decided to use a time expiration, and are very tired. This is why I'm sorry to present more options to you, but I'm afraid I have to.

Firstly, the most straightforward option is just to set an absolute expiration time. Here's how you could do that:

strVal "I'm learning so much!"
.insert("item"strValnothingdatetime.now.addminutes(10), timespan.zero'there should be no carriage return here

Ok, some explanation is needed. That 'nothing' that you see is the dependency option, which will be discussed next. That needs to be there. You may quickly decipher that we're telling the object to expire in 10 minutes, but maybe you're wondering what the last option is, the timespan.zero. That us the sliding expiration time, which I will explain next. Just remember that both must be represented by some value, if you are to use either option. Also remember that only one can be used at a time.

The second option, which was turned off in the last example by the use of timespan.zero, is called the sliding expiration policy. This will 'slide' the time to get rid of the object until a specified time after the last request for that object. We set this option in the following way:

strVal "Caching is so much fun!"
.fromseconds(60)) 'there should be no carriage return here

Nothing to cryptic in that code, just note that the datetime.maxvalue disables the absolute expiration, because as I'm confident that you remembered, only one can be used at a time. So now let's go back to first option in the chain, the dependency option.

Do You Have Any Dependents?

I find this to be a very cool option. We can tell the application to leave the object in cache, depending entirely on the variability of another object. That other object is monitored for any changes, and as soon as a change is detected, the cached object expires! This could be a file, another cached object, or even multiple objects! Here's an example in code:

strVal "I may or may not have understood these directions..."
.insert("item"strVal, New CacheDependency(Server.MapPath("depend.xml")))

I'm assuming that you're already making plans of how to use this, and I don't blame you -- it is very powerful. However, in Whidbey (ASP.NET version 2) you will have the option of setting the dependency to be upon an actual database, now THAT's powerful! But I guess until we have a public release, this will have to suffice! Ok, enough coveting what we do not have, let's continue learning what's now within our grasp.

blog comments powered by Disqus

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials