In Memory Caching in .NET

<p>In the ever-evolving world of web development, one thing&rsquo;s for sure: speed matters. And one nifty trick we&rsquo;ve got up our sleeves to turbocharge our ASP.NET Core apps is caching.</p> <p>In this article, we&rsquo;re going to take a laid-back stroll through the fascinating world of caching, while focusing specifically on in-memory caching.</p> <h1>What&rsquo;s In Memory Caching, Anyway?</h1> <p>Before we dive into the how-tos, let&rsquo;s quickly break down in-memory caching.</p> <p>Think of it as your app&rsquo;s super-fast memory vault. It stores frequently used data right in your app&rsquo;s memory, so you can grab it in a flash without hitting the database or doing complex calculations.</p> <p>It&rsquo;s like having your favorite snacks right next to you on your desk instead of running to the kitchen every time you&rsquo;re hungry.</p> <p>In ASP.NET Core, it&rsquo;s one of your best friends for performance optimization, especially if you are making calls to the database to get data which remains the same for most of the time, like a list of user profiles, or some static web content maybe.<br /> For frequently changing data like stock prices, currency prices etc. it is best to go with calls to the main server or database to get fresh data instead of using a cache.</p> <h1><strong>In Memory Caching vs. Distributed Caching</strong></h1> <p>Now, let&rsquo;s chat about the two main caching flavors:&nbsp;<strong>in-memory caching</strong>&nbsp;and&nbsp;<strong>distributed caching</strong>.</p> <p><strong>In Memory Caching</strong>: This one&rsquo;s like keeping your candy stash in your room. It&rsquo;s super quick to access but doesn&rsquo;t work well if you need to share it across multiple rooms (or servers). Example: In Memory Caching in ASP.NET Core</p> <p><strong>Distributed Caching</strong>: Distributed caching, on the other hand, is like having a candy jar in the kitchen that everyone can access. It&rsquo;s perfect when your app has multiple instances or runs on different servers. Example: RedisCache</p> <p>Which one to choose? Well, it depends on your app&rsquo;s needs and is a slightly large topic to discuss.</p> <p><a href="https://medium.com/@ecphore/in-memory-caching-in-net-9454523a457d">Website</a></p>
Tags: .NET ASP.NET core