I am writing an application which has a few object caches. The way it needs to work is when an object is retrieved from the cache:
object foo = CacheProvider.CurrentCache.Get("key");
foo should be a local copy of the original object, not a reference. What is the best way to implement this? The only way I have in mind so far is to use a BinarySerializer to create a copy, but I feel like I am missing a better way.
The backing for the cache implementation is arbitrary, as it is provider-based. I need to support any number of caches, from the HttpRuntime cache to something like Velocity. The focus here is on the layer between the cache backing and the consumer code - that layer must ensure a copy of the object is returned. Some caches will already do this, but some do not (HttpRuntime cache being one).
Rex - we implemented something very similar in a big enterprise web product, and we ended up explicitly creating an IDeepCloneable interface that many of our objects implemented. The interesting thing is that we backed it with BinarySerializer, simply because it's a convienent and virtually foolproof way to make a deep copy of an object without all the hassle of reflection or forcing developers to write and test clone methods.
It works like a charm. Our team has stopped to mull over it several times and we have yet to come up with a better option.
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