Created
March 2, 2016 11:39
Revisions
-
Jogai created this gist
Mar 2, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ Taken from https://github.com/secretGeek/til/blob/master/asp.net_mvc/from_memory.md # Handy Cache helper method I love this little thing. public static T FromMemory<T>(string Key, Func<T> func, double days = 7) where T : class { var value = MemoryCache.Default.Get(Key) as T; if (value == null) { value = func(); if (value != null) { MemoryCache.Default.Add(Key, value, DateTime.Now.AddDays(days)); } } return value; } Then, instead of simply retrieving something from storage, e.g. var items = LoadSiteMap(); You do this slightly awkward, but fairly clean alternative: var items = Latest.FromMemory("SiteMap", () => LoadSiteMap()); You do have to tell it the cache_key to use. You can override the default duration to hold it in memory... var items = Latest.FromMemory("SiteMap", () => LoadSiteMap(), 0.1); And if things go really pear shaped, and it can't infer the types, you might need to specify them, for example here: var items = Latest.FromMemory<List<ISitemapItem>>("SiteMap", () => LoadSiteMap(), 0.1);