You have a couple of questions to ask yourself:
- Am I returning dynamic data data?
- If not, how often do I expect this call to occur?
If there are answers, 1- not really and 2- the call often happens, you should cache it.
I donโt know how your data access is configured, but I just throw my data into cache objects as follows:
public IQueryable<Category> FindAllCategories() { if (HttpContext.Current.Cache["AllCategories"] != null) return (IQueryable<Category>)HttpContext.Current.Cache["AllCategories"]; else { IQueryable<Category> allCats = from c in db.Categories orderby c.Name select c; // set cache HttpContext.Current.Cache.Add("AllCategories", allCats, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 30, 0, 0), System.Web.Caching.CacheItemPriority.Default, null); return allCats; } }
This is an example of one of my queries to a repository based on LINQ to SQL. First he checks the cache, if the entry exists in the cache, it returns it. If not, it enters the database and then caches it with an expiration date.
source share