Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a call to DB and put data in cache during application startup in .NET 5.0?

I have a method FetchUsers (it is stored in separate class) that is going to database and fetch all users that my application needs and then put it in the cache.

Now this code is executed in the custom middleware before each request, but I need this code to be executed only once, when application is started, and then every time get the data from cache, and only when data is expired from cache, we should go to db and fetch it. How can I do this in Program.cs file. I don’t want to create this class explicitly (like this: new FetchUsers(someDependencies)) is there way to do this in some other way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That pretty much sounds like you want to use the singleton pattern?
What is a singleton in C#?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading