开发者

Cached data accessed by reference?

开发者 https://www.devze.com 2022-12-31 11:45 出处:网络
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 calle

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 开发者_JS百科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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消