Cache asp.net c # data list

I want to implement caching of a list retrieved from db. I looked at this:

How can I cache objects in ASP.NET MVC?

I have this in my code:

var addresses = repository.GetAllAddresses();//returns IQueryable<string> 

using the answer in the question above, how can I cache this ?, I created the CacheExtensions class, but not sure where to go from there.

+4
source share
1 answer

Once you have an extension method, it's a simple matter of using it (don't forget to bring the static class that you defined in the GetOrStore method by adding a usage directive to the namespace containing it, or you won’t see the GetOrStore<T> extension method) :

 IEnumerable<string> addresses = HttpRuntime .Cache .GetOrStore<IEnumerable<string>>( "addresses", () => repository.GetAllAddresses().ToArray() ); 

Notes:

  • We use "addresses" as the cache key, so the result will be saved under this key.
  • We call .ToArray() on an IQueryable<string> to look up addresses and store the results in a cache, not a query.
+4
source

Source: https://habr.com/ru/post/1341554/


All Articles