ÔõÑùÔÚThinkPHP6ÖоÙÐÐÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷£¿
ÔÚweb¿ª·¢ÖУ¬Îļþ²Ù×÷ÊǷǾ³£¼ûµÄʹÃüÖ®Ò»¡£ÔÚ´¦Öóͷ£Îļþʱ£¬ÎÒÃǾ³£ÐèÒªÓõ½¼ÓËøÏ¢ÕùËøµÄ²Ù×÷£¬ÒÔ±ÜÃâ¶à¸öÀú³Ìͬʱ¶Ôͳһ¸öÎļþ¾ÙÐжÁд²Ù×÷¶ø±¬·¢Êý¾Ý³åÍ»¡£ ÔÚthinkphp6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃphpµÄflockº¯ÊýÀ´ÊµÏÖÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷¡£½ÓÏÂÀ´£¬±¾ÎĽ«ÏêϸÏÈÈÝÔÚthinkphp6ÖÐÔõÑù¾ÙÐÐÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷¡£
1.Îļþ¼ÓËø²Ù×÷
ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃPHPµÄflockº¯ÊýÀ´ÊµÏÖÎļþ¼ÓËø²Ù×÷¡£¸Ãº¯ÊýÓÃÓÚ»ñÈ¡ÎļþËø£¬ÒÔ±ÜÃâÆäËûÀú³ÌÐÞ¸ÄÎļþ¡£ËüµÄÓï·¨ÈçÏ£º
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )
ÆäÖУ¬handle²ÎÊýÊÇÒ»¸ö·¿ªµÄÎļþ×ÊÔ´£¬operation²ÎÊýÊÇÒ»¸öËøµÄ²Ù×÷ÀàÐÍ£¬wouldblock²ÎÊýÊÇÒ»¸öÒýÓòÎÊý£¬ÌåÏÖÊÇ·ñ±»¹£Èû¡£operation²ÎÊý¿ÉÒÔÓÐÒÔÏÂÖµ:
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
LOCK_SH – ¹²ÏíËø£¬¶à¸öÀú³Ì¿Éͬʱ»ñÈ¡Ëø£¬µ«Ö»ÄܶÁ²»¿Éд¡£
LOCK_EX – ÅÅËûËø£¬Ö»ÓÐÒ»¸öÀú³Ì¿ÉÒÔ»ñÈ¡Ëø£¬¿ÉÒÔ¶ÁÒ²¿ÉÒÔд¡£
LOCK_UN – ÊÍ·ÅËø¡£
LOCK_NB – ÈôÊDz»Ï£Íûflockº¯ÊýÔÚ»ñÈ¡Ëøʱ¹£Èû£¬¿ÉÒÔÔÚ²Ù×÷²ÎÊýÖмÓÉÏLOCK_NB¡£
Õë¶ÔÒ»¸öʾÀý´úÂ룺
$file_path = '/path/to/file.txt'; $file_handle = fopen($file_path, 'a+'); if (flock($file_handle, LOCK_EX)) { //ÔÚ´Ë´¦¾ÙÐÐÎļþд²Ù×÷ flock($file_handle, LOCK_UN); //½âËøÎļþ } fclose($file_handle);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÑÝʾÁËÔõÑùÔÚÎļþ¾ÙÐÐд²Ù×÷ʱ¼ÓËø¡£Ê×ÏÈ£¬ÎÒÃÇʹÓÃÎļþ·¾¶·¿ªÎļþ¾ä±ú£¬È»ºóŲÓÃflockº¯ÊýÒÔʹÓÃÅÅËûËø½«Îļþ¼ÓËø¡£ÔÚÎļþ¼ÓËøÀֳɺó£¬ÎÒÃÇ¿ÉÒÔÔÚ´úÂë¿éÄÚÖ´ÐÐд²Ù×÷¡£×îºó£¬ÔÚ´úÂë¿éÍⲿ£¬ÎÒÃÇŲÓÃflockº¯ÊýÒÔÊÍ·ÅÎļþËø£¬¹Ø±ÕÎļþ¾ä±úÒÔ¿¢ÊÂÎļþ²Ù×÷¡£
2.Îļþ½âËø²Ù×÷
ÔÚÍê³ÉÎļþ²Ù×÷ºó£¬ÎÒÃÇÐèҪʹÓÃflockº¯ÊýÀ´½âËøÎļþ¡£¹ØÓÚÒѼÓËøµÄÎļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLOCK_UN²ÎÊýÀ´ÊÍ·ÅËø¡£
¹ØÓÚÊÍ·ÅËøµÄ´úÂëÈçÏ£º
flock($file_handle, LOCK_UN);
ÉÏÊö´úÂëÑÝʾÁËÔõÑùÔÚ´úÂë¿é¿¢ÊÂʱʹÓÃflockº¯Êý½âËøÎļþ¡£
×ÛÉÏ£¬±¾ÎÄÏêϸÏÈÈÝÁËÔÚThinkPHP6¿ò¼ÜÖÐʵÏÖÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷µÄÒªÁ졣ͨ¹ýʹÓÃflockº¯Êý£¬ÎÒÃÇ¿ÉÒÔÇáËÉʵÏÖÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷£¬´Ó¶ø°ü¹Ü¶à¸öÀú³ÌÔÚ²Ù×÷ͳһ¸öÎļþʱ²»»á±¬·¢Êý¾Ý³åÍ»¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐÐÎļþ¼ÓËøÏ¢ÕùËø²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡