Skip to content

Instantly share code, notes, and snippets.

@pedroadaodev
Last active May 23, 2016 17:26
Show Gist options
  • Save pedroadaodev/941c1ceabe577e39aff59d0c75c8c6b8 to your computer and use it in GitHub Desktop.
Save pedroadaodev/941c1ceabe577e39aff59d0c75c8c6b8 to your computer and use it in GitHub Desktop.
Get e Set on Cache
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);
}
}
}
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