Created
January 11, 2016 09:11
-
-
Save maftieu/c5aeea30c27ab568b80d to your computer and use it in GitHub Desktop.
ASP.Net Cache helper
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
/// <summary> | |
/// Implements a cache helper. | |
/// Taken from http://codereview.stackexchange.com/questions/25907/asp-net-caching-helper (comments added). | |
/// </summary> | |
public static class CacheHelper | |
{ | |
/// <summary> | |
/// Retrieve the specified item from the cache if it is cached. Otherwise, insert it to the cache. | |
/// Note: of the cache is not available, the <paramref name="initializer"/> method may be called on every call. | |
/// </summary> | |
/// <typeparam name="T">Type of the item to cache.</typeparam> | |
/// <param name="key">The cache key used to reference the item.</param> | |
/// <param name="initializer">The method to call to retrieve the item to cache</param> | |
/// <param name="absoluteExpiration"> | |
/// The time at which the added object expires and is removed from the cache. If you are using sliding | |
/// expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration. | |
/// </param> | |
/// <returns>The retrieved cache item, or null if the key is not found.</returns> | |
public static T GetCached<T>(string key, Func<T> initializer, DateTime absoluteExpiration) | |
{ | |
return GetCached(key, initializer, absoluteExpiration, Cache.NoSlidingExpiration); | |
} | |
/// <summary> | |
/// Retrieve the specified item from the cache if it is cached. Otherwise, insert it to the cache. | |
/// Note: of the cache is not available, the <paramref name="initializer"/> method may be called on every call. | |
/// </summary> | |
/// <typeparam name="T">Type of the item to cache.</typeparam> | |
/// <param name="key">The cache key used to reference the item.</param> | |
/// <param name="initializer">The method to call to retrieve the item to cache</param> | |
/// <param name="slidingExpiration"> | |
/// The interval between the time the added object was last accessed and the time at which that object expires. | |
/// If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes | |
/// after it is last accessed. If you are using absolute expiration, the slidingExpiration parameter must be | |
/// NoSlidingExpiration. | |
/// </param> | |
/// <returns>The retrieved cache item, or null if the key is not found.</returns> | |
public static T GetCached<T>(string key, Func<T> initializer, TimeSpan slidingExpiration) | |
{ | |
return GetCached(key, initializer, Cache.NoAbsoluteExpiration, slidingExpiration); | |
} | |
/// <summary> | |
/// Retrieve the specified item from the cache if it is cached. Otherwise, insert it to the cache. | |
/// Note: of the cache is not available, the <paramref name="initializer"/> method may be called on every call. | |
/// </summary> | |
/// <typeparam name="T">Type of the item to cache.</typeparam> | |
/// <param name="key">The cache key used to reference the item.</param> | |
/// <param name="initializer">The method to call to retrieve the item to cache</param> | |
/// <param name="absoluteExpiration"> | |
/// The time at which the added object expires and is removed from the cache. If you are using sliding | |
/// expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration. | |
/// </param> | |
/// <param name="slidingExpiration"> | |
/// The interval between the time the added object was last accessed and the time at which that object expires. | |
/// If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes | |
/// after it is last accessed. If you are using absolute expiration, the slidingExpiration parameter must be | |
/// NoSlidingExpiration. | |
/// </param> | |
/// <returns>The retrieved cache item, or null if the key is not found.</returns> | |
public static T GetCached<T>(string key, Func<T> initializer, DateTime absoluteExpiration, TimeSpan slidingExpiration) | |
{ | |
var httpContext = HttpContext.Current; | |
if (httpContext != null) | |
{ | |
key = string.Intern(key); | |
lock (key) // locking on interned key | |
{ | |
var obj = httpContext.Cache[key]; | |
if (obj == null) | |
{ | |
obj = initializer(); | |
httpContext.Cache.Add(key, obj, null, absoluteExpiration, slidingExpiration, CacheItemPriority.Default, | |
null); | |
} | |
// taking care of value types | |
if (obj == null && (typeof(T)).IsValueType) | |
{ | |
return default(T); | |
} | |
return (T)obj; | |
} | |
} | |
else | |
{ | |
return initializer(); // no available cache | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment