Cache Expiration Question

G

Guest

I cache a ton of stuff, and my application works great....but......

When I am stress testing it I'll get the "Object reference not set to an
instance of an object"
every once in a while...

It tends to happen when the cache is expired.....
I do a lot testing to make sure that it actually returns the object out of
cache...
I insure that it is a valid object, then when I try to use it I get that
error.

My question is this: When get something out of cache:

Return
CType(System.Web.HttpContext.Current.Cache.Get(CAFactory.Conts.CACHE_MANAGER), CAManager)

I believe this returns a reference to the object, so in the instant that I
return the correct reference and I am about to use it, it expires....so the
object doesn't exist anymore?

Am I thinking about this correctly? Should I test for this all the time?

If not isnothing(object) then object.Function()

What kind of performance impact would this have?
Is the "IsNothing()" function expensive?
 
R

Rick Strahl [MVP]

Realistically you should always check for data that comes out of the cache.
There may be other reasons that things don't actually end up in the cache
such as the app was restarted etc.

If you use the cache the pattern usually is to read the cache first check
for null (or other empty value) and if so go ahead and call the load routine
that populates the item and then write it out to the cache. This way you are
always making sure that no matter what your code will work.

As far as overhead this really doesn't cause much since all you're doing is
adding an extra null check.

Hope this helps,

+++ Rick ---
--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web


Brian Linden said:
I cache a ton of stuff, and my application works great....but......

When I am stress testing it I'll get the "Object reference not set to an
instance of an object"
every once in a while...

It tends to happen when the cache is expired.....
I do a lot testing to make sure that it actually returns the object out of
cache...
I insure that it is a valid object, then when I try to use it I get that
error.

My question is this: When get something out of cache:

Return
CType(System.Web.HttpContext.Current.Cache.Get(CAFactory.Conts.CACHE_MANAGER
), CAManager)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top