Home > Article > Backend Development > File locking problem in Zend_Search_Lucence_PHP tutorial
The file locking function in PHP is very unstable. In particular, it has a lot to do with the PHP version and platform. Today I saw the solution in Zend_Search_Lucence:
First flock a file. The lock mode is: flock($file, LOCK_EX | LOCK_NB). If the next process accesses $file again, it will no longer be able to obtain the lock of flock, so it will return a false, representing the file. It's locked and can no longer be accessed.
When Zend_Search_Lucence is locked, it is mainly: writing the index, reading the index, and optimizing the index.
About LOCK_NB:
It is better supported under Linux, but it is not supported until PHP 5.2.1 on Windows.
Therefore, it is best to use Zend_Search_Lucence with PHP 5.2.1 or above.
If the version is lower than PHP 5.2.1, the process will wait forever (because under Windows, versions lower than PHP 5.2.1 do not support non-blocking mode locking).
About updating the index: When updating the index, the index is often locked. Therefore, it is not recommended to update the index immediately. It is recommended to update the index at night when the number of visits is relatively small.