Change:
Utility.CacheHelper.SaveTocache("MyKey",a);
to:
Utility.CacheHelper.SaveTocache("MyKey",a.ToList());
and:
List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey");
to:
List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey").ToList();
Technically just the latter change is required – the former will make it even more bulletproof.
This is necessary since if any entries adding to the MemoryCache will be shared amongst callers – so if one caller manipulates the List
then other callers will also see that manipulated list.
By calling ToList
you ensure that each caller gets its own copy of the List
– so items added to one won’t be seen in the other.
0
solved C# MemoryCache Changing Data by Itself