-
-
Save jbubriski/3207278 to your computer and use it in GitHub Desktop.
Cache locking with timeout.
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 characters
private static readonly string _languagesKey = "languages"; | |
public static Dictionary<string, string> GetLanguages() | |
{ | |
return GetData(_languagesKey, 15, () => | |
{ | |
// TODO: Get data from language service | |
var languages = new Dictionary<string, string>(); | |
return languages; | |
}); | |
} | |
public static T GetData<T>(string dataCacheKey, int cacheTimeInMinutes, Func<T> dataMethod) where T : class | |
{ | |
var data = (T)HttpRuntime.Cache.Get(dataCacheKey); | |
if (data == null) | |
{ | |
lock (HttpRuntime.Cache) | |
{ | |
data = (T)HttpRuntime.Cache.Get(dataCacheKey); | |
if (data == null) | |
{ | |
data = dataMethod(); | |
HttpRuntime.Cache.Add(dataCacheKey, data, null, DateTime.Now.AddMinutes(cacheTimeInMinutes), 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