PHP: Does regular calls to the same file with fopen keep it in memory?

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Reaction score
13
I'm running a server with NGINX and phpfpm (PHP 5.3.3) and I have a small text file (150kb) with several categories I read from to populate 50 fields randomly when visiting a web page.

Since MySQL would be overkill (in my opinion, if you have a reason why I should have MySQL for a couple of hundred categories that gets random reads, go for it)...

I was wondering if NGINX (or PHP) is smart enough to keep the txt file I open up (with fopen) to read from, open in memory. So far disk read's have been minimal compared to the disk writes I have (mostly to the access log's i assume) and I receive several thousand impressions daily.

I haven't noticed any slow downs in server performance at all. But since I'm going to add another dimension into my web application, also using a small .txt file (since they don't need to be updated/inserted to, just read)... this question popped into my head. Does NGINX/PHP keep this file in memory for faster access... or read the disc each time?
 
Last edited:
The underlying OS will have a low-level block cache which will cache the file and prevent you having to hit the disk. In addition PHP caches file stats but only for each script execution.

Basically, as long as the file is quite static I don't think the disk will ever be a bottleneck for you.

PHP doesn't share data between processes, so caching in PHP often doesn't work the way you might expect. For that you'll need APC or similar (but only when you are up to a million hits a day or so: remember, never optimize too early :) )
 
Last edited:
Agree with the above. The OS will pick up that the file is accessed often, and push it into memory. Sometimes it will pick up patterns (pre-fetching for example will pick up a pattern, like if one file is usually opened after another, it will be put into memory just in case... even if it isn't hit).

You are looking for the term "File Caching". File caching is found in all Operating Systems (ok, all major ones, please don't point out some obscure OS made in someone's back room in the 10th grade).

Some reading here will explain how it is done in windows: http://dotnetperls.com/windows-file-cache
 
Thanks for the response guys. I figured somewhere a long the line it should cache. The file is quite static. Only changes every few months and even though I did consider working with sqlite (most of my desktop utilities I write uses sqlite), I really feel this isn't required (less resources? *shrug*)

I know how to scale projects since I've been doing this for a decade now (and usually work from the ULTIMATE AWESOME version, to the most simplistic form of it), so I'm pretty confident my needs are met with just using file-based access.

I'm really glad the caching is done by the OS (which explains why the READ on the disc is so low to begin with since the WRITE is several hundred percent more due to the logs kept) and not PHP or NGINX themselves.

@shogun, I use CentOS 5.5 64bit but the reading material came in handy :)
 
@shogun, I use CentOS 5.5 64bit but the reading material came in handy :)

Yeah, I figured it would be the same for linux seen as the article says windows basically borrowed it's caching model from unix / linux.
 
i love the smell of page faults in the morning.
 
Top
Sign up to the MyBroadband newsletter
X