Question about objects in Cache

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

If I add an instance of an object to the Cache what is returned when I
request the item from the cache?
Is it a clone of the object or is it a reference to the object?

I want to know because I would like to go through the instantiaition process
of a New instance only once and then store the object in the cache.
I need to know if subsequent users of the object will be "stepping on each
other" or not.

So, do I have to speciifcally clone the object or is it the a fresh instance
each time I retrieve it?

(Note: once a user retrieves it and works on it, changes are stored to the
user's session. not back to cache.)
 
Hi Joe:

You store a reference to an object in the cache, and later, you'll
pull out a reference to the same object. If you make changes to the
object - everyone will see them, not just the code making changes on
one user's behalf.

HTH,
 
OK.
I have it all set up and have not yet implemented cloning.

But so far it seems to not need it. I make changes to the returned object
and store the object in Session and retrieve it and my changes are there.
But another user retrieves the item from cache and there are no changes in
it. If it was a reference then wouldn't I see the changes?

I would prefer to keep it this way if it was safe. Any ideas?

======================================================================
Code sample from .aspx code behind:
myObj = CType(Util.GetItemFromCache("MyObjKey", mKey), myObj )

Then in the rest of the page I manipulate myObj. Including storing in
Session for use during postbacks.

======================================================================

This is part of the cache function I use to add the object to the cache:

Public Shared Function GetItemFromCache(ByVal Key As String, Optional ByVal
myKey As Decimal = 1) As Object
Dim cacheKey As String = Key.ToLower

If IsNothing(HttpRuntime.Cache(cacheKey)) Then
HttpRuntime.Cache.Insert(cacheKey, myObj.NewmyObj(myKey))
End If

Return HttpRuntime.Cache(cacheKey)

End Function
 
Hi Joe:

Yes, you should definitely be getting a reference to the same object
when you ask the cache for "MyObjKey" - regardless of what user
session the code is in.

Is there any chance the code could be overwriting what is in the Cache
somewhere else?
 
I am just starting to implement this so the answer to your question is no.
The relevant code was posted in my last message.

I always call the function GetItemFromCache from my .aspx page code behind.

As you can see, the function checks the cache for the object and if it
exists, it returns it.
If it does not exist, it creates a new object.

Other tests I conducted (changing default values) show that the object is
always being pulled from cache because the new defaults are not present
until I invalidate the cache.
(BTW - I use DB cache invalidation dependency to do this in ASP.Net 1.1 -
which is really slick. It is supported natively in v 2.0.)

My current theory is that my class has to be serializable to be stored in
Session (or cache) so I wonder if the cache or session methods return the
serialized values each time the object is retireved.
This would validate what I am seeing. The instance in the .aspx form is
filled with the serialized members from the object in cache, so changes to
the object in the code behind never appear in the cache object (unless the
objec is manually added to the cahce which I avoid, since at that time it is
user specific.)
 
By any chance are you using out of processes session state
persistance? (i.e. using the session state service in another process
or using SQL Server?). If so, you are describing the exact behavior.

I was assuming InProc session state, please accept humble apologies if
I caused confusion.
 
Yes. I am using Out of Proc session state.

Are you saying that under that scenario, the application cache is *also*
stored out of process?
I was not aware of that.

That would explain the behavior I am seeing.
 

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


Back
Top