Friday, February 24, 2012

BLOB to File Server Caching with ASHX

I am doing an interesting User Story this sprint. The app is a Silverlight app.
The files are uploaded to the file server and/or database.

What I want to do is build a caching system.
This is easy using asynchronous ASHX. The idea is to save the file to the DB as a BLOB. There will be an index - a primary key (pk) on the table. So with this index and a Url like the following we cache out to the User in Silverlight:
http:///cachier.ashx?pk=9

With this and making the ASHX asynchronous - ASHX is a Generic (HTTP) Handler, with a shorter lifecycle than a "page" - it should return immediately, so if numerous requests are queued up they will cache-back to the User, Silverlight Client, immediately and all of them will stream out much faster than if it were one at a time. The ASHX gets an async request, makes an async call to the DB to get the row referenced by the primary key (pk=9 in the above example) and returns to the client, thus releasing the client to make other requests. When the DB returns from the async DB request with the row referenced, the ASHX will check the file-cache and stream out directly from the file, but if not, the BLOB will be streamed out in moderately sized chunks to the client, and to the file system so future requests can be handled faster.

The files will build up, so one additional piece is semi-required:
a "sweeper" to clean up the older, less referenced cache files on a regular basis.

If I can, I will post some of my code here to illustrate.

Thanks,
Dan