Skip to content

Instantly share code, notes, and snippets.

@cburnette
Created August 4, 2016 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburnette/d44e796be4a2650697cea5cc356dc572 to your computer and use it in GitHub Desktop.
Save cburnette/d44e796be4a2650697cea5cc356dc572 to your computer and use it in GitHub Desktop.
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