Cache problem

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
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....
 
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

Back
Top