Understanding and Managing Server-Side (Output) Caching in .Net

When you check out a website, each user's request doesn't go to the database every time to respond to every bit of info. Instead, some data is pulled directly, making it faster and more efficient. This strategy helps the server handle lots of requests from different people without disturbing the database. The server keeps important stuff in its memory, so it can quickly answer when you ask for something. 

Now, you might be wondering, since we usually save data in the database, how does the server grab and store this data, and where exactly is it kept?

So the IIS(Internet Information Services) stands as the heartbeat of this operation. 

The operation name is Output Caching.

Output Caching

You can even set what kind of files will be stored and for which time. By following simple steps below.

  • Launch the Internet Information Services (IIS) Manager on your server.
  • Navigate to your web application in the Connections pane.
  • Double-click on the "Output Caching" feature.
  • In the "Output Caching" feature, explore the cached items, their expiration policies, and other relevant information.

Output Caching



But how to store the cache there?

So the first step is to install System.Web.Caching from nuget package


The System.Web.Caching namespace provides classes that allow you to implement caching within a web application running on the server side, typically hosted by a web server like Internet Information Services (IIS). When you use the System.Web.Caching namespace in an ASP.NET web application hosted on Internet Information Services (IIS), the caching mechanism stores data in the server's memory.

So there are a couple of methods inside this namespace to do CRUD operations with those caches.


HttpRuntime.Cache.Insert("yourkey","your value"); 

Here is how you can set the cache. However, you may notice that the above code doesn't specify the expiration of this caching item. This implies that the caching item will not be destroyed until the web application is stopped or restarted. Therefore, you need to add an expiration as well.

Here is another way to achieve this:

     string key, 
     object value, 
     System.Web.Caching.CacheDependency dependencies, 
     DateTime absoluteExpiration, 
     TimeSpan slidingExpiration 

Here, you know the key and value, but what about the other parameters?

Dependencies: A cache item with dependencies will be automatically removed from the cache when any of its dependencies change or are removed. 

However, if you want it to remain in the cache until it is explicitly removed or until other configured expiration conditions (such as absolute or sliding expiration) are met.

Absolute Expiration: You can set an exact time for the cache to expire. There are three ways to set its value:

  1. Absolute Expiration Time: AbsoluteExpiration is of type DateTime. You provide a specific date and time when you want the cache item to expire. Once this date and time are reached, the cache expires.
  2. Cache.NoAbsoluteExpiration: This constant value indicates that there is no specific absolute expiration time, and the item will remain in the cache until manually removed.
  3. Combining with Sliding Expiration:  If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.


1. Sliding Expiration ensures that if the data is accessed within the specified time interval the cache item lifespan will be extended by the interval value. For example, a session is added with 10 minutes expiration.

ex. TimeSpan.FromMinutes(5),  TimeSpan.FromSeconds(30), NoSlidingExpiration;

2. Combining with AbsoluteExpiration: If you are using slidingExpiration then the absolute expiration parameter must be NoAbsoluteExpiration.