[eluser]Rick Jolly[/eluser]
Hi Paul. Thank you for responding. I think there are two routes to take:
1) prevent cache file corruption with flock
2) detect cache file corruption from writes during reading
Prevent corruption with flock
I'm no expert, but I haven't come across a reliable way to solve the flock issues. Adding the non-blocking lock (LOCK_NB) seems to be the ideal solution - if it were supported by all operating systems...
[quote author="Paul Burdick" date="1203108520"]...Looking at your fix, you are using LOCK_NB, which is only available on Windows...[/quote]
PHP.net claims that it isn't supported by windows.
Still, as long as the non-blocking lock has some support, it seems worth including. Maybe first detect the OS and add the non-blocking lock if supported.
The locks work on the OS level, and every hack I've seen to replace flock doesn't work. Using rename/copy to replace the cache file would corrupt the file for those users simultaneously reading the file - I've tested that to be sure.
Detect corruption from writes during reading
So if flock is out, and you can't prevent cache file corruption for readers, then the next best thing is to detect corruption. In the event the cache has been updated during a read, then simply don't use the cache. To detect corruption, a hash, checksum, or a simple length of the original data is included in the cache file when writing. When the cache file is read, that check (hash, checksum, or length) is extracted from the file and compared with the same check on the actual data. Of course, the drawback is that there is overhead involved.