Difference between Absolute Expiration and Sliding Expiration

Asked

Viewed 542 times

7

I am doing a POC with generic cache in C# and followed a model that shows Absolute Expiration and Sliding Expiration. Some could explain these concepts better to me?

            //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;
    }

1 answer

3


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

absoluteExpiration: The time the inserted object expires and is removed from the cache regardless 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 when 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 left to expire, it will be waited another 1 minute before removing it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.