I want to cache a data collection in asp.net mvc and filter this collection without to changed the cached collection.
Is it possible to do this?
Because for now, when I get my cached collection I get a reference to it and filter data of this collection change the cached collection.
Does anyone know?
ASP.NET Core uses two caching techniques. In-memory caching uses the server memory to store cached data locally, and distributed caching distributes the cache across various servers. We'll explore them both below.
Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.
ASP.NET supports caching of data as well web pages. Applications Data caching enables caching of data whereas Page Output Caching enables Web pages.
Simply copy the items from your cached list to a new list. The instances will be the same but the original (cached) list will not be touched. Something like this:
List<[CLASS]> collectionCached; //assuming this this your cached list
List<[CLASS]> collectionWorking = new List<[CLASS]>();
collectionWorking.AddRange(collectionCached);
This will allow you to filter out any instances you want without touching the original (cached) list.
EDIT:
After further clarification from OP, it seems there is a need to make a copy of the entities themselves. The basic idea behind making a copy of a reference object is called "Cloning". This makes a copy of the original entity and allows you to change the copy without changing the original instance. SO has some good links, here is one that discusses both the concept of cloning and LINQ.
How to get a copy of data instead of a reference using linq/lambda in c#?
This should get you on the right track. Let me know if you have any other questions.
Cache the full list, and then use Linq to filter the collection.
var list = GetFromCache();
var filtered = list.Where(x => x.Name.Contains("rob"));
EDIT:
When you get this filtered list, are you changing the objects contained WITHIN the list? Yes, that would be reflected for the same instance in the cached list as well. But at that point it sounds like this isn't great data to be caching. Usually cached data doesn't change, or you can live without seeing changes for some time. If data accuracy is more important, don't cache.
You might be able to get what you want simply though, if you are changing the state of items in your lists:
var filtered = list.Where(x => x.Name.Contains("rob")).Select(x => x.MemberwiseClone()).Cast<YourObjType>();
This uses MemberwiseClone which does a shallow copy; if that's not good enough, you'll need do some work on a method that does a keep copy. It returns Object, so I use the Cast extension method to get it back to the type of object originally in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With