简体   繁体   English

Symfony 2资产,检查文件是否存在性能

[英]Symfony 2 asset, check if file exists performance

on symfony 2, using Twig you can use the asset() function to link an image in the way 在symfony 2上,使用Twig可以使用asset()函数以一种方式链接图像
{{ asset('path_to_the_image') }} , now if the file does not exists then the src of the file keeps the same, {{ asset('path_to_the_image') }} ,现在,如果文件不存在,则文件的src保持不变,

Thinking about that, I was tempted to create another Twig function on my TwigExtensions , in order to check the file existence, in order to do the following, If the file exists then I will use the given url and if does not exists then I will change the result of the function to a default image that I will use as a not_image image. 考虑到这一点,我很想在TwigExtensions上创建另一个Twig函数,以检查文件是否存在,以执行以下操作:如果文件存在,则将使用给定的url,如果不存在,则将将函数的结果更改为默认图像,该图像将用作not_image图像。 The motivation for this function is to always show an image to the user. 此功能的动机是始终向用户显示图像。

Now, my question. 现在,我的问题。

I can't figure out the performance issues with this approach because I will check the file twice, the first time to check if exists and the other time is the request that asks for the file. 我无法弄清楚这种方法的性能问题,因为我将检查文件两次,第一次是检查文件是否存在,而第二次是请求文件的请求。 And the usual thing to do is to put the asset address and in case the file does not exists replace it with some default file using javascript. 通常要做的是放置资产地址,如果文件不存在,则使用javascript将其替换为某些默认文件。

I will use php's file_exists function, and on the manual I have read that is very inexpensive and that in case that the file indeed exists the result is cached to avoid performance issues. 我将使用php的file_exists函数,并且在手册上我读到它非常便宜,并且在文件确实存在的情况下,结果将被缓存以避免性能问题。

Thanks in advance... 提前致谢...

file_exists triggers a read access to your file system (or, even just the FS metadata). file_exists触发对文件系统(或什至只是FS元数据)的读取访问。 This is very inexpensive indeed. 这确实非常便宜。 Keep in mind that, when running a Symfony application, you're usually accessing hundreds of PHP files alone. 请记住,在运行Symfony应用程序时,通常只需要访问数百个PHP文件。

The result of file_exists is cached indeed, but only while during the execution of a script. 确实缓存了file_exists的结果,但仅在脚本执行期间才缓存。 So, if you call file_exists several times within one script execution (and don't call clearstatcache in between), the result is cached. 因此,如果您在一次脚本执行中多次调用file_exists (并且在两次脚本之间不调用clearstatcache ),则结果将被缓存。 If you call the script again later, PHP will look for the file again. 如果稍后再次调用该脚本,PHP将再次查找该文件。


However, if you really worry about performance, you shouldn't check for the files' existence “on the fly”, but instead create a Symfony command that checks if all linked assets are valid during building or deployment. 但是,如果您真的担心性能,则不要“即时”检查文件是否存在,而应创建一个Symfony命令,该命令检查所有链接资产在构建或部署期间是否有效。

This command would basically render all pages, and your custom asset function would, instead of returning a dummy image, throw an exception. 该命令基本上将呈现所有页面,并且您的自定义资产函数将抛出异常,而不是返回虚拟图像。 This way, you only need to check once, during deployment, if all assets are valid. 这样,您只需在部署期间检查一次所有资产是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM