Created
August 4, 2016 15:58
-
-
Save cburnette/d44e796be4a2650697cea5cc356dc572 to your computer and use it in GitHub Desktop.
This file contains 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 characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web; | |
namespace BasicMvcSample.Helpers | |
{ | |
public static class CacheHelper | |
{ | |
static readonly string CACHE_PREFIX = ConfigurationManager.AppSettings["boxEnterpriseId"]; | |
public static object Fetch(string key, TimeSpan expiresIn, Func<object> generateObjectFunction) | |
{ | |
var cache = HttpContext.Current.Cache; | |
var keyWithPrefix = CACHE_PREFIX + "/" + key; | |
object result = cache.Get(keyWithPrefix); | |
if (result == null) | |
{ | |
result = generateObjectFunction(); | |
cache.Add(keyWithPrefix, result, null, DateTime.Now.Add(expiresIn), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment