PHP - file_exists 或数组缓存?
我有一个 PHP 代码,其中包含多个文件(如果存在)。可能需要包含 150 个文件。
在这种情况下使用 file_exists 会很慢吗?我应该构建一个数组来创建文件结构的缓存吗?
还有其他方法可以对文件结构进行某种缓存吗?
I have a PHP code which includes several files if they exists. It might be 150 files to include.
Is it slow to use file_exists in this case? Should I build an array to create a cache of the file structure instead?
Is there other ways to make some kind of cache of the file structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务单个请求期间有 150 个文件?
如果是这样,您必须减少这个数字,不是因为 150 个 file_exists,而是因为 150 个 include。
如果取决于请求的参数并且每个请求3-4个,那就可以了。
但您不能使用常规数组作为缓存。 PHP 不保留请求之间的变量。您不必重新考虑您的应用程序结构,而不是发明此类缓存。
150 files during serving single request?
If so, you have to reduce this number, not because of 150 file_exists but because of 150 includes.
If it depends on the requested parameters and being 3-4 per request, it's OK.
You cannot use a regular array as a cache though. PHP doesn't preserve variables between requests. Instead of inventing such cache you have tho rethink your application structure.
这是非常糟糕的主意。有两种行之有效的方法可以解决这个问题。
This is very bad idea. There are two proven ways around this.
@include($file)
include
在尝试打开文件时隐式检查文件是否存在。根据下面的评论:
file_exists
检查慢得多。@include($file)
include
implicitly checks whether the file exists when it tries to open it.Per comments below:
file_exists
check.