Share via


Feeling the Cache Love

I've been working on the business and data layers for a web system and, as you might expect, I've been making quite a bit of use of ASP.NET's caching system. Now, to test the business layer, I had gone ahead and set up a non-ASP.NET caching system as well using my standard method for caching in Windows applications, a static hashtable with strongly typed string keys (works well, fairly compatible with the ASP.NET cache so it is easy to move code between the two models) but then I realized that I could just use the ASP.NET cache even when my code was being used from a Windows Forms applications (for my class libraries, where I don't know what type of interface is being used) and it works just fine.

Shared Function GetValueFromCache( _
ByVal key As String) As Object
Dim myContext As HttpContext
myContext = HttpContext.Current

If myContext Is Nothing Then
Return HttpRuntime.Cache.Get(key)
Else
Return myContext.Cache.Get(key)
End If
End Function

Shared Sub PlaceValueIntoCache( _
ByVal key As String, _
ByVal item As Object, _
ByVal cacheDuration As Integer)
Dim myContext As HttpContext
myContext = HttpContext.Current
Dim myCache As Caching.Cache

If myContext Is Nothing Then
myCache = HttpRuntime.Cache
Else
myCache = myContext.Cache
End If

myCache.Insert(key, item, Nothing, _
Now.AddSeconds(cacheDuration), _
Caching.Cache.NoSlidingExpiration)
End Sub

This might be common knowledge, but I have been handling my own non-ASP.NET caching all on my own, and this just makes it too easy. In fact, it makes it so easy that I started thinking of ways to perform even more caching in some of my Windows Forms applications... I can see many performance gains in my future!

Comments

  • Anonymous
    October 05, 2003
    ::This might be common knowledge

    It is not. It is well "documented" in some message boards and blogs now (people find it out), but it is not official.

    IMHO one of the errors MS has done - they should really have put these classes outside ASP.NET - this is VERY useful in win apps, too. And they should have documented it.
  • Anonymous
    October 05, 2003
    You might also check into the caching application block. You might needed some different caching features as time goes by, for example, sharing the same cache between different applications,or even machines..
  • Anonymous
    October 06, 2003
    Note that this creates a dependency on System.Web which is not available for systems such as Windows 98. It' not to be forgotten if you are targetting such environments.
  • Anonymous
    October 06, 2003
    Been there done that:

    http://blog.gatosoft.com/PermaLink.aspx?guid=62a79bd8-c5e6-4423-9ad3-cd2f6b22f251

    Note that the System.Caching.Cache uses the application domain's thread pool -- bear this in mind if you will scheduling your own threads out of the thread pool. Don't particularly like the System.Web.dll dependency either. My WeakReference cache (see above link) addresses most of these issues...