[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Rich Armstrong

1/8/2009 11:44:00 PM

If one were to retrieve a cached class object and then update it, is the
cached value being changed, or does retrieving the object only create a
local copy?

For example, if I ran the following code:

MyClass c = HttpContext.Current.Cache["someKey"] as MyClass;
c.SomeString = "abc";

does the cached value associated with "someKey" change? (For the sake of
discussion, assume that Cache["someKey"] exists and holds an object of type
MyClass.) Would a subsequent retrieval of that object by code from another
page see the value assigned above, or would a Cache.Insert() be required to
persist the change?


8 Answers

Alvin Bruney [ASP.NET MVP]

1/9/2009 2:04:00 AM

0

The type of the parameter is object, since objects are passed by value
(where the value is the reference of the object) you are infact manipulating
the object itself (shortened form of the explanation). now here is the
kicker, if you retrieve and change the object, you need to put it back in
cache, not for the sake of preserving the value, but for the sake of caching
the object so it can be available to other callers.

--
Regards,
Alvin Bruney

Auther Plug
OWC Blackbook now on download at www.lulu.com/owc

"Rich Armstrong" <RichA24@nospam.nospam> wrote in message
news:6EC506A6-B252-4344-B376-3681222D2407@microsoft.com...
> If one were to retrieve a cached class object and then update it, is the
> cached value being changed, or does retrieving the object only create a
> local copy?
>
> For example, if I ran the following code:
>
> MyClass c = HttpContext.Current.Cache["someKey"] as MyClass;
> c.SomeString = "abc";
>
> does the cached value associated with "someKey" change? (For the sake of
> discussion, assume that Cache["someKey"] exists and holds an object of
> type MyClass.) Would a subsequent retrieval of that object by code from
> another page see the value assigned above, or would a Cache.Insert() be
> required to persist the change?
>
>

stcheng

1/9/2009 3:03:00 AM

0

Hi Rich,

Regarding on the Cache object question, here are my answers inline:


>does the cached value associated with "someKey" change? (For the sake of
>discussion, assume that Cache["someKey"] exists and holds an object of
type
>MyClass.)
===========================
It depends on whether your class is a reference type or value type. For
reference type, any instance of them are passed and held as a
reference(like pointer in C/C++). Therefore, what you store into Cache
collection is that handle/reference. Whenever you make any changes on any
reference variable(pointing to that object instance), all the other
reference/handle pointing to the same instance will see the changes.

However, if the MyClass is a value type, then each time you read or store
this type object into Cache, box and unbox will occur, and something like a
local copy you mentioned will occur in that case.


#Value vs Reference Types in C#
http://www.albahari.com/valuevsref...


Would a subsequent retrieval of that object by code from another
>page see the value assigned above, or would a Cache.Insert() be required
to
>persist the change?

The same as above.


Here is a web article which also provide some good explanation on ASP.NET
Cache/Session/... storage behavior:

#ASP.NET Cache and Session State Storage
http://www.west-wind.com/Weblog/posts...

If you have anything unclear on this, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa9...
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.





--------------------
>From: "Rich Armstrong" <RichA24@nospam.nospam>
>Subject: Updating Cached Objects
>Date: Thu, 8 Jan 2009 17:43:44 -0600

>
>If one were to retrieve a cached class object and then update it, is the
>cached value being changed, or does retrieving the object only create a
>local copy?
>
>For example, if I ran the following code:
>
> MyClass c = HttpContext.Current.Cache["someKey"] as MyClass;
> c.SomeString = "abc";
>
>does the cached value associated with "someKey" change? (For the sake of
>discussion, assume that Cache["someKey"] exists and holds an object of
type
>MyClass.) Would a subsequent retrieval of that object by code from another
>page see the value assigned above, or would a Cache.Insert() be required
to
>persist the change?
>
>
>

Rich Armstrong

1/9/2009 5:16:00 PM

0

> It depends on whether your class is a reference type or value type. For
> reference type, any instance of them are passed and held as a
> reference(like pointer in C/C++). Therefore, what you store into Cache
> collection is that handle/reference. Whenever you make any changes on any
> reference variable(pointing to that object instance), all the other
> reference/handle pointing to the same instance will see the changes.

I was speaking only of class objects (the post begins, "If one were to
retrieve a cached class object ...") which, I thought, were reference types
by definition. How can a class (vs. a struct) be a value type? I thought
that defining a type as a class is what _makes_ it a reference type.

In any case, the behavior I'm seeing in our application is consistent with
Alvin Bruney's statement in his earlier post, "... if you retrieve and
change the object, you need to put it back in cache, not for the sake of
preserving the value, but for the sake of caching the object so it can be
available to other callers." What's confusing here is the suggestion that
the (reference type's) changed value can be "preserved," without being
"available to other callers."

stcheng

1/10/2009 8:11:00 AM

0

Thanks for your reply Rich,

Yes, generally "class" means a reference type. However, there is a special
type "System.ValueType" which you need to take care(if you used to treat
all "class" defined type as reference type.

#ValueType Class
http://msdn.microsoft.com/en-us/library/system.valu...

Actually, "struct" keyword is somewhat C# specfic and the C# compiler help
you generate type that derived from "ValueType" when you use "struct" to
define a C# type.

Also, if you have interests, I would suggest you the books authored by
Jeffrey Richter:


#Applied Microsoft? .NET Framework Programming (Pro-Developer) (Paperback)
http://www.amazon.com/Applied-Microsoft%C2%AE-Framework-Programmin...
loper/dp/0735614229

#CLR via C#, Second Edition (Pro Developer) (Paperback)
http://www.amazon.com/CLR-via-Second-Pro-Developer/dp/...

ASP.NET Cache is in-memory based and it use standard .NET collection to
hold objects, there is no paritcular magic in it. BTW, if you are using SQL
server session, you'll need to take a bit care since SQL Server Session
mode will perform serialize/deserialize on objects stored in it. That may
affect the behavior you care about. But Cache doesn't adopt such kind of
persistence mode.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.




--------------------
>From: "Rich Armstrong" <RichA24@nospam.nospam>
>References: <6EC506A6-B252-4344-B376-3681222D2407@microsoft.com>
<GHcX$agcJHA.2056@TK2MSFTNGHUB02.phx.gbl>
>Subject: Re: Updating Cached Objects
>Date: Fri, 9 Jan 2009 11:16:24 -0600

>
>> It depends on whether your class is a reference type or value type. For
>> reference type, any instance of them are passed and held as a
>> reference(like pointer in C/C++). Therefore, what you store into Cache
>> collection is that handle/reference. Whenever you make any changes on any
>> reference variable(pointing to that object instance), all the other
>> reference/handle pointing to the same instance will see the changes.
>
>I was speaking only of class objects (the post begins, "If one were to
>retrieve a cached class object ...") which, I thought, were reference
types
>by definition. How can a class (vs. a struct) be a value type? I thought
>that defining a type as a class is what _makes_ it a reference type.
>
>In any case, the behavior I'm seeing in our application is consistent with
>Alvin Bruney's statement in his earlier post, "... if you retrieve and
>change the object, you need to put it back in cache, not for the sake of
>preserving the value, but for the sake of caching the object so it can be
>available to other callers." What's confusing here is the suggestion that
>the (reference type's) changed value can be "preserved," without being
>"available to other callers."
>
>

Rich Armstrong

1/10/2009 9:11:00 PM

0

Thanks. I'm very familiar with Mr. Richter's work.

stcheng

1/12/2009 2:12:00 AM

0

Thanks for your reply,

If there is anything we can help, welcome to post here.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.


--------------------
>From: =?Utf-8?B?UmljaCBBcm1zdHJvbmc=?= <RichA24@nospam.nospam>
>Subject: Re: Updating Cached Objects
>Date: Sat, 10 Jan 2009 13:11:01 -0800

>
>Thanks. I'm very familiar with Mr. Richter's work.
>

yanni

1/14/2009 12:33:00 PM

0

Funny, I did the test, after I changed object from cache, but not put it
back, I read it from another browser session, the updated value shows up
there...

"Rich Armstrong" <RichA24@nospam.nospam> wrote in message
news:18889212-7454-4056-A760-3ACA9C5564CD@microsoft.com...
>> It depends on whether your class is a reference type or value type. For
>> reference type, any instance of them are passed and held as a
>> reference(like pointer in C/C++). Therefore, what you store into Cache
>> collection is that handle/reference. Whenever you make any changes on any
>> reference variable(pointing to that object instance), all the other
>> reference/handle pointing to the same instance will see the changes.
>
> I was speaking only of class objects (the post begins, "If one were to
> retrieve a cached class object ...") which, I thought, were reference
> types by definition. How can a class (vs. a struct) be a value type? I
> thought that defining a type as a class is what _makes_ it a reference
> type.
>
> In any case, the behavior I'm seeing in our application is consistent with
> Alvin Bruney's statement in his earlier post, "... if you retrieve and
> change the object, you need to put it back in cache, not for the sake of
> preserving the value, but for the sake of caching the object so it can be
> available to other callers." What's confusing here is the suggestion that
> the (reference type's) changed value can be "preserved," without being
> "available to other callers."


Alvin Bruney [ASP.NET MVP]

1/21/2009 12:21:00 AM

0

That will only work if the cache is the global cache. If the cache is the
session store or viewstate, it won't persist. There's a potential for
confusion between names that can lead to misinterpretation.

--
Regards,
Alvin Bruney

Auther Plug
OWC Blackbook now on download at www.lulu.com/owc

"yanni" <yanni66cn@126.com> wrote in message
news:OkBJzQkdJHA.3864@TK2MSFTNGP05.phx.gbl...
> Funny, I did the test, after I changed object from cache, but not put it
> back, I read it from another browser session, the updated value shows up
> there...
>
> "Rich Armstrong" <RichA24@nospam.nospam> wrote in message
> news:18889212-7454-4056-A760-3ACA9C5564CD@microsoft.com...
>>> It depends on whether your class is a reference type or value type. For
>>> reference type, any instance of them are passed and held as a
>>> reference(like pointer in C/C++). Therefore, what you store into Cache
>>> collection is that handle/reference. Whenever you make any changes on
>>> any
>>> reference variable(pointing to that object instance), all the other
>>> reference/handle pointing to the same instance will see the changes.
>>
>> I was speaking only of class objects (the post begins, "If one were to
>> retrieve a cached class object ...") which, I thought, were reference
>> types by definition. How can a class (vs. a struct) be a value type? I
>> thought that defining a type as a class is what _makes_ it a reference
>> type.
>>
>> In any case, the behavior I'm seeing in our application is consistent
>> with Alvin Bruney's statement in his earlier post, "... if you retrieve
>> and change the object, you need to put it back in cache, not for the sake
>> of preserving the value, but for the sake of caching the object so it can
>> be available to other callers." What's confusing here is the suggestion
>> that the (reference type's) changed value can be "preserved," without
>> being "available to other callers."
>
>