I've got a web-service serving from the MySQL database. I must create cache file to enhance the performance. The concept is when a in the end read data from DB and produce a text file. My real question is:

Let's say a customer-side user is being able to access the file when we're producing it?

We're using Light. In PHP there's flock() handles concurrency problem, but my understanding is the fact that it's just for when 2 PHP processes being able to access the file concurrently. Our situation differs.

I'm not sure whether this makes issues whatsoever. If that's the case, how do i prevent it?


A variety of optimisation options here

1) Are you currently while using MySQL queryCache - that can an enormous load from the database to begin with.

2) You can pull the file via a web proxy like squid (or Apache set up like a reverse caching proxy). I actually do this constantly and it is a very handy technique - create the file by fetching it from the url using wget for instance (that method for you to get it inside a cron job). The net proxy takes proper care of either delivering exactly the same file which was there before, or regenerating it if needs be.

3) You won't want to be moving your personal file securing solution within this scenario.

Based on your scenario, you might consider cacheing pages in something similar to memcache which is wonderful for high traffic situations, but possibly past the scope of the question.

Whenever a client-side access the file, it reads it because it is for the reason that moment. flock() is perfect for when 2 PHP processes being able to access the file concurrently.

Use a -> B switching to avert this problem.

E.g. : Let there be two copies of the cache file A and B, program should read these using a symlink, C.

When program is building the cache, it might customize the file that's not "current" I.e. if C connect to A, update B. Once update is done, switch symlink to B.

the next time, update A and switch symlink to some once update is done.

by doing this clients would not read personal files even though it is being up-to-date.

avoid using securing in case your cachefile is /tmp/cache.txt then it is best to regenerate the cache to /tmp/cache2.txt after which perform a

mv /tmp/cache2.txt /tmp/cache.txt 



the mv/relabel operation is atomic whether it happens within the same filesystem no securing needed

I'd solve it such as this:

  • While producing the brand new text file, save it to some temporary file (cache.tmp), this way that old file (cache.txt) has been utilized like before.
  • When generation is performed, remove that old file and relabel the brand new file
  • To prevent problems throughout that short time, your code should check wether cache.txt is available and retry for a while of your time.

Trivial but which should have the desired effect