Hi,
Here's a MVC C# cache helper I found here https://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application, but I've modified it to take in account all data types and not only classes...
Here's how to use it, using the lamba is an elegant way, isn't it?
public static class InMemoryCache { public static T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) { if (HttpContext.Current == null || HttpContext.Current.Cache == null) return getItemCallback(); bool cacheExists = HttpContext.Current.Cache[cacheKey] != null; T item; if (cacheExists) item = (T)HttpContext.Current.Cache[cacheKey]; else { item = getItemCallback(); if (item != null) HttpContext.Current.Cache.Insert(cacheKey, item, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration); } return item; } }
public static Dictionary<string, string> getTipsCategory() { return InMemoryCache.GetOrSet("getTipsCategory", () => { return db .BlogCategory .Where(c => c.BlogEntry.Count() > 0) .ToDictionary(c=>c.Name, c=>c.url); }); }