Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get objects by value out of cache

Tags:

c#

.net

caching

Abstract

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.

Details

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).

like image 809
Rex M Avatar asked Jul 21 '09 02:07

Rex M


1 Answers

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.

like image 106
womp Avatar answered Oct 07 '22 13:10

womp