Difference between Absolute Expiration and Sliding Expiration

7

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;
    }
    
asked by anonymous 17.07.2017 / 19:48

1 answer

3

Both allow you to set the expiration time of your data in the cache, so I understood here , it works like this:

absoluteExpiration: The time that the inserted object expires and is removed from the cache independent of when the object is accessed.

Example:

string cacheData = "Seu dado";
Cache.Insert("AbsoluteCacheKey", cacheData, null, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);

Your data will be removed from the cache after 1 minute.

slidingExpiration: The time the object will be removed from the cache if it has not been accessed.

Example:

string cacheData = "Seu dado";
Cache.Insert("SlidingExpiration", cacheData, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));

Your data will be removed from the cache after 1 minute, but if it is accessed with 30 seconds to expire, it will be waited another 1 minute before removing it.

    
17.07.2017 / 22:29