ASP.NET Core Interview Question #182

What are absolute and sliding cache expiration?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

Absolute expiration removes or expires an entry after a fixed time or at a fixed timestamp regardless of how frequently it is accessed.

Detailed Explanation

Absolute expiration removes or expires an entry after a fixed time or at a fixed timestamp regardless of how frequently it is accessed.

Sliding expiration extends an entry's lifetime when it is accessed, provided it is used again within the sliding window. A frequently accessed entry can therefore remain cached for a long time.

Combining a sliding expiration with an absolute maximum can prevent an entry from remaining indefinitely.

Code Example

cache.Set("key", value, new MemoryCacheEntryOptions
{
    SlidingExpiration = TimeSpan.FromMinutes(5),
    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
});