Improving Performance By Using ASP.NET Caching - Application Caching
Improving Performance By Using ASP.NET Caching - Application Caching
What is ASP.NET caching mechanism?
When to use caching? and how can I
use it in order to improve my site
performance? are the questions this
post series is going to answer.
In today’s post I’ll introduce
the application caching. You can
read the previous post in the subject here.
What is Application Caching?
As indicated from its name the application cache is a caching mechanism that is
constructed for every ASP.NET application when the application starts. Like the
Application and Session objects the Cache object is a singleton key value
collection. This singleton representation enables the cached items to be shared
between user sessions and requests. Unlike the Application and Session mechanisms,
the Cache has more capabilities, it is more powerful and it doesn’t store the data
for the entire life of the Session or Application.
Cache Scavenging
One thing you need to know about the cache is that its storage is in memory and
is limited (because the memory of a server is limited). As such a limited storage the
cache uses an automatic scavenging behavior in order to remove objects. The
removing is based on object priorities which is one of the parameters that you can
configure for an inserted cache object. Also, the cache remove items when they
have not been accessed for a period of time. Whenever the memory is starting to
be full the scavenging starts. You can’t control which object will be remove
(you control only the priorities) and therefore you can’t trust the availability of
a cached object and should always check if the object is available or not.
There are things you can do about object availability which will be described in the
next section (priorities, callback and etc).
Caching Objects
As written earlier in the post, the application cache is a collection and in order to
insert objects to the cache all I have to do is insert it by a key like this:
Now you should ask yourself things like that’s it? or why the hell did I bother to read
the post until now?
But… the power of the application cache comes with the Add and Insert methods
and not by the use of the cache indexer.
Lets explore the Add and Insert functionality.
The Add method gets a full bunch of parameters. If you don’t need to use all these
parameters you should use the Insert method with one of its overloaded
methods. The type of parameters that you can use to insert an object to the
cache are:
- Key – which will be the key of the stored object.
- The object to store in cache.
- Dependency – a CacheDependency object that watch a key or a file and
when they change triggers the removal of the cached object. - Absolute expiration – the time at which the object will be removed from the
cache whether it was accessed recently or needed. - Sliding expiration – the time span after which the object will be removed
from the cache if it hasn’t been accessed. - Priority – a priority value that can determine which objects will be removed
first when the scavenging mechanism starts. - OnRemoveCallback – an event handler that is called whenever the object is
removed from the cache. You can use this ability to restore object back to
the cache whenever they expired.
Lets look at an example:
public partial class _Default : Page
{
#region Members
private const string KEY = "Key";
#endregion
#region Page Events
protected void Page_Load(object sender,
EventArgs e)
{
if (Cache[KEY] == null)
{
CacheItemRemovedCallback callback =
new CacheItemRemovedCallback(
OnRemove);
Cache.Add(KEY,
"Value",
new CacheDependency(Server.MapPath(
"file.txt")),
DateTime.Now.AddDays(1),
new TimeSpan(1000),
CacheItemPriority.Normal,
callback);
}
string value = Cache[KEY].ToString();
}
#endregion
#region Methods
private void OnRemove(string key, object val,
CacheItemRemovedReason reason)
{
// do something
}
#endregion
}
The example is very simple to understand and shows all the parameters I
wrote about in action.
Summary
Lets sum up the post, today I explained the concept of application cache.
The application cache is very helpful and powerful singleton collection that can
be used to cache application data in memory for a faster retrieval.
It can be configured very easily by the Add or Insert method parameters.
The next post will continue the tour of caching and will explain the output
cache.