Are you aware of any serious performance problems with checking if a file exists before you open it and send it? The context for this is a web application that serves up files dynamically via an ASP page. I was told that there is no check for whether or not the file exists because the database stores this information and shouldn't be wrong. It is of course sometimes wrong for any number of reasons so we end up sending back nothing leaving the user unhappy.
在打开并发送文件之前,您是否知道检查文件是否存在严重的性能问题?此上下文是一个Web应用程序,它通过ASP页面动态提供文件。我被告知没有检查文件是否存在,因为数据库存储了这些信息,不应该是错误的。当然,由于各种原因,它有时会出错,所以我们最终不会发回任何让用户不满意的事情。
My instinct is that checking for a file's existence is so cheap that you shouldn't worry about it but I was told otherwise. Our storage solution is a pretty high powered one (not just an IDE drive on the web server) if that helps. My question is basically: Is my instinct correct?
我的直觉是检查文件的存在是如此便宜,你不应该担心它,但我被告知否则。如果有帮助的话,我们的存储解决方案是一个非常强大的存储解决方案(不仅仅是Web服务器上的IDE驱动器)。我的问题基本上是:我的直觉是否正确?
Thanks!
Note: These files are never just deleted. Something has to go wrong for them to be missing but this happens a few times a week. Also, the reason I want to check for a file's existence is because I can return an alternate asset that is on disk so I'd like to be able to have that logic all in one spot rather than deal with catching an exception and dealing with that in that context.
注意:这些文件永远不会被删除。他们错过了一些必须出错的事情,但这种情况每周发生几次。另外,我想检查文件存在的原因是因为我可以返回磁盘上的备用资产,所以我希望能够在一个地方拥有该逻辑而不是处理捕获异常并处理在那种情况下。
11 个解决方案
#1
5
Even if you check it exists just before you try to serve it, it could be deleted between the check and you serving it.
即使您在尝试提供服务之前检查它是否存在,也可以在支票和服务之间删除它。
The problem here is that you send back nothing if you can't serve the file. Trying to serve a non-existent file should throw an exception (or whatever the equivalent is on your platform) - you should handle that exception by returning an appropriate "Sorry, we couldn't find your file" error page.
这里的问题是,如果您无法提供文件,则不发回任何内容。尝试提供不存在的文件应该抛出异常(或者平台上的等价物) - 您应该通过返回适当的“抱歉,我们找不到您的文件”错误页面来处理该异常。
#2
3
If the file not being there is an exceptional circumstance (as you say the db is always right), you should not check for it.
如果不存在的文件存在异常情况(正如您所说的数据库总是正确的那样),则不应检查它。
If its not there you get an exception and handle it accordingly. That seems to be the way you say the system works, and I would handle it as such.
如果它不在那里你得到一个例外并相应地处理它。这似乎是你说系统工作的方式,我会这样处理它。
One more note, If your saying that a file not being there when opened is just returning nothing, then that tells me there is a design flaw in your exception hanlding, and your lower level is not bubbling the exception far up enough to be properly handled so you can convey an error message back to the client.
还有一点需要注意的是,如果你说打开时不存在的文件只是没有返回任何内容,那就告诉我你的异常中有一个设计缺陷,而你的较低级别并没有将异常冒泡到远远不足以正确处理因此您可以将错误消息传回客户端。
With proper exception handling/bublling you should have no problem returning an alernate asset. Rethink your exception handling rather than redesigning the way the system is supposed to work.
通过适当的异常处理/冒泡,您应该没有问题返回alernate资产。重新思考您的异常处理,而不是重新设计系统的工作方式。
#3
1
You shouldn't experience any serious performance issues so long as you're not using some sort of really weird storage scheme.
只要您没有使用某种非常奇怪的存储方案,就不应该遇到任何严重的性能问题。
#4
1
I'm not aware of any major performance problems with this approach, but checking for a file's existence before opening it can have unexpected results (another process can delete the file between the check and opening it).
我不知道这种方法有任何重大的性能问题,但在打开文件之前检查文件是否存在可能会产生意外结果(另一个进程可以在检查和打开文件之间删除文件)。
#5
0
I really don't know if it is a cheap or expensive task, but, considering that not checking for the existence of the file could result in an unpleasant response to the client I would say it's not expensive.
我真的不知道这是一个便宜或昂贵的任务,但是,考虑到不检查文件的存在可能会导致对客户端的不愉快响应,我会说它并不昂贵。
I do have some web apps where I check for file existence and is working just fine.
我有一些网络应用程序,我检查文件存在,并正常工作。
#6
0
Are you redirecting to the file, or reading and serving up the contents of the file through code?
您是重定向到文件,还是通过代码读取和提供文件的内容?
If the first, what happens if the file for some reason doesn't exist? Standard 404 error, or do you have a specialized error page? Is that acceptable for that case?
如果是第一个,如果由于某种原因文件不存在会发生什么?标准404错误,或者您是否有专门的错误页面?这种情况可以接受吗?
If the latter, simply open the file and handle the exception appropriately.
如果是后者,只需打开文件并适当处理异常。
#7
0
File IO is generally costly in comparison to database or in-memory reads, but I have heard the old "the database stores the information so it shouldn't be wrong" too many times when hunting down a system crash or unhandled exception. So I would check for existence and recover elegantly unless your performance requirements are unusually high.
与数据库或内存中读取相比,文件IO通常是昂贵的,但是我在听到系统崩溃或未处理的异常时听到过旧的“数据库存储信息所以它不应该是错误的”太多次。因此,除非您的性能要求异常高,否则我会检查是否存在并优雅地恢复。
#8
0
Performance wise there should be essentially no impact. The slow part here is going to disk to read a file header, but if you're going to load the file you'll have to do that anyway and any decent storage system (pretty much any one from the past couple of decades) will cache the read from the check so that part of the read is much faster when you actually open the file. Having said that, I agree with mattlant that it would be generally better to catch the file not there exception and deal with it properly if the file is generally expected to be there.
性能方面应该基本上没有影响。这里缓慢的部分是转到磁盘读取文件头,但如果你要加载文件,你必须这样做,任何体面的存储系统(几乎是过去几十年中的任何一个)都将从检查中缓存读取,以便在实际打开文件时,部分读取速度更快。话虽如此,我同意mattlant,如果通常预期文件存在,那么通常更好的方法是捕获没有异常的文件并妥善处理它。
#9
0
If you're going to open it immediately after then checking for it's existance is essentially free.
如果您要在检查之后立即打开它,那么它的存在基本上是免费的。
#10
0
Try opening the file and then streaming it. Is that a possibility? That way if you're unable to open and stream it then you will be unable to send it and can action the appropriate error handling.
尝试打开文件,然后流式传输。这有可能吗?这样,如果您无法打开并流式传输,那么您将无法发送它并可以执行相应的错误处理。
#11
0
If not checking for the file causes confusion for the user and/or doesn't give them what they need, or at least display an error message indicating the problem - then it doesn't matter what the cost is.
如果没有检查文件导致用户混淆和/或没有给他们他们需要的东西,或者至少显示一个指示问题的错误消息 - 那么成本是多少并不重要。
The application needs the check.
该应用程序需要检查。
#1
5
Even if you check it exists just before you try to serve it, it could be deleted between the check and you serving it.
即使您在尝试提供服务之前检查它是否存在,也可以在支票和服务之间删除它。
The problem here is that you send back nothing if you can't serve the file. Trying to serve a non-existent file should throw an exception (or whatever the equivalent is on your platform) - you should handle that exception by returning an appropriate "Sorry, we couldn't find your file" error page.
这里的问题是,如果您无法提供文件,则不发回任何内容。尝试提供不存在的文件应该抛出异常(或者平台上的等价物) - 您应该通过返回适当的“抱歉,我们找不到您的文件”错误页面来处理该异常。
#2
3
If the file not being there is an exceptional circumstance (as you say the db is always right), you should not check for it.
如果不存在的文件存在异常情况(正如您所说的数据库总是正确的那样),则不应检查它。
If its not there you get an exception and handle it accordingly. That seems to be the way you say the system works, and I would handle it as such.
如果它不在那里你得到一个例外并相应地处理它。这似乎是你说系统工作的方式,我会这样处理它。
One more note, If your saying that a file not being there when opened is just returning nothing, then that tells me there is a design flaw in your exception hanlding, and your lower level is not bubbling the exception far up enough to be properly handled so you can convey an error message back to the client.
还有一点需要注意的是,如果你说打开时不存在的文件只是没有返回任何内容,那就告诉我你的异常中有一个设计缺陷,而你的较低级别并没有将异常冒泡到远远不足以正确处理因此您可以将错误消息传回客户端。
With proper exception handling/bublling you should have no problem returning an alernate asset. Rethink your exception handling rather than redesigning the way the system is supposed to work.
通过适当的异常处理/冒泡,您应该没有问题返回alernate资产。重新思考您的异常处理,而不是重新设计系统的工作方式。
#3
1
You shouldn't experience any serious performance issues so long as you're not using some sort of really weird storage scheme.
只要您没有使用某种非常奇怪的存储方案,就不应该遇到任何严重的性能问题。
#4
1
I'm not aware of any major performance problems with this approach, but checking for a file's existence before opening it can have unexpected results (another process can delete the file between the check and opening it).
我不知道这种方法有任何重大的性能问题,但在打开文件之前检查文件是否存在可能会产生意外结果(另一个进程可以在检查和打开文件之间删除文件)。
#5
0
I really don't know if it is a cheap or expensive task, but, considering that not checking for the existence of the file could result in an unpleasant response to the client I would say it's not expensive.
我真的不知道这是一个便宜或昂贵的任务,但是,考虑到不检查文件的存在可能会导致对客户端的不愉快响应,我会说它并不昂贵。
I do have some web apps where I check for file existence and is working just fine.
我有一些网络应用程序,我检查文件存在,并正常工作。
#6
0
Are you redirecting to the file, or reading and serving up the contents of the file through code?
您是重定向到文件,还是通过代码读取和提供文件的内容?
If the first, what happens if the file for some reason doesn't exist? Standard 404 error, or do you have a specialized error page? Is that acceptable for that case?
如果是第一个,如果由于某种原因文件不存在会发生什么?标准404错误,或者您是否有专门的错误页面?这种情况可以接受吗?
If the latter, simply open the file and handle the exception appropriately.
如果是后者,只需打开文件并适当处理异常。
#7
0
File IO is generally costly in comparison to database or in-memory reads, but I have heard the old "the database stores the information so it shouldn't be wrong" too many times when hunting down a system crash or unhandled exception. So I would check for existence and recover elegantly unless your performance requirements are unusually high.
与数据库或内存中读取相比,文件IO通常是昂贵的,但是我在听到系统崩溃或未处理的异常时听到过旧的“数据库存储信息所以它不应该是错误的”太多次。因此,除非您的性能要求异常高,否则我会检查是否存在并优雅地恢复。
#8
0
Performance wise there should be essentially no impact. The slow part here is going to disk to read a file header, but if you're going to load the file you'll have to do that anyway and any decent storage system (pretty much any one from the past couple of decades) will cache the read from the check so that part of the read is much faster when you actually open the file. Having said that, I agree with mattlant that it would be generally better to catch the file not there exception and deal with it properly if the file is generally expected to be there.
性能方面应该基本上没有影响。这里缓慢的部分是转到磁盘读取文件头,但如果你要加载文件,你必须这样做,任何体面的存储系统(几乎是过去几十年中的任何一个)都将从检查中缓存读取,以便在实际打开文件时,部分读取速度更快。话虽如此,我同意mattlant,如果通常预期文件存在,那么通常更好的方法是捕获没有异常的文件并妥善处理它。
#9
0
If you're going to open it immediately after then checking for it's existance is essentially free.
如果您要在检查之后立即打开它,那么它的存在基本上是免费的。
#10
0
Try opening the file and then streaming it. Is that a possibility? That way if you're unable to open and stream it then you will be unable to send it and can action the appropriate error handling.
尝试打开文件,然后流式传输。这有可能吗?这样,如果您无法打开并流式传输,那么您将无法发送它并可以执行相应的错误处理。
#11
0
If not checking for the file causes confusion for the user and/or doesn't give them what they need, or at least display an error message indicating the problem - then it doesn't matter what the cost is.
如果没有检查文件导致用户混淆和/或没有给他们他们需要的东西,或者至少显示一个指示问题的错误消息 - 那么成本是多少并不重要。
The application needs the check.
该应用程序需要检查。