Stupid Caching question

C

Chris

I periodically, once every week or so, get an error when trying to read an
XML doc stored in cache. I get an 'object not set to a reference' I have a
gut feeling it happens when the app is under strain and the moment between
testing the cache and returning the cached xml doc it expires. I have read
several other people have the same problem. I have seen some C# code which
theoretically makes sense to me, but I don't understand the syntax and how
you would write it in VB.net. And I thought I had got me head round
"converting" C# ....... Can anyone explain it. I have tried:

Dim cacheitem as object = Cache(key) as XMLDocument

but with no luck. Here is the C# code in full.


Object cacheItem = Cache[key] as DataTable;
if(cacheItem == null)
{
cacheItem = GetData();
Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1),
TimeSpan.Zero);
}
return (DataTable)cacheItem;

Regards, Chris.
 
A

Alvin Bruney [MVP]

You know implicitly that the object is a datatable, you don't need to
*download* it into a type of object and then cast the return type to a
datatable, just use a datatable, it will avoid the conversion hit and make
the code more readable.

Dim cacheItem As Object = TryCast(Cache(key), DataTable)

If cacheItem Is Nothing Then

cacheItem = GetData()

Cache.Insert(key, cacheItem, Nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)

End If

Return CType(cacheItem, DataTable)


--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top