« SlickRun and QuickSilver | Main | 20% off Acronis TrueImage »

Using the ASP.NET Cache outside of ASP.NET

Posted 2005-11-23 07:31 AM in ASP.NET | NUnit.

Travis was talking about using the ASP.NET Cache object/subsystem outside of ASP.NET. I found it a little creepy as I've had all sorts of trouble trying to to Mock testing with ASP.NET outside of IIS and ended up using Cassini back in the day.

He convinced me though, check out the sample code on his site. I also started a conversation on a list server and here's what came of that:

Rob Howard said:

Yes, it's fairly common (and easy) to do. You just have to include a reference to the System.Web assembly in non-web applications; which may have led to your "creep out" – for what it’s worth it used to do the same to me :) 
FWIW, I believe (from memory) the recommended way you grab a reference outside of a web application is:
using System.Web;
using System.Web.Caching;

Cache cache = HttpRuntime.Cache;
<snip>...the Cache is just too important of a feature to only belong to ASP.NET.

Scott Stanfield said:

The biggest problem you'll run into using the cache outside of a web app is simply the namespace: System.Web. People freak out in code reviews. We got a lot of trash talk from the J2EE world on PetShop because of this.

Chris Kinsman said he seemed to remember some trouble with the Cache not sticking around in memory when used outside of ASP.NET, but that hasn't been substantiated. I'm going to dig more.

Adding System.Web to your non-web project is a good way to get folks to panic. Another is adding a reference to Microsoft.VisualBasic in a C# application. Both are reasonable and darned useful things to do, though.



Wednesday, November 23, 2005 10:50:37 AM (Pacific Standard Time, UTC-08:00)
Where I used to work, we commonly used the System.Web namespace for MailMessages in non-web applications. I've never tried it for using the cache, but I seem to remember someone in a google group using HttpCookie in a WinForm app not that long ago.

I've never used the VisualBasic namespace, what's in there that's useful? Removal of short-circuiting? :P
Marty Thompson
Wednesday, November 23, 2005 11:23:09 AM (Pacific Standard Time, UTC-08:00)
Why not use the Enterprise Library Caching Block from MS Patterns & Practices?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/caching1.asp
Wednesday, November 23, 2005 11:26:28 AM (Pacific Standard Time, UTC-08:00)
As long as you use it as a cache with proper fallback on regular data calculation/retrieval, it works great in non-web apps or even libraries that could be used in either scenario. The big pitfall is that the cache will gladly let go of data before it has actually "expired" in scenarios like low memory. I suppose that advice goes for using it ASP.net as well. I've just seen people use it like a big global Hashtable in an app, and then it breaks under stess because things are going away and they think something is wrong with it.
Wednesday, November 23, 2005 11:26:55 AM (Pacific Standard Time, UTC-08:00)
In my opinion that block has way too many moving parts. Using the ASP.NET Cache is fairly lightweight, and includes the very useful cache dependancies stuff.
Wednesday, November 23, 2005 12:10:08 PM (Pacific Standard Time, UTC-08:00)
public sealed class CacheProvider
{
private CacheProvider(){};

public static Instance
{
get
{
return (HttpContext.Current == null) ? HttpRuntime.Cache : HttpContext.Current.Cache;
}
}
}

or the 2.0 version

return (HttpContext.Current ?? HttpRutime.Cache);
Wednesday, November 23, 2005 12:20:20 PM (Pacific Standard Time, UTC-08:00)
I like to pull in the J# DLLs, too. Just to REALLY freak people out.
Wednesday, November 23, 2005 1:57:11 PM (Pacific Standard Time, UTC-08:00)
Still wonder why the FX team hasn't just moved caching into another (non web) namespace. It pwns for web stuff, but there's no reason you wouldn't want caching in other types of solutions. (And who wants all the bloat of the enterprise libs, etc.)

Honestly, just a simple static object that supports sliding expirations? Seems that that SQL Server 2005 folks would have been clamoring to get SQL Server Cache invalidation working for winforms/WPF applications too.
Thursday, November 24, 2005 1:35:17 AM (Pacific Standard Time, UTC-08:00)
karl...if you look (reflector) at HttpContext.Current.Cache all it does is return HttpRuntime.Cache.
Scott Hanselman
Thursday, November 24, 2005 4:55:43 AM (Pacific Standard Time, UTC-08:00)
Scott:
So always use HttpRuntime.Cache istead of HttpContext.Current.Cache ;) thanks

karl
Thursday, November 24, 2005 9:30:32 AM (Pacific Standard Time, UTC-08:00)
ObjectDataSource, anyone?

The problem with this one, as I recall, is mostly in setup.

It'd be nice to see BuildProviders pulled out, as well.

Re Cache vs EntLib: Cache has one singular advantage -- it's already there. As much as I pimped EntLib a year ago, it's a download-modify-and-compile project. With all do respect to the hard work that went into it: if I wanted that, I could use Java. I prefer my batteries included.
Keith J. Farmer
Thursday, November 24, 2005 2:28:21 PM (Pacific Standard Time, UTC-08:00)
EntLib Caching block is actually pretty easy to use. I too was concerned that it was a bit hefty/intertwined. If you spend a little time writing your first trival app with it, it planes out fast. It has the same types of dependencies and it allows you to do have a variety of stores (you can have two levels of cache with one backed with memory, one backed with db). It's grown on me for non-ASP.NET ctxs.
Grant
Monday, November 28, 2005 9:44:55 AM (Pacific Standard Time, UTC-08:00)
Most of the conversation has been around enterprise components and the use of ASP.NET cache. Can anyone think of a good reason why the use of the ASP.NET cache on the client side of a rich client app wouldn't work? For these types of applications, preventing a round-trip all together would be the ideal situation. Granted that with rich-client it's easier maintain state but why "roll your own" cache when there is a robust library to do just that. Thoughts?
Mike Morris
Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (5)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads