Using the application Web Cache

Posted on: Sat Jun 05 17:09:31 -0700 2010. Updated on: Sat Jun 05 17:13:22 -0700 2010.
Category: DotNet

It is a good idea to use caching for frequently accessed objects to reduce trips to the database or your web services. Here is an example of using the System.Web application cache. You can even use this in dll's outside of your web project, as long as you reference System.Web.

string cacheName = "objectNameCache";
int cacheDurationMinutes = 10;
var cache = System.Web.HttpRuntime.Cache;
			
cache.Insert(cacheName, quote, null, DateTime.UtcNow.AddMinutes(cacheDurationMinutes), TimeSpan.Zero);

// now to access it... First check if null, then simply cast cache to outcoming object.
if (cache[cacheName] != null)
{
    quote = (DataTransferObjects.QuoteData)cache[cacheName];
}

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.