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.
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:
= "I'm learning so much!" cache.insert("item", strVal, nothing, datetime.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:
= "Caching is so much fun!" cache.insert("item", strVal, nothing, datetime.maxvalue, timespan.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:
= "I may or may not have understood these directions..." cache.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.