开发者

How to efficiently add metadata to an arbitrary file in .NET?

开发者 https://www.devze.com 2023-02-17 18:52 出处:网络
I\'m building a web shopping site. Each product that is sold can have several pictures attached to it as well as arbitrary attachments (like a PDF with a manual etc.) These resources are stored in our

I'm building a web shopping site. Each product that is sold can have several pictures attached to it as well as arbitrary attachments (like a PDF with a manual etc.) These resources are stored in our own ERP system, and are accessed via it's remote API. In reality the file can reside anywhere - in the DB, on a hard drive, on a network share etc. The API provides a unified interface of accessing it.

To improve performance I intend to cache these files on the webserver. There is a version counter mechanism in place which will allow me to effectively determine when the file has changed and invalidate the cache entry. However where do I store the version number for the file that is currently in the cache? The cache is essentially a single folder, which can be accessed by several ASP.NET worker processes on the webserver. There is no DB for the website itself - all the data comes from the ERP API.

Ideally this number would be stored together with the file itself somehow. So that when the file is deleted (say, the admin decides to clear the cache to improve disk usage), the metadata wouldn't linger around. So far I have two ideas:

  1. Write it in an ADS (Alternate Data Stream). There is no easy way of accessing it through .NET though. Libraries exist, but I'd prefer a more elegant solution (and I've no idea abo开发者_JAVA百科ut the performance either).
  2. Append it to the file itself. All file formats that I know of are pretty relaxed about data appended to the file. But I would need to remove it before transmitting the contents, and that would mean that I couldn't use the built-in TransmitFile - again leaves me worried about performance.

Is there any better way?


If it's just a version number, have you thought of including it in the filename? For example:

foo.txt.v131

for version 131 of foo.txt. One nice feature of this approach is that you can be writing the new version (e.g. 132) of the file while the webserver is still able to read 131. You'd still need to work out all the swap-over semantics of course...


If you know that the products are changing that often you can invalidate your cache every hour.

A more advanced way would be to store in a DB version the CRC value for each cached file. And then have a background service run (eg. via Cron) to check that files are changing and then invalidate.

0

精彩评论

暂无评论...
验证码 换一张
取 消