Last active
May 23, 2016 17:26
-
-
Save pedroadaodev/941c1ceabe577e39aff59d0c75c8c6b8 to your computer and use it in GitHub Desktop.
Get e Set on Cache
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
public static class CacheManager | |
{ | |
// for session cache use HttpContext.Current.Cache class | |
/// <summary> | |
/// Gets the object from cache. | |
/// </summary> | |
/// <param name="objectName">Name of the object.</param> | |
/// <param name="objectInCache">The object in cache.</param> | |
/// <returns></returns> | |
public static bool GetObjectFromCache(string objectName, out object objectInCache) | |
{ | |
objectInCache = HttpRuntime.Cache[objectName]; | |
if (objectInCache == null) | |
{ | |
return false; | |
} | |
return true; | |
} | |
/// <summary> | |
/// Inserts the object in cache. | |
/// </summary> | |
/// <param name="objectName">Name of the object.</param> | |
/// <param name="objectToCache">The object to cache.</param> | |
public static void InsertObjectInCache(string objectName, object objectToCache) | |
{ | |
int outputCacheDuration; | |
int.TryParse(ConfigurationManager.AppSettings["OutputCacheDuration"], out outputCacheDuration); | |
if (outputCacheDuration > 0) | |
{ | |
HttpRuntime.Cache.Insert(objectName, objectToCache, null, DateTime.Now.AddSeconds(outputCacheDuration), Cache.NoSlidingExpiration); | |
} | |
} | |
} |
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
List<Badges> listBadges; | |
object cachedBadges; | |
if (!CacheManager.GetObjectFromCache("Badges", out cachedBadges)) | |
{ | |
listBadges = GetBadges(); | |
// CACHE | |
CacheManager.InsertObjectInCache("Badges", listBadges); | |
} | |
else | |
{ | |
listBadges = cachedBadges as List<Badges>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment