Captured Technology

Adding Items to the Cache, Retrieving the Items, and Clearing the Cache

Mar 27 2009

Adding Items to the Cache, Retrieving the Items, and Clearing the Cache

When working with a web application that contains data items that do not change too often, it improves the performance of the application to store data items in the server cache and retrieve those items from the cache instead of making an extra trip to the database. Also clearing the cache when a item does change is important for your application.

'*** The cache key is used to uniquely identify the cache items
Dim CacheKey As String = "SomeKey"
 
'*** Get the the amount of time the object stays in the cache from web config
Dim CacheTime As Double = CDbl(ConfigurationManager.AppSettings("cachetime"))
 
If Cache(CacheKey) Is Nothing Then
    '*** Create the objItem
    Dim objItem as Item
 
    '*** Add the object to the cache
    Cache.Add(CacheKey, objItem, Nothing, Now().AddMinutes(CacheTime), Nothing, _
    CacheItemPriority.Normal, Nothing)
Else
    '*** Pull the data from the cache
    Dim objItem As Item = Cache(CacheKey)
End If

Clearing all the cache of an ASP.Net application:

For Each de As DictionaryEntry In HttpContext.Current.Cache
        HttpContext.Current.Cache.Remove(DirectCast(de.Key, String))
Next

Blog Directory

Latest technology news.
 Steve Patterson
 424  245477  11/17/2017

FaceBook

Translate

Sponsors





Blog Calendar

Categories