I am doing a POC with generic cache in C # and I followed a template that shows Absolute Expiration and Sliding Expiration . Could anyone explain these concepts better?
//This is for Absolute Expiration
string strCache = GenericCustomCache.GetItemToCache<string>("String", "Poc Cache Teste 1!", DateTime.Now.AddSeconds(20));
DateTime dtCache = GenericCustomCache.GetItemToCache<DateTime>("DT", DateTime.Now, DateTime.Now.AddSeconds(20));
Response.Write("Absolute Expiration<br/>");
Response.Write(strCache + dtCache.ToString() + "<br/>");
//This is for Sliding Expiration
string strCache1 = GenericCustomCache.GetItemToCache<string>("String1", "Good Moring!", TimeSpan.FromSeconds(20));
DateTime dtCache1 = GenericCustomCache.GetItemToCache<DateTime>("DT1", DateTime.Now, TimeSpan.FromSeconds(20));
Response.Write("Sliding Expiration<br/>");
Response.Write(strCache1 + dtCache1.ToString() + "<br/>");
Generic class:
internal static T GetItemToCache<T>(string key, object cacheObject, DateTime absoluteExpiration)
{
return GetItemToCache<T>(key, cacheObject, absoluteExpiration, Cache.NoSlidingExpiration);
}
/// <summary>
/// This is used to get item from cache using absoluteExpiration.
/// </summary>
internal static T GetItemToCache<T>(string key, object cacheObject, TimeSpan slidingExpiration)
{
return GetItemToCache<T>(key, cacheObject, Cache.NoAbsoluteExpiration, slidingExpiration);
}
/// <summary>
/// This is used to get item from cache.
/// </summary>
internal static T GetItemToCache<T>(string key, object cacheObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
T customObj;
if (HttpRuntime.Cache[key] != null)
{
customObj = (T)HttpRuntime.Cache[key];
}
else
{
//DateTime.Now.AddMinutes(30)
HttpRuntime.Cache.Insert(key, cacheObject, null, absoluteExpiration, slidingExpiration);
customObj = (T)HttpRuntime.Cache[key];
}
return customObj;
}