That way to build High-Performance APIs in .NET - Part 2: Caching

ngtduc693

Duc Nguyen Thanh

Posted on October 16, 2024

That way to build High-Performance APIs in .NET - Part 2: Caching

Hello, I’m Duc Nguyen (Duke)

But why?
Querying the database each time your application requests data that isn't changed frequently (such as lists of categories, configurations, or metadata) can result in needless overhead. You can improve response times and lessen the strain on your database server by avoiding duplicate database calls

How many ways to implement the Caching?

1.Memory Cache
Enter fullscreen mode Exit fullscreen mode
  • The Goal: The goal is to store frequently accessed data in the memory of your application so that it can be quickly retrieved.
  • Scope: Information is only stored in the cache for as long as the application runs or until it is invalidated.
  • Ideal for: Regularly accessed small to medium-sized data sets that don't change frequently.
  • How to implement: Using the MemoryCache (IMemoryCache) and the TryGetValue method
2.Distributed Cache
Enter fullscreen mode Exit fullscreen mode
  • The Goal: The goal of distributed caching is to enable multiple application instances to share the cached data by centrally storing the cache (such as Redis or SQL Server).
  • Scope: Perfect for multi-server cloud-hosted applications or large-scale applications.
  • Ideal for: Situations in which several instances or servers need to share the cache.
  • How to implement: you can refer to this post
3.HTTP Cache
Enter fullscreen mode Exit fullscreen mode

The client's (such as a browser or API consumer) response caching is managed by HTTP headers. To tell clients to cache responses for a predetermined amount of time or until particular conditions change, you can set headers in your controller actions.

  • How to implement: Cache-Control is a keyword, you can refer to this post to know how does it work
💖 💪 🙅 🚩
ngtduc693
Duc Nguyen Thanh

Posted on October 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related