Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive is reading file properties? .NET

We're experimenting with appending timestamps to some URL's to let things cache but freshen them when they do change. We have code that boils down to this:

DateTime ts = File.GetLastWriteTime(absPath);

where absPath is a MappedPath of a url. So the web server will be checking this file's last write time every time we serve up a link to the file. Kinda gives me the willies - should it?

like image 725
n8wrl Avatar asked Aug 25 '09 18:08

n8wrl


2 Answers

You should performance-test it, but off-hand I doubt it's any more expensive than testing a file's existence (e.g. whether it's read-only), and certainly less expensive than actually opening the file.

If (after testing) it you decide that it's a problem, you could also cache your calls to GetLastWriteTime (e.g. don't call it more than once every 5 seconds for any given file).

Also, I've never used it but if caching is a concern I hope you've considered delegating its implementation to some specialist like Squid instead of rolling your own.

like image 193
ChrisW Avatar answered Oct 17 '22 14:10

ChrisW


I have not tried this, but your question is relevant to a situation that I have been thinking about.

You did not indicate what data is changing? database, xml data etc.

ASP.NET caching does support updating the cache based on a variety of dependencies.

Check out this article in the sections of File-based Dependency, Time-based Dependency, and Key-based Dependency.

"Dependencies allow us to invalidate a particular item within the Cache based on changes to files, changes to other Cache keys, or at a fixed point in time. Let's look at each of these dependencies."

Here is the article:

http://msdn.microsoft.com/en-us/library/ms972379.aspx

Thanks

Joe

like image 26
Joe Pitz Avatar answered Oct 17 '22 15:10

Joe Pitz