Cache problem

J

Joe Fallon

I have a class which is stored in the cache.
When I retrieve it from cache I return a clone of the class so that I do not
keep a reference to the cached item. (That is the theory anyway.)

When I modify the cloned object and then navigate to another page, the data
in the cache is modified with my changes anyway!

So when I go to another page and expect a "clean" copy of my class to be
retrieved from the cache, it is "dirty" with the changes made on a different
page.

Can someone please explain what is happening here?
I thought that returning a clone would break the reference to the cached
object.

I would hate to have to abandon this technique, but if it doesn't work....
 
J

Joe Fallon

I found this using Google:
========================================================
This is the expected behaviour. What you really get from of the cache is a
reference to the object in cache. Changes to this object are seen by
everyone that has a reference to this object.

You could solve this by wrapping the Cache access in a method that gets the
excel object from the cache and then clones it, before giving a reference to
the clone to you. But this might not be enough, since this is only a shallow
copy (objects referenced in the original are not copied, just their
references). Depends on how the excel object is implemented.
========================================================

I believe the shallow copy is my problem as I change one of the internal
objects using a reference.
 

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

Geo cache puzzle 8
cache reseting problem stumped 1
To Cache or not 1
Cache not working as expected when using list(of integer) 2
cache 2
cache 2
Cache problem 1
problem with cache 3

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top