I want to implement caching of a list pulled from the db. I’ve had a look 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 have created the class CacheExtensions, but not sure where to go from there.
Once you have the extension method it’s a simple matter of using it (don’t forget to bring the static class you have defined the
GetOrStoremethod into scope by adding a using directive to the namespace containing it or you won’t be able to see theGetOrStore<T>extension method):Things to note:
.ToArray()on theIQueryable<string>in order to eagerly fetch the addresses and store the results into the cache and not the query.