views:

41

answers:

1

I am running into an odd problem, and this is the only thing I can think of. I'm storing a list in cache, and I am randomly losing items from my list as users use the site. I have a class that is called that either goes to cache and returns the list from there, or if the cache is over a certain time frame old, it goes to the database and refreshes the cache. So when I pull the data from cache, this is what it looks like....

results = (List<Software>)cache["software"];

And then I return results and do some processing, filter for security, and eventually it winds up on the screen. For each Software record, there can be multiple resources attached to it, and based on how the security goes they may see some, all, or none of the records. So in the security check it will remove some of those resources from the software record.

So my question is.... when I return my results list, is it a reference directly to the cache object? So when I remove a resource from the software object, it is really removing from cache as well? If that is the case, is there any way to not return it as a reference? Thanks!

edit: I think I may have just answered my own question.... so if I do something like this:

results = new List<Software>((List<Software>)cache["software"]);

it will copy the cached list to my results list, correct?

+1  A: 

You are referencing a single object. So if User A has some parts of the object changed, the User B will also see those changes.

You should make a deep copy of this object after you get it from the cache, but before you filter it. That way you won't run into this conflict. So the pseudo code would be something like this:

var results = (List<Software>)cache["software"];
var userresults = DeepCopyResults(results);
var filteredresults = UserFilter(userresults);

DeepCopyResults and UserFilter are functions defined by you.

Keltex