简体   繁体   中英

Implementing database caching in ASP.NET

I'm considering implementing sql database caching using the following scheme:

  1. In the ASP.NET webapplication I want a continuously running thread that check's a table, say dbStatus, to see if field dbDirty has been set true. If so, the local in-memory cache is updated, querying a view in which all needed tables are present.
    1. When any of the tables in the view is updated, a trigger on that table is fired setting dbStatus.dbDirty true. So this would mean I have to add a on insert,update,delete trigger on those tables

One of the reasons I want to implement such a caching scheme is that the same database is used in a Winform version of this application.

My question: is this a viable approach?

Many thanks in advance for helping me with this one, Paul

This is a viable approach.

The main problem you need to be aware of is that ASP.NET worker processes can exit at any time for many reasons (deployment, recycle, reboot, bluescreen, bug, ...). This means that your code must tolerate being aborted (in fact just disappearing) at any time.

Also, consider that your app can run two times at the same time during worker recycling and if you run multiple servers for HA.

Also, cross-request state in a web app requires you to correctly synchronize your actions. This sounds like you might need to solve some race conditions.

Besides that this approach works.

Consider incrementing a version number instead of a boolean. That makes it easier to avoid synchronization issues such as lost updates because there is no need to reset the flag. There is only one writer. That's easier than multiple writers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM