Using multithreading: Thread-safe caching in ASP.NET HttpModules on newest questions tagged multithreading – Stack Overflow
Here’s the scenario
I have an ASP.NET 4.0 application which has a LOT of legacy URL rewrites. I want to build an HTTP module that will look at a database of these rewrites and do a 301 redirect if needed. There’s tens of thousands of records in this table.
So obviously I need to do some sort of caching so I’m not hitting the database and searching through all those records for each page request.
One of our requirements is to have a fast start up time when our application recycles for some reason (worker process recycles or what have you). So I don’t want to load the whole table at app start up. The app start up is already excruciatingly slow.
My brilliant/ridiculous idea is to,
- At app start up in my HttpModule create an empty dictionary to hold rewrites.
- Start a background worker thread. The app continues starting up.
- The background worker thread updates the dictionary with say, oh, the most 1000 most used rewrites.
My questions are:
- Is this ridiculous?
- Is there such a thing as
a thread-safe dictionary? The
background thread might be updating
the dictionary while new requests
are coming in.
- Would all the locking
that goes on in a thread-safe
collection slow down incoming requests?
Using multithreading: using-multithreading