I am writing a .net c# application.
I retrieve some data from an xml file, cache the data to the .net cache and return it from my method. I perform some processing on the data and return it another part of my application.
Next call, I read from cache, process it and return it etc.
The problem I have is that the processing performed on the cache data seems to modify the cache and not the local variable which means the next time I read from cache, its the processed data from the previous processing that is returned.
So it seems that the data returned from cache is returned by ref and not value.
Any idea how I can prevent the cache being modified?
An in memory cache will store a pointer to the object. Its like a global variable. Any other variable assignments to the cached item will be referencing the same object. This is different for an out-of-process or distributed cache. For those types of cache, the object must be serialized and deserialized from the cache. In those cases you are getting copies.
If you want to simulate out-of-process that behavior you could copy/clone an object or serialize/deserialize the object in an out of the cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With