Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Cache problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arjen - 29 Aug 2006 18:54 GMT
Hi,

I'm using the cache block of the enterprise lib Jan. 2006.

There is somethings strange going on.

I add a varable to the cache, let's say the string myData.
string myData = "1111111";
primitivesCache.Add("Key1", myData, CacheItemPriority.Normal, null, new
SlidingTime(TimeSpan.FromMinutes(15)));

When I do after this line this:
myData = "*****";

And I get my data out of the cache manager then myData has the value of
*********. I also tried to load it inside an other variable. Again the
value is the same.

How is it posible that the data in the cache is changed? Or not added
liked I did?

Thanks!
Arjen
Bruce Wood - 29 Aug 2006 21:55 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> How is it posible that the data in the cache is changed? Or not added
> liked I did?

Please post a short but complete program that demonstrates your
problem. See Jon Skeet's page for a description of what constitutes a
"short but complete program":

http://www.yoda.arachsys.com/csharp/complete.html
Arjen - 29 Aug 2006 23:00 GMT
Well, I found the problem... still strange...
I add an item to the cache.
After that I change the item, and also the item in the cache is changed
(why?).
Now I have made a copy of the variable after I add it to the cache.
This works fine.

Thanks.
Arjen

> > Hi,
> >
[quoted text clipped - 22 lines]
>
> http://www.yoda.arachsys.com/csharp/complete.html
Bruce Wood - 29 Aug 2006 23:11 GMT
What type is the variable?

Is it a string, like the code in your original post? Or is it one of
your own classes? If it is an object of your own type (class) then I
know why....

> Well, I found the problem... still strange...
> I add an item to the cache.
[quoted text clipped - 32 lines]
> >
> > http://www.yoda.arachsys.com/csharp/complete.html
Arjen - 30 Aug 2006 08:56 GMT
Hi,

It's an arraylist with my own objects (classes).
So, it sounds like you know why...

Thanks,
Arjen

> What type is the variable?
>
[quoted text clipped - 38 lines]
> > >
> > > http://www.yoda.arachsys.com/csharp/complete.html
Bruce Wood - 30 Aug 2006 17:25 GMT
It's because what is cached is a _reference_ to the object instance.
Class instance are _reference types_, so your local variable and the
cache both store the same thing: a reference (or, if you prefer, a
pointer) to the same object in memory. Change the state (contents) of
the object, and it changes in both places because there is only one
object.

If you want to cache a copy of the object, not a reference to the same
object you're holding, then your class has to implement ICloneable and
you have to cache myObject.Clone(), not myObject.

> Hi,
>
[quoted text clipped - 46 lines]
> > > >
> > > > http://www.yoda.arachsys.com/csharp/complete.html

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.