开发者

Does the PHP function filemtime use up memory lesser than it has to when it would file_get_contents() a file?

开发者 https://www.devze.com 2023-03-06 11:15 出处:网络
I know it\'s kind of obvious, but take a look at it this way: Let\'s say I have a text file which contains a JSON data, and this file will be opened up by a certain PHP file say once per second every

I know it's kind of obvious, but take a look at it this way:

Let's say I have a text file which contains a JSON data, and this file will be opened up by a certain PHP file say once per second every day and do something with it. I was thinking of caching the contents onto memory using memcache. Now the problem is, how will it know if the contents of the text file changed? I figured I save the original filemtime to memcached and compare it with the current filemtime every single time.

Now here's where开发者_如何学C the problem arises, because I also have to do filemtime every single time. And I'm not sure if in this scenario, the memcache + filemtime setup would be a better choice or is it not much different than doing file_get_contents each time the script is ran getting fresh contents all the time?


I think filemtime() should be much faster that file_get_contents() if PHP does what it's right (by far) and uses adequate syscalls for getting the information from the underlying filesystem. Be careful though as filemtime() is cached by default, and has a resolution of a second:

http://php.net/manual/en/function.filemtime.php

Given that you're using memcache I'd say the overall performance also depends on several factors particular to your setup and files (what's the average size, for example - and your expected change rate, probably mapping to a particular memcache hit rate). You should probably benchmark using your real life data. You'll probably get a better answer on Stack Overflow though, I'm flagging your question.


filemtime will be faster than file_get_contents - but as per Kyle Smith's comment, looking at what you propose as a whole, the additional code and the difference in speed between reading from the OS disk cache vs memcache means that I would be very surprised if the memcached method is measurably faster, and there's the risk of introducing functional defects with the additional code required.

0

精彩评论

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