Skip to content

Instantly share code, notes, and snippets.

@agehlot
Last active March 14, 2021 04:21
Show Gist options
  • Save agehlot/071ba39df6a298f2122478d1f83786be to your computer and use it in GitHub Desktop.
Save agehlot/071ba39df6a298f2122478d1f83786be to your computer and use it in GitHub Desktop.
Helper methods for managing cache using MemoryCache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Threading;
using System.Web;
namespace Core.Foundation.Caching.CustomCaching
{
public static class MemoryCache
{
private static ObjectCache memCache = System.Runtime.Caching.MemoryCache.Default;
private static readonly object _memObj = new object();
/// <summary>
/// Sets the cache.
/// </summary>
/// <param name="cacheKey">The cache key.</param>
/// <param name="Container">The container.</param>
/// <param name="expireTime">The expire time.</param>
/// <param name="refreshTimeH">The refresh time h.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool SetCache(string cacheKey, object Container, int expireTime, int refreshTimeH)
{
Monitor.Enter(_memObj);
try
{
if (memCache.Contains(cacheKey))
{
RemoveCache(cacheKey);
SetCache(cacheKey, Container, expireTime, refreshTimeH);
}
else
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(expireTime);
if (refreshTimeH > 0)
{
cacheItemPolicy.SlidingExpiration = new TimeSpan(0, 0, 10);
}
memCache.Add(cacheKey, Container, cacheItemPolicy);
}
return true;
}
catch (Exception e)
{
return false;
}
finally
{
Monitor.Exit(_memObj);
}
}
/// <summary>
/// Removes the cache.
/// </summary>
/// <param name="cacheKey">The cache key.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool RemoveCache(string cacheKey)
{
Monitor.Enter(_memObj);
try
{
if (memCache.Contains(cacheKey))
{
memCache.Remove(cacheKey);
}
return true;
}
catch (Exception e)
{
return false;
}
finally
{
Monitor.Exit(_memObj);
}
}
/// <summary>
/// Gets the cache.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey">The cache key.</param>
/// <returns>T.</returns>
public static T GetCache<T>(string cacheKey)
{
Monitor.Enter(_memObj);
try
{
if (memCache.Contains(cacheKey))
{
object cachedItem = memCache[cacheKey];
if (cachedItem is T)
{
return (T)cachedItem;
}
}
return default(T);
}
catch (Exception e)
{
return default(T);
}
finally
{
Monitor.Exit(_memObj);
}
}
/// <summary>
/// Gets all cache keys.
/// </summary>
/// <returns>List&lt;System.String&gt;.</returns>
public static List<string> GetAllCacheKeys()
{
List<string> cacheKeys = new List<string>();
Monitor.Enter(_memObj);
try
{
cacheKeys = memCache.Select(kvp => kvp.Key).ToList();
return cacheKeys;
}
catch (Exception e)
{
}
finally
{
Monitor.Exit(_memObj);
}
return cacheKeys;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment