Search code examples
c#memcachedgeneric-list

Memory cached generic list removing without effect cached list


I am trying remove one item from my list.BUT, I am getting the list from cache. After this I am removing one item in my fuction. This removing effect my cached list.If I try again to get cached list from cache in another page. Removed item is missing how is this possible ?

example ,

var IList list;
 if(condition)

 list=CacheManagement.GetFromCache();
 else
 list=SqlManagement.GetFromSql();


 list.removeall(x=>x.id==1);
 //end of my function

Solution

  • So when you make a change to list, it is reflected to the cache, that is because the reference (memory address of the list) is copied to cache, so any changes would actually be reflected in the cache.

    To avoid it simply put a Clone() of your list to the cache. (assuming UpdateCache() is the method that sets the cache) you can do it like this:

    UpdateCache(list.Clone());