Question

I'm working on a MVC website for displaying products. There's a requirement to keep track of every product that user views and store the log in database.

Because I don't want to hit database every time the page loads, I'm trying to store the log in Application level cache. The question is: how can I schedully dump the data in the cache into database? Is there any framework that has done this for me, or any best practice to this problem?

Was it helpful?

Solution

ASP.NET is not designed to host background tasks/jobs, but it's possible to do it, although it won't be 100% reliable.

Create a worker class that contains a timer that fires every X interval and empties the app cache and saves the data to the database. In global.asax, create a handler for Application_Start and call the worker class' init method from there.

Also expose a flush method on your worker class that you call from the Application_Stop event handlar in global.asax, to make sure you flush any outstanding data before the application shuts down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top