DEV Community

Cover image for That way to build High-Performance APIs in .NET - Part 2: Caching
Duc Nguyen Thanh
Duc Nguyen Thanh

Posted on

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay