Which to choose? It all depends on the scope and duration of the cache’s validity.
Let’s me explain a little bit when to use Scoped lifetime and when to use the Singleton lifetime in this new article.
Caching per Request (Scoped Lifetime)
What are the use cases for the Scoped lifetime?
- Request-Specific Data: Caching data that is specific to a single request and should not persist beyond that request.
- Avoiding Repeated Computations: If certain computations or data retrievals are expensive and the result is needed multiple times within the same request, caching within the request’s scope can save resources.
What are real-life examples?
- Database Queries: If a particular database query is performed multiple times during a single request and the result is the same, caching the result within the request can improve performance.
- User Permissions: Caching the permissions of a user for the duration of a request can avoid repeated checks against an authorization service.
It could be implemented as follows:
|
|
Caching per Application Lifetime
What are the use cases for the Singleton lifetime?
- Global or Shared Data: Caching data that is shared across multiple requests and does not change frequently.
- Expensive Operations: Data that is expensive to fetch or compute, and should be reused across different requests to avoid repeated overhead.
What are real-life examples?
- Configuration Data: Application-wide configuration settings that are loaded once and used throughout the application’s lifetime.
- Static Data: Reference data that does not change often, such as country lists or product categories.
It could be implemented as follows:
|
|
Conclusion
While both scoped and singleton lifetimes have their use cases for caching, the choice depends on the nature of the data and the requirements of your application.
Scoped caching is less common than singleton caching but is useful for request-specific data that needs to be reused within a single request.
Singleton caching is more common for data that is shared and relatively static across the entire application.
Sources
You can read more from the following resources:
- Microsoft Documentation:
- Service lifetimes in dependency injection
- This article explains the three lifetimes (Transient, Scoped, Singleton) and provides guidance on when to use each one.
- Microsoft Learn Module:
- Dependency injection in .NET
- This module provides an in-depth look at dependency injection in .NET, including examples and best practices for using different service lifetimes.
- Stack Overflow:
- When to use AddTransient vs AddScoped vs AddSingleton in ASP.NET Core?
- This thread discusses the practical scenarios and implications of using different lifetimes, with community-provided examples and insights.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.