Forum Moderators: coopster
I want to have some pages write out a static version, so my webserver can serve that if it's present.
Of course I want to be sure that there wouldn't be an instance where two processes try to write to the file at the same time.
Thanks.
and without a special renamer-process, you definetly will still have a race-condition: A writes his unique file, B writes his unique file, A renames, B renames. A's changes are lost.
Yes it won't be instant however we are talking about cache here, if such cache file is active 1 second past first user request then it is reasonable time for most cases.
The name for filename should really include checksum for its contents, this way if A writes unique filename and then B writes it - with the same content, then overwriting same file won't matter since content was the same, otherwise filename might be different.
I think insofar as file renaming is concerned then this race condition will be handled easily by OS, something that you can't expect to happen efficiently and easy with file writing - locks may sound convinient but it's best to avoid them.
which functions do you mean?
>>Yes it won't be instant however we are talking about cache here, if such cache file is active 1 second past first user request then it is reasonable time for most cases.
you're right ... but why not make it "right" ;)
>>locks may sound convinient but it's best to avoid them.
why? if the environment is somewhat stable, they'll do the job.
locks are bad in that they will block processing - if we are talking about race conditions this automatically implies that there is a good probability that the same resources will be accessed at the same time (hence locking is needed), so this means that with lock other processes will be _blocked_ awaiting end of lock. Locking is convenient for programmers as it allows poor "serial" algorithms to be used, however the drawback is that scalability is not going to be good. Sorry for so much text - you said you wanted to get it right :)
So flock is only needed if you have separate read and write 'phases' and you don't want to lose updates.
While it's true that file locking blocks other processes and other instances of the same process, it only blocks them when they try to write to the same file -- i.e. it only blocks them when it has to, and when you want it to. If you check for 'fairly-fresh cached version exists' before you generate another cached static copy, then instances of locked-file waits should be rare indeed.
Jim
thanks for pointing me towards tempnam(), didn't know of that yet. As stated in the comments, it's not possible to do the same thing in php itself and that's mainly what had me wondering. thanks again.
The thing to watch with file based caching is to avoid having too many files - NTFS for example performs very poorly when number of files in a directory exceeds reasonable number: I've had 350-400k small files once in a single directory and boy that sucked :(
Suppose cache is totally necessary, ok, then lets say you have 100000 different products for which cache needs to be created, you definately don't want that many files so generate single big cache file for all products _offline_ and keep in database offsets for each of the products and length of data to read for that cache value. This way you'd avoid auto-generating cache on the fly and effectively cache everything without getting into a big mess with lots of files (backing those up is a nightmare, and FTPing is even worse!).
Are site is dynamic, but on these high traffic peak times, I'd like to be able to toss some files off to the filesystem.
That way, my webserver (nginx) can rapidly serve the static files if they're there. If not, they can generate them from the database.
Just like wp-cache, it's a way to survive a digging or TV mention.