Education logo

Learn This for your Coding Interview!

What is Caching in ASP.NET?

By Ivaylo ManolovPublished 4 months ago 3 min read
1

One of the probably most important topics in todays world is, caching.

You : Caching … I have heard about it. Isn’t this when you store something?

Me : Exactly that, you are way above average… Are you sure you need this article ?

You : Well, we store information but why ? Can’t we get everything that we need at any point of time?

Me : Yes, exactly. You can, but why would we want to go to a disk for retrieving the same information that 1000 clients need, when we can go once and store it somewhere in our application.

Whenever a client needs it, we will just pick it from the RAM and send it to them.

You : OOOOH, I will follow you. I like you a lot. You are my favorite person.

Me : Power of writing, gals. I can make you say anything :)

Now, that we have established the fact that you most probably like me a lot, let’s get right into it!

Caching Types

In ASP.NET, caching is a technique used to store frequently accessed data in memory for faster retrieval and improved performance. There are several types of caching available in ASP.NET. Here are some of the commonly used types:

  • Output Caching: Output caching allows you to cache the rendered output of an entire page or a user control. This means that instead of dynamically generating the page content on each request, the cached version is served, reducing the processing time and server load.
  • Fragment Caching: Fragment caching allows you to cache specific sections or fragments of a page. This is useful when only certain parts of a page are dynamic or expensive to generate. By caching those sections, you can improve performance while keeping other parts of the page dynamic.
  • Data Caching: Data caching involves caching frequently accessed data, such as database queries or expensive calculations. ASP.NET provides the Cache object, which allows you to store data in memory for a specified duration or until it is explicitly removed. This helps reduce the load on the database or expensive computations.
  • Partial Page Caching: Partial page caching is similar to fragment caching but provides more granular control. You can cache specific regions of a page, and the cached content can be shared across multiple pages or user controls. This technique is useful when you have reusable components that are shared across different pages.
  • SQL Caching: ASP.NET provides a SQL cache dependency feature that allows you to cache data from a SQL Server database. When the underlying data changes, the cache is automatically invalidated, ensuring that you always have the latest data.
  • Donut Caching: Donut caching is a combination of output caching and fragment caching. It allows you to cache the entire page while still keeping some sections dynamic. By defining certain sections as “donut holes,” you can cache most of the page but leave specific parts to be dynamically generated on each request.

In ASP.NET, the terms “output caching” and “page caching”, which is where you are going to see it most of the times, are sometimes used interchangeably, but they can have different meanings depending on the context. Let me clarify:

  • Output Caching: Output caching refers to the caching of the rendered output of a web page or a user control. It can be applied to individual sections (fragments) of a page or the entire page itself. The cached output is stored in memory and served to subsequent requests without executing the server-side logic or rendering the page again. This technique improves performance by reducing processing time and server load.
  • Page Caching: Page caching, on the other hand, specifically refers to caching the entire HTML output of a page, including both static and dynamic content. When a page is cached, subsequent requests for the same page are served directly from the cache, without executing the page’s code-behind logic or accessing data sources. This type of caching improves performance by eliminating the need to regenerate the entire page for each request.

So, in summary, output caching is a broader term that encompasses caching of both individual fragments and entire pages, whereas page caching specifically refers to caching the complete output of a page.

Conclusion

    A very big thing that I seem to be missing here is the code.
    I also want to include code examples, so you can better understand what's going on and how to perform the caching. I always compile some examples for my articles. However, I can't seem to embed code segments here.
    If you have any ideas how I can achieve it, please comment on this post. For now, only the theory will be presented in my articles.
    Thank you for being with me and I will see you in the next one!
    Happy coding, engineers!

interview
1

About the Creator

Ivaylo Manolov

I am a senior software engineer working for a big software tech company in the Video Management Software industry.

I love reading, learning, teaching and writing.

I write about coding interview questions, answers and solutions

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.