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

Time:05-24

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?

CodePudding user response:

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

  • Related