By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

Question about objects in Cache

P: n/a
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.)
--
Joe Fallon

Nov 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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,

--
Scott
http://www.OdeToCode.com/

On Tue, 5 Oct 2004 16:05:32 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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.)


Nov 18 '05 #2

P: n/a
It's a reference so you do have to wrap them up in cloning properties...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.)
--
Joe Fallon

Nov 18 '05 #3

P: n/a
Thanks!
Just what I needed to know.
--
Joe Fallon
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:b1********************************@4ax.com...
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,

--
Scott
http://www.OdeToCode.com/

On Tue, 5 Oct 2004 16:05:32 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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 processof 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 eachother" or not.

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

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

Nov 18 '05 #4

P: n/a
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

--
Joe Fallon

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:#8**************@TK2MSFTNGP14.phx.gbl...
Thanks!
Just what I needed to know.
--
Joe Fallon
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:b1********************************@4ax.com...
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,

--
Scott
http://www.OdeToCode.com/

On Tue, 5 Oct 2004 16:05:32 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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 processof 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 eachother" or not.

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

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


Nov 18 '05 #5

P: n/a
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?

--
Scott
http://www.OdeToCode.com/

On Tue, 5 Oct 2004 18:51:48 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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


Nov 18 '05 #6

P: n/a
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.)

--
Joe Fallon

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:6f********************************@4ax.com...
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?

--
Scott
http://www.OdeToCode.com/

Nov 18 '05 #7

P: n/a
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.

--
Scott
http://www.OdeToCode.com/

On Wed, 6 Oct 2004 20:23:42 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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.)


Nov 18 '05 #8

P: n/a
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.
--
Joe Fallon


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:56********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com/

On Wed, 6 Oct 2004 20:23:42 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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.)

Nov 18 '05 #9

P: n/a
Application cache is definitely in process.

--
Scott
http://www.OdeToCode.com/

On Wed, 6 Oct 2004 22:39:55 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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.


Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.