Home arrow ASP.NET arrow Page 2 - 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 - Methods
(Page 2 of 4 )

Method # 1: the Cache Method

This is by far the simplest way to get your object/data into the cache. If you're at all familiar with classic ASP, you'll see it's as easy as using session variables. If you're new to all of this, then you're about to see just how easy it really is. Here's the incredibly large amount of code you will  need to type in order to store an item in the cache:

strVal "something trivial"
("item") = strVal

So that's it. 'item' is the variable name you will use, and 'value' is none other than -- you guessed it -- the value! But how do we now retrieve the item once it's sitting in memory? Also, not very difficult:

newVariable cache("item")

There, wasn't that far less painful than a trip to the dentist? But you may be wondering if we're limited to storing simplistic variable such as the string I used in the example. Well, recall that this is caching at the object level. So any object, for instance an array, can certainly be stored this way! Now I sense you are starting to get excited, beginning to understand the power and versatility of data caching.

But if you really examine this first method, you'll agree that it's very basic. All you can do it add and retrieve items from the cache. There's really no further level of control granted us. For instance, how do we tell the application when to oust the object from memory at a given time to free up resources? We don't; It just remains there until a server restart or explicit removal (through the cache.remove(“item”) method), which is far too onerous of a task for most developers to bother with. Hmm, you're thinking that something more is needed. Well, something more is given, let's check it out!

Methods #2 & #3, .Insert & .Add

Now we move beyond the restrictions of the Cache method. We take the control of the cache-flow out of the hands of the server, and place it where it should be, in your hands. You and I will get to determine the lifetime of the objects, either with time expiration, external dependencies, or even based on priority against other cached objects. Before we do that though, I'll just quickly show you how to use these methods in their raw form, wit no options:

strVal "this is SOOO exciting!"
cache.add("item", strVal)

Why you would choose one method over the other will not be readily apparent yet. Really the only difference is that cache.add has one extra option, that of returning an object representing the cached data. Other than that option, the syntax for both is the same, so I'll only use one method in my example, and you can interchange as you see fit. So why don't we just proceed through these one at a time, starting with the expiration option.

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials