background cache invalidation
i came up with a pattern for background cache invalidation this morning that seems solid (i need to noodle on it a bit, tho).
my problem is that some updates require the invalidation of *many* cache entries. for example, if i update a single blog entry, that could invalidate not only that one entry, but any number of pages that show that entry including the home page, and many list pages. even lists built to give the editor a pick list of blog entries is now invalid!
at first, i created a list of cache URIs that should be invalidated after each POST/PUT/DELETE. this works fine, but can be quite slow when lots of URI are in the list. next, i worked up a routine that looped through the list firing off a new thread for each item. bad idea! i ran out of threads and choked the server!
next i implemented a single loop, on a separate thread, that invalidated all the cache items. problem there was, if the list was long, some screens might not update soon enough. i had to refresh my edit screen pick list before i saw the updated version of the resource!
finally, i implemented a mix of immediate and background cache invalidation. Now the SqlXmlHandler
has two collections: ImmediateCacheUri
and BackgroundCacheUri
. that way, i can control which cache invalidations must be completed before yielding to the user and which can be done on a separate thread. not too bad.
here's how my ClearCache
method looks now: