admin管理员组文章数量:1295927
I have a WCF project where I have some dependencies that are really expensive to start up, as in 2+ seconds per call. The dependencies are instantiated with a sting pair, kind of like username/password, so they can't just be reused in a pool between calls to the API.
Currently we are not using dependency injection and I want to rewrite the project as an ASP.NET Web API project with DI and a cache so we have the dependencies with the most common string pairs already loaded into memory.
I would like help with the two main components of this problem.
- Best approach for adding the cache?
- How do I control the lifetime of these cached dependencies with DI?
I have a WCF project where I have some dependencies that are really expensive to start up, as in 2+ seconds per call. The dependencies are instantiated with a sting pair, kind of like username/password, so they can't just be reused in a pool between calls to the API.
Currently we are not using dependency injection and I want to rewrite the project as an ASP.NET Web API project with DI and a cache so we have the dependencies with the most common string pairs already loaded into memory.
I would like help with the two main components of this problem.
- Best approach for adding the cache?
- How do I control the lifetime of these cached dependencies with DI?
- .NET has great documentation – roten Commented Feb 12 at 9:51
1 Answer
Reset to default 1It's important to consider the difference in lifetime between a cache and a DI container, as the easiest answer will largely depend on the number of different instances of your expensive dependency you have.
DI containers typically have 3 scopes or lifetimes
- A new instance every time a dependency is resolved (transient)
- A new instance per call into the application (scoped)
- One instance in the lifetime of the application (singleton)
A cache contains potentially millions of records, each of which has a Time To Live or Expiry, which may or may not be refreshed upon a cache retrieval.
If you only have a few expensive dependencies then it makes sense to register them in the DI container as keyed services, they will all get created when the app starts up and they can be retrieved from the container via their key.
If you are going to have lots of instances, perhaps one per user with a large number of concurrent users, then a cache is the way to go. The simplest implementation is to use an IMemoryCache
but there are other options available. IMemoryCache
has a method GetOrCreate
or GetOrCreateAsync
. If your dependencies are complex and you want to make this work with DI then you can wrap the IMemoryCache in a class and inject a factory class into it to aid in object creation.
本文标签: cCaching and lifetime in ASPNET Web APIStack Overflow
版权声明:本文标题:c# - Caching and lifetime in ASP.NET Web API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741614723a2388461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论