Skip to content

Instantly share code, notes, and snippets.

@codeimpossible
Forked from jbubriski/gist:3207278
Created August 14, 2012 19:25
Show Gist options
  • Save codeimpossible/3351974 to your computer and use it in GitHub Desktop.
Save codeimpossible/3351974 to your computer and use it in GitHub Desktop.
cache locking
private static readonly string _languagesKey = "languages";
public static Dictionary<string, string> GetLanguages()
{
return GetData(_languagesKey, () =>
{
// TODO: Get data from language service
var languages = new Dictionary<string, string>();
return languages;
});
}
public static T GetData<T>(string dataCacheKey, Func<T> dataMethod) where T : class
{
var data = HttpRuntime.Cache.Get(dataCacheKey) as T;
if (data == null)
{
lock (HttpRuntime.Cache)
{
data = dataMethod();
HttpRuntime.Cache.Add(dataCacheKey, data, null, DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment