Article:
  ASP.NET Caching
Subject:   cache retrieval order
Date:   2008-10-31 19:58:28
From:   Ulfius
I notice that you have two methods of retrieving and checking for items from the cache. One is the correct method and one could result in unexpected behavior.


The correct method is the code -
ds = CType(Cache("News"), DataSet)


If ds Is Nothing Then
ds = loadData()
End If


The incorrect method is -
If Cache("News") Is Nothing Then
...
Cache.Insert("News", ds, depends)
Else
ds = CType(Cache("News"), DataSet)
End If


The reason you should do the assigment first, then check for nothing is that it is possible (albeit unlikely) that the cached item could expire inbetween the checking = nothing and the assignment to the variable.